| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/sensors/sensor_manager_chromeos.h" | 5 #include "device/sensors/sensor_manager_chromeos.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "chromeos/accelerometer/accelerometer_reader.h" | 9 #include "chromeos/accelerometer/accelerometer_reader.h" |
| 10 #include "chromeos/accelerometer/accelerometer_types.h" | 10 #include "chromeos/accelerometer/accelerometer_types.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 StartObservingAccelerometer(); | 41 StartObservingAccelerometer(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 bool SensorManagerChromeOS::StopFetchingDeviceMotionData() { | 44 bool SensorManagerChromeOS::StopFetchingDeviceMotionData() { |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | 45 DCHECK(thread_checker_.CalledOnValidThread()); |
| 46 if (!motion_buffer_) | 46 if (!motion_buffer_) |
| 47 return false; | 47 return false; |
| 48 | 48 |
| 49 // Make sure to indicate that the sensor data is no longer available. | 49 // Make sure to indicate that the sensor data is no longer available. |
| 50 motion_buffer_->seqlock.WriteBegin(); | 50 motion_buffer_->seqlock.WriteBegin(); |
| 51 motion_buffer_->data.allAvailableSensorsAreActive = false; | 51 motion_buffer_->data.all_available_sensors_are_active = false; |
| 52 motion_buffer_->seqlock.WriteEnd(); | 52 motion_buffer_->seqlock.WriteEnd(); |
| 53 | 53 |
| 54 motion_buffer_ = nullptr; | 54 motion_buffer_ = nullptr; |
| 55 | 55 |
| 56 if (!orientation_buffer_) | 56 if (!orientation_buffer_) |
| 57 StopObservingAccelerometer(); | 57 StopObservingAccelerometer(); |
| 58 return true; | 58 return true; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void SensorManagerChromeOS::StartFetchingDeviceOrientationData( | 61 void SensorManagerChromeOS::StartFetchingDeviceOrientationData( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 72 if (!motion_buffer_) | 72 if (!motion_buffer_) |
| 73 StartObservingAccelerometer(); | 73 StartObservingAccelerometer(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | 76 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
| 77 DCHECK(thread_checker_.CalledOnValidThread()); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 78 if (!orientation_buffer_) | 78 if (!orientation_buffer_) |
| 79 return false; | 79 return false; |
| 80 // Make sure to indicate that the sensor data is no longer available. | 80 // Make sure to indicate that the sensor data is no longer available. |
| 81 orientation_buffer_->seqlock.WriteBegin(); | 81 orientation_buffer_->seqlock.WriteBegin(); |
| 82 orientation_buffer_->data.allAvailableSensorsAreActive = false; | 82 orientation_buffer_->data.all_available_sensors_are_active = false; |
| 83 orientation_buffer_->seqlock.WriteEnd(); | 83 orientation_buffer_->seqlock.WriteEnd(); |
| 84 orientation_buffer_ = nullptr; | 84 orientation_buffer_ = nullptr; |
| 85 | 85 |
| 86 if (!motion_buffer_) | 86 if (!motion_buffer_) |
| 87 StopObservingAccelerometer(); | 87 StopObservingAccelerometer(); |
| 88 return true; | 88 return true; |
| 89 } | 89 } |
| 90 | 90 |
| 91 void SensorManagerChromeOS::OnAccelerometerUpdated( | 91 void SensorManagerChromeOS::OnAccelerometerUpdated( |
| 92 scoped_refptr<const chromeos::AccelerometerUpdate> update) { | 92 scoped_refptr<const chromeos::AccelerometerUpdate> update) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 113 | 113 |
| 114 void SensorManagerChromeOS::StopObservingAccelerometer() { | 114 void SensorManagerChromeOS::StopObservingAccelerometer() { |
| 115 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | 115 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void SensorManagerChromeOS::GenerateMotionEvent(double x, double y, double z) { | 118 void SensorManagerChromeOS::GenerateMotionEvent(double x, double y, double z) { |
| 119 if (!motion_buffer_) | 119 if (!motion_buffer_) |
| 120 return; | 120 return; |
| 121 | 121 |
| 122 motion_buffer_->seqlock.WriteBegin(); | 122 motion_buffer_->seqlock.WriteBegin(); |
| 123 motion_buffer_->data.accelerationIncludingGravityX = x; | 123 motion_buffer_->data.acceleration_including_gravity_x = x; |
| 124 motion_buffer_->data.hasAccelerationIncludingGravityX = true; | 124 motion_buffer_->data.has_acceleration_including_gravity_x = true; |
| 125 motion_buffer_->data.accelerationIncludingGravityY = y; | 125 motion_buffer_->data.acceleration_including_gravity_y = y; |
| 126 motion_buffer_->data.hasAccelerationIncludingGravityY = true; | 126 motion_buffer_->data.has_acceleration_including_gravity_y = true; |
| 127 motion_buffer_->data.accelerationIncludingGravityZ = z; | 127 motion_buffer_->data.acceleration_including_gravity_z = z; |
| 128 motion_buffer_->data.hasAccelerationIncludingGravityZ = true; | 128 motion_buffer_->data.has_acceleration_including_gravity_z = true; |
| 129 motion_buffer_->data.allAvailableSensorsAreActive = true; | 129 motion_buffer_->data.all_available_sensors_are_active = true; |
| 130 motion_buffer_->seqlock.WriteEnd(); | 130 motion_buffer_->seqlock.WriteEnd(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void SensorManagerChromeOS::GenerateOrientationEvent(double x, | 133 void SensorManagerChromeOS::GenerateOrientationEvent(double x, |
| 134 double y, | 134 double y, |
| 135 double z) { | 135 double z) { |
| 136 if (!orientation_buffer_) | 136 if (!orientation_buffer_) |
| 137 return; | 137 return; |
| 138 | 138 |
| 139 // Create a unit vector for trigonometry | 139 // Create a unit vector for trigonometry |
| 140 gfx::Vector3dF data(x, y, z); | 140 gfx::Vector3dF data(x, y, z); |
| 141 data.Scale(1.0f / data.Length()); | 141 data.Scale(1.0f / data.Length()); |
| 142 | 142 |
| 143 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | 143 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
| 144 // x = sin(gamma) | 144 // x = sin(gamma) |
| 145 // y = -cos(gamma) * sin(beta) | 145 // y = -cos(gamma) * sin(beta) |
| 146 // z = cos(beta) * cos(gamma) | 146 // z = cos(beta) * cos(gamma) |
| 147 // With only accelerometer alpha cannot be provided. | 147 // With only accelerometer alpha cannot be provided. |
| 148 double beta = kRad2deg * atan2(data.y(), data.z()); | 148 double beta = kRad2deg * atan2(data.y(), data.z()); |
| 149 double gamma = kRad2deg * asin(-data.x()); | 149 double gamma = kRad2deg * asin(-data.x()); |
| 150 | 150 |
| 151 // Convert beta and gamma to fit the intervals in the specification. Beta is | 151 // Convert beta and gamma to fit the intervals in the specification. Beta is |
| 152 // [-180, 180) and gamma is [-90, 90). | 152 // [-180, 180) and gamma is [-90, 90). |
| 153 if (beta >= 180.0f) | 153 if (beta >= 180.0f) |
| 154 beta = -180.0f; | 154 beta = -180.0f; |
| 155 if (gamma >= 90.0f) | 155 if (gamma >= 90.0f) |
| 156 gamma = -90.0f; | 156 gamma = -90.0f; |
| 157 orientation_buffer_->seqlock.WriteBegin(); | 157 orientation_buffer_->seqlock.WriteBegin(); |
| 158 orientation_buffer_->data.beta = beta; | 158 orientation_buffer_->data.beta = beta; |
| 159 orientation_buffer_->data.hasBeta = true; | 159 orientation_buffer_->data.has_beta = true; |
| 160 orientation_buffer_->data.gamma = gamma; | 160 orientation_buffer_->data.gamma = gamma; |
| 161 orientation_buffer_->data.hasGamma = true; | 161 orientation_buffer_->data.has_gamma = true; |
| 162 orientation_buffer_->data.allAvailableSensorsAreActive = true; | 162 orientation_buffer_->data.all_available_sensors_are_active = true; |
| 163 orientation_buffer_->seqlock.WriteEnd(); | 163 orientation_buffer_->seqlock.WriteEnd(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 } // namespace device | 166 } // namespace device |
| OLD | NEW |