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

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: add comments to unittest 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 namespace device {
8
9 namespace {
10
11 // Checks if at least one value has been changed.
12 bool HasValuesChanged(const SensorReading& lhs, const SensorReading& rhs) {
13 return lhs.values[0] != rhs.values[0] || lhs.values[1] != rhs.values[1] ||
14 lhs.values[2] != rhs.values[2];
15 }
16
17 } // namespace
18
19 PlatformSensorIio::PlatformSensorIio(
20 mojom::SensorType type,
21 mojo::ScopedSharedBufferMapping mapping,
22 PlatformSensorProvider* provider,
23 const SensorDataIio& data,
24 std::unique_ptr<base::Thread> polling_thread,
25 std::unique_ptr<SensorReader> sensor_reader)
26 : PlatformSensor(type, std::move(mapping), provider),
27 timer_(new base::RepeatingTimer()),
28 default_configuration_(data.default_configuration),
29 reporting_mode_(data.reporting_mode),
30 sensor_reader_(std::move(sensor_reader)),
31 polling_thread_(std::move(polling_thread)),
32 polling_thread_task_runner_(polling_thread_->task_runner()),
33 weak_factory_(this) {}
34
35 PlatformSensorIio::~PlatformSensorIio() {
36 polling_thread_task_runner_->DeleteSoon(FROM_HERE, timer_);
37 }
38
39 mojom::ReportingMode PlatformSensorIio::GetReportingMode() {
40 return reporting_mode_;
41 }
42
43 bool PlatformSensorIio::StartSensor(
44 const PlatformSensorConfiguration& configuration) {
45 DCHECK(task_runner_->BelongsToCurrentThread());
46 if (!polling_thread_->IsRunning() &&
47 !polling_thread_->StartWithOptions(
48 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
49 return false;
50 }
51
52 return polling_thread_task_runner_->PostTask(
53 FROM_HERE, base::Bind(&PlatformSensorIio::BeginPoll,
54 weak_factory_.GetWeakPtr(), configuration));
55 }
56
57 void PlatformSensorIio::StopSensor() {
58 DCHECK(task_runner_->BelongsToCurrentThread());
59 if (polling_thread_->IsRunning()) {
60 polling_thread_task_runner_->PostTask(
61 FROM_HERE, base::Bind(&PlatformSensorIio::StopPoll, this));
62 }
63 }
64
65 bool PlatformSensorIio::CheckSensorConfiguration(
66 const PlatformSensorConfiguration& configuration) {
67 DCHECK(task_runner_->BelongsToCurrentThread());
68 return configuration.frequency() > 0 &&
shalamov 2016/10/19 16:37:11 I think we have restricted frequency to be > 0 and
maksims (do not use this acc) 2016/10/20 09:50:36 It will be sensor dependent, when I start to work
69 configuration.frequency() <=
70 mojom::SensorConfiguration::kMaxAllowedFrequency;
71 }
72
73 PlatformSensorConfiguration PlatformSensorIio::GetDefaultConfiguration() {
74 DCHECK(task_runner_->BelongsToCurrentThread());
75 return default_configuration_;
76 }
77
78 void PlatformSensorIio::BeginPoll(
79 const PlatformSensorConfiguration& configuration) {
80 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
81 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds(
82 base::Time::kMicrosecondsPerSecond /
83 configuration.frequency()),
84 this, &PlatformSensorIio::PollForReadingData);
85 }
86
87 void PlatformSensorIio::StopPoll() {
88 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
89 timer_->Stop();
90 }
91
92 void PlatformSensorIio::PollForReadingData() {
93 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
94
95 SensorReading reading;
96 if (!sensor_reader_->ReadSensorReading(&reading)) {
97 task_runner_->PostTask(
98 FROM_HERE, base::Bind(&PlatformSensorIio::NotifySensorError, this));
99 StopPoll();
100 return;
101 }
102
103 bool notifyNeeded = false;
104 if ((GetReportingMode() == mojom::ReportingMode::ON_CHANGE)) {
105 if (!HasValuesChanged(reading, old_values_)) {
106 return;
107 }
108 notifyNeeded = true;
109 }
110
111 old_values_ = reading;
112 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
113 UpdateSensorReading(reading, notifyNeeded);
114 }
115
116 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698