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_fusion_relative_orientation.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <cmath> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "device/generic_sensor/generic_sensor_consts.h" |
| 15 #include "device/generic_sensor/platform_sensor_provider.h" |
| 16 #include "device/generic_sensor/relative_orientation_fusion_algorithm_using_acce
lerometer.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 constexpr double kQuaternionThreshold = 0.01; |
| 21 |
| 22 bool IsSignificantlyDifferent(const device::SensorReading& reading1, |
| 23 const device::SensorReading& reading2) { |
| 24 return (std::fabs(reading1.values[0] - reading2.values[0]) >= |
| 25 kQuaternionThreshold) || |
| 26 (std::fabs(reading1.values[1] - reading2.values[1]) >= |
| 27 kQuaternionThreshold) || |
| 28 (std::fabs(reading1.values[2] - reading2.values[2]) >= |
| 29 kQuaternionThreshold) || |
| 30 (std::fabs(reading1.values[3] - reading2.values[3]) >= |
| 31 kQuaternionThreshold); |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 namespace device { |
| 37 |
| 38 PlatformSensorFusionRelativeOrientation:: |
| 39 PlatformSensorFusionRelativeOrientation( |
| 40 mojo::ScopedSharedBufferMapping mapping, |
| 41 PlatformSensorProvider* provider, |
| 42 const PlatformSensorProviderBase::CreateSensorCallback& callback) |
| 43 : PlatformSensor(mojom::SensorType::RELATIVE_ORIENTATION, |
| 44 std::move(mapping), |
| 45 provider), |
| 46 callback_(callback) { |
| 47 accelerometer_ = provider->GetSensor(mojom::SensorType::ACCELEROMETER); |
| 48 if (accelerometer_) { |
| 49 CreateAccelerometerSucceeded(); |
| 50 } else { |
| 51 provider->CreateSensor(mojom::SensorType::ACCELEROMETER, |
| 52 base::Bind(&PlatformSensorFusionRelativeOrientation:: |
| 53 CreateAccelerometerCallback, |
| 54 base::Unretained(this))); |
| 55 } |
| 56 } |
| 57 |
| 58 PlatformSensorFusionRelativeOrientation:: |
| 59 ~PlatformSensorFusionRelativeOrientation() { |
| 60 if (accelerometer_) |
| 61 accelerometer_->RemoveClient(this); |
| 62 } |
| 63 |
| 64 mojom::ReportingMode |
| 65 PlatformSensorFusionRelativeOrientation::GetReportingMode() { |
| 66 return accelerometer_->GetReportingMode(); |
| 67 } |
| 68 |
| 69 PlatformSensorConfiguration |
| 70 PlatformSensorFusionRelativeOrientation::GetDefaultConfiguration() { |
| 71 return accelerometer_->GetDefaultConfiguration(); |
| 72 } |
| 73 |
| 74 bool PlatformSensorFusionRelativeOrientation::StartSensor( |
| 75 const PlatformSensorConfiguration& configuration) { |
| 76 return accelerometer_->StartSensor(configuration); |
| 77 } |
| 78 |
| 79 void PlatformSensorFusionRelativeOrientation::StopSensor() { |
| 80 accelerometer_->StopSensor(); |
| 81 } |
| 82 |
| 83 void PlatformSensorFusionRelativeOrientation::OnSensorReadingChanged() { |
| 84 SensorReading reading; |
| 85 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 86 |
| 87 if (!accelerometer_->GetLatestReading(&reading)) |
| 88 return; |
| 89 |
| 90 fusion_algorithm_->set_acceleration(reading.values[0], reading.values[1], |
| 91 reading.values[2]); |
| 92 |
| 93 fusion_algorithm_->GetRelativeOrientationData( |
| 94 &reading.values[0].value(), &reading.values[1].value(), |
| 95 &reading.values[2].value(), &reading.values[3].value()); |
| 96 |
| 97 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE && |
| 98 !IsSignificantlyDifferent(reading_, reading)) { |
| 99 return; |
| 100 } |
| 101 |
| 102 reading_ = reading; |
| 103 UpdateSensorReading(reading_, |
| 104 GetReportingMode() == mojom::ReportingMode::ON_CHANGE); |
| 105 } |
| 106 |
| 107 void PlatformSensorFusionRelativeOrientation::OnSensorError() { |
| 108 NotifySensorError(); |
| 109 } |
| 110 |
| 111 bool PlatformSensorFusionRelativeOrientation::IsNotificationSuspended() { |
| 112 for (auto& client : clients_) { |
| 113 if (!client.IsNotificationSuspended()) |
| 114 return false; |
| 115 } |
| 116 return true; |
| 117 } |
| 118 |
| 119 bool PlatformSensorFusionRelativeOrientation::CheckSensorConfiguration( |
| 120 const PlatformSensorConfiguration& configuration) { |
| 121 return accelerometer_->CheckSensorConfiguration(configuration); |
| 122 } |
| 123 |
| 124 void PlatformSensorFusionRelativeOrientation::CreateAccelerometerCallback( |
| 125 scoped_refptr<PlatformSensor> accelerometer) { |
| 126 accelerometer_ = accelerometer; |
| 127 if (accelerometer_) { |
| 128 CreateAccelerometerSucceeded(); |
| 129 } else { |
| 130 callback_.Run(nullptr); |
| 131 } |
| 132 } |
| 133 |
| 134 void PlatformSensorFusionRelativeOrientation::CreateAccelerometerSucceeded() { |
| 135 accelerometer_->AddClient(this); |
| 136 fusion_algorithm_ = |
| 137 base::MakeUnique<RelativeOrientationFusionAlgorithmUsingAccelerometer>(); |
| 138 callback_.Run(this); |
| 139 } |
| 140 |
| 141 } // namespace device |
OLD | NEW |