Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(619)

Side by Side Diff: device/generic_sensor/platform_sensor_ambient_light_iio.cc

Issue 2370343002: [sensors] Ambient light sensor implementation for ChromeOS and Linux. (Closed)
Patch Set: rebased on top of Mihail's cl Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "device/generic_sensor/platform_sensor_ambient_light_iio.h"
6
7 #include "base/bind.h"
8 #include "device/generic_sensor/public/cpp/device_sensors_consts.h"
9
10 namespace device {
11
12 namespace {
13
14 void DestroySensorOnMainThread(scoped_refptr<PlatformSensor> sensor) {
15 sensor = nullptr;
16 }
17
18 } // namespace
19
20 // static
21 scoped_refptr<PlatformSensor> PlatformSensorAmbientLightIio::CreateSensor(
22 mojom::SensorType type,
23 mojo::ScopedSharedBufferMapping mapping,
24 PlatformSensorProvider* provider,
25 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) {
26 scoped_refptr<PlatformSensorAmbientLightIio> sensor =
27 new PlatformSensorAmbientLightIio(type, std::move(mapping), provider,
28 ipc_task_runner);
29 if (!sensor->sensor_exists()) {
30 ipc_task_runner->PostTask(FROM_HERE, base::Bind(&DestroySensorOnMainThread,
31 base::Passed(&sensor)));
32 return nullptr;
33 }
34 return std::move(sensor);
35 }
36
37 PlatformSensorAmbientLightIio::PlatformSensorAmbientLightIio(
38 mojom::SensorType type,
39 mojo::ScopedSharedBufferMapping mapping,
40 PlatformSensorProvider* provider,
41 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner)
42 : PollingPlatformSensor(type,
43 std::move(mapping),
44 provider,
45 std::move(ipc_task_runner)),
46 lux_value_(0.0),
47 sensor_exists_(false),
48 ambient_light_reader_(new AmbientLightSensorReaderIio()),
49 weak_factory_(this) {
50 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
51 if (ambient_light_reader_->DetectLightSensor()) {
52 sensor_exists_ = true;
53 }
54 }
55
56 PlatformSensorAmbientLightIio::~PlatformSensorAmbientLightIio() = default;
57
58 mojom::ReportingMode PlatformSensorAmbientLightIio::GetReportingMode() {
59 return mojom::ReportingMode::ON_CHANGE;
60 }
61
62 PlatformSensorConfiguration
63 PlatformSensorAmbientLightIio::GetDefaultConfiguration() {
64 DCHECK(task_runner_->BelongsToCurrentThread());
65 return PlatformSensorConfiguration(kDefaultAmbientLightFrequencyHz);
66 }
67
68 bool PlatformSensorAmbientLightIio::CheckSensorConfiguration(
69 const PlatformSensorConfiguration& configuration) {
70 DCHECK(task_runner_->BelongsToCurrentThread());
71 return configuration.frequency() > 0 &&
72 configuration.frequency() <= kDefaultAmbientLightFrequencyHz;
73 }
74
75 void PlatformSensorAmbientLightIio::UpdateReading() {
76 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
77
78 double update = -1;
79 if (!ambient_light_reader_->ReadSensorLuxValue(&update)) {
80 OnSensorReadFailed();
81 return;
82 }
83
84 if (lux_value_ == update) {
85 return;
86 }
87 lux_value_ = update;
88
89 SensorReading reading;
90 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
91 reading.values[0] = lux_value_;
92
93 bool needNotify = (GetReportingMode() == mojom::ReportingMode::ON_CHANGE);
94 UpdateSensorReading(reading, needNotify);
95 }
96
97 void PlatformSensorAmbientLightIio::OnSensorReadFailed() {
98 StopSensor();
99 task_runner_->PostTask(
100 FROM_HERE, base::Bind(&PlatformSensorAmbientLightIio::NotifySensorError,
101 weak_factory_.GetWeakPtr()));
102 }
103
104 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698