OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "chromeos/accelerometer/accelerometer_reader.h" | |
9 #include "chromeos/accelerometer/accelerometer_types.h" | |
10 #include "ui/gfx/geometry/vector3d_f.h" | |
11 | |
12 namespace { | |
13 // Conversion ratio from radians to degrees. | |
14 const double kRad2deg = 180.0 / M_PI; | |
15 } | |
16 | |
17 namespace content { | |
18 | |
19 SensorManagerChromeOS::SensorManagerChromeOS() : orientation_buffer_(nullptr) { | |
20 } | |
21 | |
22 SensorManagerChromeOS::~SensorManagerChromeOS() { | |
23 } | |
24 | |
25 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | |
26 DeviceOrientationHardwareBuffer* buffer) { | |
27 DCHECK(buffer); | |
28 { | |
29 base::AutoLock autolock(orientation_buffer_lock_); | |
30 if (orientation_buffer_) | |
31 return false; | |
32 orientation_buffer_ = buffer; | |
33 | |
34 // No compass information, so we cannot provide absolute orientation. | |
35 orientation_buffer_->seqlock.WriteBegin(); | |
36 orientation_buffer_->data.absolute = false; | |
37 orientation_buffer_->data.hasAbsolute = true; | |
38 orientation_buffer_->seqlock.WriteEnd(); | |
39 } | |
40 | |
41 StartObservingAccelerometer(); | |
42 return true; | |
43 } | |
44 | |
45 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | |
46 { | |
47 base::AutoLock autolock(orientation_buffer_lock_); | |
48 if (!orientation_buffer_) | |
49 return false; | |
50 // Make sure to indicate that the sensor data is no longer available. | |
51 orientation_buffer_->seqlock.WriteBegin(); | |
52 orientation_buffer_->data.allAvailableSensorsAreActive = false; | |
53 orientation_buffer_->seqlock.WriteEnd(); | |
54 orientation_buffer_ = nullptr; | |
55 } | |
56 | |
57 StopObservingAccelerometer(); | |
58 return true; | |
59 } | |
60 | |
61 void SensorManagerChromeOS::OnAccelerometerUpdated( | |
62 const chromeos::AccelerometerUpdate& update) { | |
63 base::AutoLock autolock(orientation_buffer_lock_); | |
64 if (!orientation_buffer_) | |
65 return; | |
66 | |
67 chromeos::AccelerometerSource source; | |
68 if (update.has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) { | |
69 source = chromeos::ACCELEROMETER_SOURCE_SCREEN; | |
70 } else if (update.has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) { | |
71 source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
72 } else { | |
73 return; | |
74 } | |
75 | |
76 double x = update.get(source).x; | |
77 double y = update.get(source).y; | |
78 double z = update.get(source).z; | |
79 | |
80 // Create a unit vector for trigonometry | |
81 // TODO(jonross): Stop reversing signs for vector components once | |
82 // accelerometer values have been fixed. crbug.com/431391 | |
83 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | |
84 // results. | |
85 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | |
86 data.Scale(1.0f / data.Length()); | |
87 | |
88 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | |
89 // x = sin(gamma) | |
90 // y = -cos(gamma) * sin(beta) | |
91 // z = cos(beta) * cos(gamma) | |
92 // With only accelerometer alpha cannot be provided. | |
93 double beta = kRad2deg * atan2(data.y(), data.z()); | |
94 double gamma = kRad2deg * asin(data.x()); | |
95 | |
96 // Convert beta and gamma to fit the intervals in the specification. Beta is | |
97 // [-180, 180) and gamma is [-90, 90). | |
98 if (beta >= 180.0f) | |
99 beta = -180.0f; | |
100 if (gamma >= 90.0f) | |
101 gamma = -90.0f; | |
102 orientation_buffer_->seqlock.WriteBegin(); | |
103 orientation_buffer_->data.beta = beta; | |
104 orientation_buffer_->data.hasBeta = true; | |
105 orientation_buffer_->data.gamma = gamma; | |
106 orientation_buffer_->data.hasGamma = true; | |
107 orientation_buffer_->data.allAvailableSensorsAreActive = true; | |
108 orientation_buffer_->seqlock.WriteEnd(); | |
109 } | |
110 | |
111 void SensorManagerChromeOS::StartObservingAccelerometer() { | |
112 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
113 } | |
114 | |
115 void SensorManagerChromeOS::StopObservingAccelerometer() { | |
116 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
117 } | |
118 | |
119 } // namespace content | |
OLD | NEW |