OLD | NEW |
(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 return PlatformSensorConfiguration(kDefaultAccelerometerFrequencyHz); |
| 61 } |
| 62 |
| 63 bool PlatformSensorAccelerometerMac::StartSensor( |
| 64 const PlatformSensorConfiguration& configuration) { |
| 65 if (!sudden_motion_sensor_) |
| 66 return false; |
| 67 |
| 68 float axis_value[3]; |
| 69 if (!sudden_motion_sensor_->ReadSensorValues(axis_value)) |
| 70 return false; |
| 71 |
| 72 timer_.Start( |
| 73 FROM_HERE, |
| 74 base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond / |
| 75 configuration.frequency()), |
| 76 this, &PlatformSensorAccelerometerMac::PollForData); |
| 77 |
| 78 return true; |
| 79 } |
| 80 |
| 81 void PlatformSensorAccelerometerMac::StopSensor() { |
| 82 timer_.Stop(); |
| 83 } |
| 84 |
| 85 void PlatformSensorAccelerometerMac::PollForData() { |
| 86 // Retrieve per-axis calibrated values. |
| 87 float axis_value[3]; |
| 88 if (!sudden_motion_sensor_->ReadSensorValues(axis_value)) |
| 89 return; |
| 90 |
| 91 SensorReading reading; |
| 92 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 93 reading.values[0] = axis_value[0] * kMeanGravity; |
| 94 reading.values[1] = axis_value[1] * kMeanGravity; |
| 95 reading.values[2] = axis_value[2] * kMeanGravity; |
| 96 |
| 97 if (IsSignificantlyDifferent(reading_, reading)) { |
| 98 reading_ = reading; |
| 99 UpdateSensorReading(reading, true); |
| 100 } |
| 101 } |
| 102 |
| 103 } // namespace device |
OLD | NEW |