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

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

Issue 2370343002: [sensors] Ambient light sensor implementation for ChromeOS and Linux. (Closed)
Patch Set: Comments from Alex 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_iio.h"
6
7 #include "base/threading/thread.h"
8 #include "base/timer/timer.h"
9 #include "device/generic_sensor/iio/platform_sensor_utils_iio.h"
10 #include "device/generic_sensor/iio/sensor_data_iio.h"
11
12 namespace device {
13
14 namespace {
15
16 // Checks if at least one value has been changed.
17 bool HasValuesChanged(const SensorReading& lhs, const SensorReading& rhs) {
Reilly Grant (use Gerrit) 2016/10/24 21:49:27 HaveValuesChanged
maksims (do not use this acc) 2016/10/25 06:23:54 Done.
18 return lhs.values[0] != rhs.values[0] || lhs.values[1] != rhs.values[1] ||
19 lhs.values[2] != rhs.values[2];
20 }
21
22 } // namespace
23
24 PlatformSensorIio::PlatformSensorIio(
25 mojom::SensorType type,
26 mojo::ScopedSharedBufferMapping mapping,
27 PlatformSensorProvider* provider,
28 const SensorDataIio& data,
29 std::unique_ptr<base::Thread> polling_thread,
30 std::unique_ptr<SensorReader> sensor_reader)
31 : PlatformSensor(type, std::move(mapping), provider),
32 timer_(new base::RepeatingTimer()),
33 default_configuration_(data.default_configuration),
34 reporting_mode_(data.reporting_mode),
35 sensor_reader_(std::move(sensor_reader)),
36 polling_thread_(std::move(polling_thread)),
37 polling_thread_task_runner_(polling_thread_->task_runner()),
38 weak_factory_(this) {}
39
40 PlatformSensorIio::~PlatformSensorIio() {
41 polling_thread_task_runner_->DeleteSoon(FROM_HERE, timer_);
42 }
43
44 mojom::ReportingMode PlatformSensorIio::GetReportingMode() {
45 return reporting_mode_;
46 }
47
48 bool PlatformSensorIio::StartSensor(
49 const PlatformSensorConfiguration& configuration) {
50 DCHECK(task_runner_->BelongsToCurrentThread());
51 if (!polling_thread_->IsRunning() &&
52 !polling_thread_->StartWithOptions(
53 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
54 return false;
55 }
56
57 return polling_thread_task_runner_->PostTask(
58 FROM_HERE, base::Bind(&PlatformSensorIio::BeginPoll,
59 weak_factory_.GetWeakPtr(), configuration));
60 }
61
62 void PlatformSensorIio::StopSensor() {
63 DCHECK(task_runner_->BelongsToCurrentThread());
64 if (polling_thread_->IsRunning()) {
65 polling_thread_task_runner_->PostTask(
66 FROM_HERE, base::Bind(&PlatformSensorIio::StopPoll, this));
67 }
68 }
69
70 bool PlatformSensorIio::CheckSensorConfiguration(
71 const PlatformSensorConfiguration& configuration) {
72 DCHECK(task_runner_->BelongsToCurrentThread());
73 // TODO(maksims): make this sensor dependent.
74 // For example, in case of accelerometer, check current polling frequency
75 // exposed by iio driver.
76 return configuration.frequency() > 0 &&
77 configuration.frequency() <=
78 mojom::SensorConfiguration::kMaxAllowedFrequency;
79 }
80
81 PlatformSensorConfiguration PlatformSensorIio::GetDefaultConfiguration() {
82 DCHECK(task_runner_->BelongsToCurrentThread());
83 return default_configuration_;
84 }
85
86 void PlatformSensorIio::BeginPoll(
87 const PlatformSensorConfiguration& configuration) {
88 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
89 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds(
90 base::Time::kMicrosecondsPerSecond /
91 configuration.frequency()),
92 this, &PlatformSensorIio::PollForReadingData);
93 }
94
95 void PlatformSensorIio::StopPoll() {
96 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
97 timer_->Stop();
98 }
99
100 void PlatformSensorIio::PollForReadingData() {
101 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
102
103 SensorReading reading;
104 if (!sensor_reader_->ReadSensorReading(&reading)) {
105 task_runner_->PostTask(
106 FROM_HERE, base::Bind(&PlatformSensorIio::NotifySensorError, this));
107 StopPoll();
108 return;
109 }
110
111 bool notifyNeeded = false;
112 if ((GetReportingMode() == mojom::ReportingMode::ON_CHANGE)) {
Reilly Grant (use Gerrit) 2016/10/24 21:49:27 nit: Extra set of parens.
maksims (do not use this acc) 2016/10/25 06:23:54 Done.
113 if (!HasValuesChanged(reading, old_values_)) {
114 return;
115 }
Reilly Grant (use Gerrit) 2016/10/24 21:49:27 nit: No braces around single-line if.
maksims (do not use this acc) 2016/10/25 06:23:54 Done.
116 notifyNeeded = true;
117 }
118
119 old_values_ = reading;
120 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
121 UpdateSensorReading(reading, notifyNeeded);
122 }
123
124 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698