Index: device/generic_sensor/platform_sensor_ambient_light_iio.cc |
diff --git a/device/generic_sensor/platform_sensor_ambient_light_iio.cc b/device/generic_sensor/platform_sensor_ambient_light_iio.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d3d392db40256b1c4f80f82ee48457ad842da5de |
--- /dev/null |
+++ b/device/generic_sensor/platform_sensor_ambient_light_iio.cc |
@@ -0,0 +1,104 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "device/generic_sensor/platform_sensor_ambient_light_iio.h" |
+ |
+#include "base/bind.h" |
+#include "device/generic_sensor/public/cpp/device_sensors_consts.h" |
+ |
+namespace device { |
+ |
+namespace { |
+ |
+void DestroySensorOnMainThread(scoped_refptr<PlatformSensor> sensor) { |
+ sensor = nullptr; |
+} |
+ |
+} // namespace |
+ |
+// static |
+scoped_refptr<PlatformSensor> PlatformSensorAmbientLightIio::CreateSensor( |
+ mojom::SensorType type, |
+ mojo::ScopedSharedBufferMapping mapping, |
+ PlatformSensorProvider* provider, |
+ scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) { |
+ scoped_refptr<PlatformSensorAmbientLightIio> sensor = |
+ new PlatformSensorAmbientLightIio(type, std::move(mapping), provider, |
+ ipc_task_runner); |
+ if (!sensor->sensor_exists()) { |
+ ipc_task_runner->PostTask(FROM_HERE, base::Bind(&DestroySensorOnMainThread, |
+ base::Passed(&sensor))); |
+ return nullptr; |
+ } |
+ return std::move(sensor); |
+} |
+ |
+PlatformSensorAmbientLightIio::PlatformSensorAmbientLightIio( |
+ mojom::SensorType type, |
+ mojo::ScopedSharedBufferMapping mapping, |
+ PlatformSensorProvider* provider, |
+ scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) |
+ : PollingPlatformSensor(type, |
+ std::move(mapping), |
+ provider, |
+ std::move(ipc_task_runner)), |
+ lux_value_(0.0), |
+ sensor_exists_(false), |
+ ambient_light_reader_(new AmbientLightSensorReaderIio()), |
+ weak_factory_(this) { |
+ DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); |
+ if (ambient_light_reader_->DetectLightSensor()) { |
+ sensor_exists_ = true; |
+ } |
+} |
+ |
+PlatformSensorAmbientLightIio::~PlatformSensorAmbientLightIio() = default; |
+ |
+mojom::ReportingMode PlatformSensorAmbientLightIio::GetReportingMode() { |
+ return mojom::ReportingMode::ON_CHANGE; |
+} |
+ |
+PlatformSensorConfiguration |
+PlatformSensorAmbientLightIio::GetDefaultConfiguration() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ return PlatformSensorConfiguration(kDefaultAmbientLightFrequencyHz); |
+} |
+ |
+bool PlatformSensorAmbientLightIio::CheckSensorConfiguration( |
+ const PlatformSensorConfiguration& configuration) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ return configuration.frequency() > 0 && |
+ configuration.frequency() <= kDefaultAmbientLightFrequencyHz; |
+} |
+ |
+void PlatformSensorAmbientLightIio::UpdateReading() { |
+ DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); |
+ |
+ double update = -1; |
+ if (!ambient_light_reader_->ReadSensorLuxValue(&update)) { |
+ OnSensorReadFailed(); |
+ return; |
+ } |
+ |
+ if (lux_value_ == update) { |
+ return; |
+ } |
+ lux_value_ = update; |
+ |
+ SensorReading reading; |
+ reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
+ reading.values[0] = lux_value_; |
+ |
+ bool needNotify = (GetReportingMode() == mojom::ReportingMode::ON_CHANGE); |
+ UpdateSensorReading(reading, needNotify); |
+} |
+ |
+void PlatformSensorAmbientLightIio::OnSensorReadFailed() { |
+ StopSensor(); |
+ task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&PlatformSensorAmbientLightIio::NotifySensorError, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+} // namespace device |