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

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: Newest version. and some comments 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 weak_factory_(this) {
33 polling_thread_task_runner_ = polling_thread_->task_runner();
Mikhail 2016/10/14 13:59:05 could be inited in ctor init list.
maksims (do not use this acc) 2016/10/17 06:06:59 Done.
34 }
35
36 PlatformSensorIio::~PlatformSensorIio() {
37 polling_thread_task_runner_->DeleteSoon(FROM_HERE, timer_);
38 }
39
40 mojom::ReportingMode PlatformSensorIio::GetReportingMode() {
41 return reporting_mode_;
42 }
43
44 bool PlatformSensorIio::StartSensor(
45 const PlatformSensorConfiguration& configuration) {
46 DCHECK(task_runner_->BelongsToCurrentThread());
47 if (!polling_thread_->IsRunning() &&
48 !polling_thread_->StartWithOptions(
49 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
50 return false;
51 }
52
53 return polling_thread_task_runner_->PostTask(
54 FROM_HERE, base::Bind(&PlatformSensorIio::BeginPoll,
55 weak_factory_.GetWeakPtr(), configuration));
56 }
57
58 void PlatformSensorIio::StopSensor() {
59 DCHECK(task_runner_->BelongsToCurrentThread());
60 if (polling_thread_->IsRunning()) {
61 polling_thread_task_runner_->PostTask(
62 FROM_HERE, base::Bind(&PlatformSensorIio::StopPoll, this));
63 }
64 }
65
66 bool PlatformSensorIio::CheckSensorConfiguration(
67 const PlatformSensorConfiguration& configuration) {
68 DCHECK(task_runner_->BelongsToCurrentThread());
69 return configuration.frequency() > 0 &&
70 configuration.frequency() <=
71 mojom::SensorConfiguration::kMaxAllowedFrequency;
72 }
73
74 PlatformSensorConfiguration PlatformSensorIio::GetDefaultConfiguration() {
75 DCHECK(task_runner_->BelongsToCurrentThread());
76 return default_configuration_;
77 }
78
79 void PlatformSensorIio::BeginPoll(
80 const PlatformSensorConfiguration& configuration) {
81 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
82 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds(
83 base::Time::kMicrosecondsPerSecond /
84 configuration.frequency()),
85 this, &PlatformSensorIio::PollForReadingData);
86 }
87
88 void PlatformSensorIio::StopPoll() {
89 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
90 timer_->Stop();
91 }
92
93 void PlatformSensorIio::PollForReadingData() {
94 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
95
96 SensorReading reading;
97 if (!sensor_reader_->ReadSensorReading(&reading)) {
98 StopPoll();
99 return;
100 }
101
102 bool notifyNeeded = false;
103 if ((GetReportingMode() == mojom::ReportingMode::ON_CHANGE)) {
Mikhail 2016/10/14 13:59:05 GetReportingMode is supposed to be invoked from ot
maksims (do not use this acc) 2016/10/17 06:06:59 reporting_mode_ is already const.
104 if (!HasValuesChanged(reading, old_values_)) {
105 return;
106 }
107 notifyNeeded = true;
108 }
109
110 old_values_ = reading;
111 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
112 UpdateSensorReading(reading, notifyNeeded);
113 }
114
115 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698