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

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

Issue 2880743003: [Device Service] Add ACCELEROMETER implementation on Mac to //device/generic_sensor (Closed)
Patch Set: add ACCELEROMETER implementation on Mac to //device/generic_sensor Created 3 years, 7 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 2017 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_accelerometer_mac.h"
6
7 #include <stdint.h>
8
9 #include <cmath>
10
11 #include "base/bind.h"
12 #include "device/base/synchronization/shared_memory_seqlock_buffer.h"
13 #include "device/generic_sensor/generic_sensor_consts.h"
14 #include "device/generic_sensor/platform_sensor_provider_mac.h"
15 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h"
16
17 namespace {
18
19 constexpr double kMeanGravity = 9.80665;
20
21 constexpr double kGravityThreshold = kMeanGravity * 0.01;
22
23 bool IsSignificantlyDifferent(const device::SensorReading& reading1,
24 const device::SensorReading& reading2) {
25 return (std::fabs(reading1.values[0] - reading2.values[0]) >=
26 kGravityThreshold) ||
27 (std::fabs(reading1.values[1] - reading2.values[1]) >=
28 kGravityThreshold) ||
29 (std::fabs(reading1.values[2] - reading2.values[2]) >=
30 kGravityThreshold);
31 }
32
33 } // namespace
34
35 namespace device {
36
37 PlatformSensorAccelerometerMac::PlatformSensorAccelerometerMac(
38 mojo::ScopedSharedBufferMapping mapping,
39 PlatformSensorProvider* provider)
40 : PlatformSensor(mojom::SensorType::ACCELEROMETER,
41 std::move(mapping),
42 provider),
43 sudden_motion_sensor_(SuddenMotionSensor::Create()) {}
44
45 PlatformSensorAccelerometerMac::~PlatformSensorAccelerometerMac() = default;
46
47 mojom::ReportingMode PlatformSensorAccelerometerMac::GetReportingMode() {
48 return mojom::ReportingMode::ON_CHANGE;
49 }
50
51 bool PlatformSensorAccelerometerMac::CheckSensorConfiguration(
52 const PlatformSensorConfiguration& configuration) {
53 return configuration.frequency() > 0 &&
54 configuration.frequency() <=
55 mojom::SensorConfiguration::kMaxAllowedFrequency;
56 }
57
58 PlatformSensorConfiguration
59 PlatformSensorAccelerometerMac::GetDefaultConfiguration() {
60 PlatformSensorConfiguration default_configuration;
61 default_configuration.set_frequency(kDefaultAccelerometerFrequencyHz);
62 return default_configuration;
Reilly Grant (use Gerrit) 2017/05/13 00:33:16 return PlatformSensorConfiguration(kDefaultAcceler
juncai 2017/05/13 00:53:49 Done.
63 }
64
65 bool PlatformSensorAccelerometerMac::StartSensor(
66 const PlatformSensorConfiguration& configuration) {
67 if (!sudden_motion_sensor_)
68 return false;
69
70 float axis_value[3];
71 if (!sudden_motion_sensor_->ReadSensorValues(axis_value))
72 return false;
73
74 timer_.Start(
75 FROM_HERE,
76 base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond /
77 configuration.frequency()),
78 this, &PlatformSensorAccelerometerMac::PollForData);
79
80 return true;
81 }
82
83 void PlatformSensorAccelerometerMac::StopSensor() {
84 timer_.Stop();
85 }
86
87 void PlatformSensorAccelerometerMac::PollForData() {
88 // Retrieve per-axis calibrated values.
89 float axis_value[3];
90 if (!sudden_motion_sensor_->ReadSensorValues(axis_value))
91 return;
92
93 SensorReading reading;
94 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
95 reading.values[0] = axis_value[0] * kMeanGravity;
96 reading.values[1] = axis_value[1] * kMeanGravity;
97 reading.values[2] = axis_value[2] * kMeanGravity;
98
99 if (IsSignificantlyDifferent(reading_, reading)) {
100 reading_ = reading;
101 UpdateSensorReading(reading, true);
102 }
103 }
104
105 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698