| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "power_manager/ambient_light_sensor.h" | 5 #include "power_manager/ambient_light_sensor.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 | 12 |
| 13 namespace power_manager { | 13 namespace power_manager { |
| 14 | 14 |
| 15 // Period in which to poll the ambient light sensor. | 15 // Period in which to poll the ambient light sensor. |
| 16 static const int kSensorPollPeriodMs = 1000; | 16 static const int kSensorPollPeriodMs = 1000; |
| 17 | 17 |
| 18 AmbientLightSensor::AmbientLightSensor(BacklightController* controller) | 18 AmbientLightSensor::AmbientLightSensor(BacklightController* controller) |
| 19 : controller_(controller), | 19 : controller_(controller), |
| 20 als_fd_(-1), | 20 als_fd_(-1), |
| 21 last_level_(0), | 21 last_level_(0), |
| 22 is_polling_(false), | 22 is_polling_(false), |
| 23 disable_polling_(false), | 23 disable_polling_(false), |
| 24 still_deferring_(false) {} | 24 still_deferring_(false) {} |
| 25 | 25 |
| 26 AmbientLightSensor::~AmbientLightSensor() { | 26 AmbientLightSensor::~AmbientLightSensor() { |
| 27 if (als_fd_ >= 0) | 27 if (als_fd_ >= 0) |
| 28 close(als_fd_); | 28 close(als_fd_); |
| 29 } | 29 } |
| 30 | 30 |
| 31 bool AmbientLightSensor::DeferredInit(AmbientLightSensor* self) { | 31 bool AmbientLightSensor::DeferredInit() { |
| 32 // tsl2561 is currently the only supported light sensor. | 32 // tsl2561 is currently the only supported light sensor. |
| 33 // If the lux file is not immediately found, issue a deferral | 33 // If the lux file is not immediately found, issue a deferral |
| 34 // message and try again later. | 34 // message and try again later. |
| 35 self->als_fd_ = open("/sys/class/iio/device0/lux", O_RDONLY); | 35 als_fd_ = open("/sys/class/iio/device0/lux", O_RDONLY); |
| 36 if (self->als_fd_ == -1) { | 36 if (als_fd_ == -1) { |
| 37 if (self->still_deferring_) | 37 if (still_deferring_) |
| 38 return false; | 38 return false; |
| 39 LOG(WARNING) << "Deferring lux: " << strerror(errno); | 39 LOG(WARNING) << "Deferring lux: " << strerror(errno); |
| 40 self->still_deferring_ = true; | 40 still_deferring_ = true; |
| 41 return false; | 41 return false; |
| 42 } | 42 } |
| 43 if (self->still_deferring_) | 43 if (still_deferring_) |
| 44 LOG(INFO) << "Finally found the lux file"; | 44 LOG(INFO) << "Finally found the lux file"; |
| 45 return true; | 45 return true; |
| 46 } | 46 } |
| 47 | 47 |
| 48 bool AmbientLightSensor::Init() { | 48 bool AmbientLightSensor::Init() { |
| 49 if (controller_) | 49 if (controller_) |
| 50 controller_->set_light_sensor(this); | 50 controller_->set_light_sensor(this); |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 | 53 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 65 // it is already on. | 65 // it is already on. |
| 66 // is_polling_ resolves the race. No locking is needed in this single | 66 // is_polling_ resolves the race. No locking is needed in this single |
| 67 // threaded application. | 67 // threaded application. |
| 68 disable_polling_ = false; | 68 disable_polling_ = false; |
| 69 if (is_polling_) | 69 if (is_polling_) |
| 70 return; // already polling. | 70 return; // already polling. |
| 71 | 71 |
| 72 // Start polling. | 72 // Start polling. |
| 73 LOG(INFO) << "Enabling light sensor poll"; | 73 LOG(INFO) << "Enabling light sensor poll"; |
| 74 is_polling_ = true; | 74 is_polling_ = true; |
| 75 g_timeout_add(kSensorPollPeriodMs, ReadAls, this); | 75 g_timeout_add(kSensorPollPeriodMs, ReadAlsThunk, this); |
| 76 } | 76 } |
| 77 | 77 |
| 78 gboolean AmbientLightSensor::ReadAls(gpointer data) { | 78 gboolean AmbientLightSensor::ReadAls() { |
| 79 AmbientLightSensor* self = static_cast<AmbientLightSensor*>(data); | 79 if (disable_polling_) { |
| 80 if (self->disable_polling_) { | 80 is_polling_ = false; |
| 81 self->is_polling_ = false; | |
| 82 return false; // Returning false removes the timeout. | 81 return false; // Returning false removes the timeout. |
| 83 } | 82 } |
| 84 | 83 |
| 85 // We really want to read the ambient light level. | 84 // We really want to read the ambient light level. |
| 86 // Complete the deferred lux file open if necessary. | 85 // Complete the deferred lux file open if necessary. |
| 87 if (self->als_fd_ < 0) { | 86 if (als_fd_ < 0) { |
| 88 if (!DeferredInit(self)) | 87 if (!DeferredInit()) |
| 89 return true; // Return true to try again later. | 88 return true; // Return true to try again later. |
| 90 } | 89 } |
| 91 | 90 |
| 92 char buffer[10]; | 91 char buffer[10]; |
| 93 int n; | 92 int n; |
| 94 if (lseek(self->als_fd_, 0, SEEK_SET) != 0 || | 93 if (lseek(als_fd_, 0, SEEK_SET) != 0 || |
| 95 (n = read(self->als_fd_, buffer, sizeof(buffer) - 1)) == -1) { | 94 (n = read(als_fd_, buffer, sizeof(buffer) - 1)) == -1) { |
| 96 LOG(WARNING) << "Unable to read light sensor file"; | 95 LOG(WARNING) << "Unable to read light sensor file"; |
| 97 } | 96 } |
| 98 if (n > 0 && n < static_cast<int>(sizeof(buffer))) { | 97 if (n > 0 && n < static_cast<int>(sizeof(buffer))) { |
| 99 buffer[n] = '\0'; | 98 buffer[n] = '\0'; |
| 100 int luxval = atoi(buffer); | 99 int luxval = atoi(buffer); |
| 101 int64 level = Tsl2563LuxToLevel(luxval); | 100 int64 level = Tsl2563LuxToLevel(luxval); |
| 102 if (level != self->last_level_ && self->controller_) | 101 if (level != last_level_ && controller_) |
| 103 self->controller_->SetAlsBrightnessLevel(level); | 102 controller_->SetAlsBrightnessLevel(level); |
| 104 self->last_level_ = level; | 103 last_level_ = level; |
| 105 } | 104 } |
| 106 return true; | 105 return true; |
| 107 } | 106 } |
| 108 | 107 |
| 109 int64 AmbientLightSensor::Tsl2563LuxToLevel(int luxval) { | 108 int64 AmbientLightSensor::Tsl2563LuxToLevel(int luxval) { |
| 110 // Notes on tsl2563 Ambient Light Response (_ALR) table: | 109 // Notes on tsl2563 Ambient Light Response (_ALR) table: |
| 111 // | 110 // |
| 112 // measurement location: lux file value, intended luma level | 111 // measurement location: lux file value, intended luma level |
| 113 // dark room: 0, 0 | 112 // dark room: 0, 0 |
| 114 // office: 75, 50 | 113 // office: 75, 50 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 133 // there is an exact match), or just off the end of the table if there is no | 132 // there is an exact match), or just off the end of the table if there is no |
| 134 // next highest value. | 133 // next highest value. |
| 135 int* bound = std::lower_bound(lux_table, lux_table + arraysize(lux_table), | 134 int* bound = std::lower_bound(lux_table, lux_table + arraysize(lux_table), |
| 136 luxval); | 135 luxval); |
| 137 | 136 |
| 138 // Normalize table pointer to 100. | 137 // Normalize table pointer to 100. |
| 139 return ((bound - lux_table) * 100 / arraysize(lux_table)); | 138 return ((bound - lux_table) * 100 / arraysize(lux_table)); |
| 140 } | 139 } |
| 141 | 140 |
| 142 } // namespace power_manager | 141 } // namespace power_manager |
| OLD | NEW |