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.
5 #include "content/browser/device_sensors/sensor_manager_chromeos.h"
9 #include "chromeos/accelerometer/accelerometer_reader.h"
10 #include "chromeos/accelerometer/accelerometer_types.h"
11 #include "content/browser/device_sensors/inertial_sensor_consts.h"
12 #include "ui/gfx/geometry/vector3d_f.h"
15 // Conversion ratio from radians to degrees.
16 const double kRad2deg
= 180.0 / M_PI
;
21 SensorManagerChromeOS::SensorManagerChromeOS()
22 : motion_buffer_(nullptr), orientation_buffer_(nullptr) {
25 SensorManagerChromeOS::~SensorManagerChromeOS() {
28 void SensorManagerChromeOS::StartFetchingDeviceMotionData(
29 DeviceMotionHardwareBuffer
* buffer
) {
30 DCHECK(thread_checker_
.CalledOnValidThread());
31 DCHECK(!motion_buffer_
);
32 motion_buffer_
= buffer
;
34 motion_buffer_
->seqlock
.WriteBegin();
35 // The interval between updates is the longer of the rate set on the buffer,
36 // and the rate at which AccelerometerReader polls the sensor.
37 motion_buffer_
->data
.interval
=
38 std::max(kInertialSensorIntervalMicroseconds
/ 1000,
39 chromeos::AccelerometerReader::kDelayBetweenReadsMs
);
40 motion_buffer_
->seqlock
.WriteEnd();
42 if (!orientation_buffer_
)
43 StartObservingAccelerometer();
46 bool SensorManagerChromeOS::StopFetchingDeviceMotionData() {
47 DCHECK(thread_checker_
.CalledOnValidThread());
51 // Make sure to indicate that the sensor data is no longer available.
52 motion_buffer_
->seqlock
.WriteBegin();
53 motion_buffer_
->data
.allAvailableSensorsAreActive
= false;
54 motion_buffer_
->seqlock
.WriteEnd();
56 motion_buffer_
= nullptr;
58 if (!orientation_buffer_
)
59 StopObservingAccelerometer();
63 void SensorManagerChromeOS::StartFetchingDeviceOrientationData(
64 DeviceOrientationHardwareBuffer
* buffer
) {
65 DCHECK(thread_checker_
.CalledOnValidThread());
66 DCHECK(!orientation_buffer_
);
67 orientation_buffer_
= buffer
;
69 // No compass information, so we cannot provide absolute orientation.
70 orientation_buffer_
->seqlock
.WriteBegin();
71 orientation_buffer_
->data
.absolute
= false;
72 orientation_buffer_
->data
.hasAbsolute
= true;
73 orientation_buffer_
->seqlock
.WriteEnd();
76 StartObservingAccelerometer();
79 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() {
80 DCHECK(thread_checker_
.CalledOnValidThread());
81 if (!orientation_buffer_
)
83 // Make sure to indicate that the sensor data is no longer available.
84 orientation_buffer_
->seqlock
.WriteBegin();
85 orientation_buffer_
->data
.allAvailableSensorsAreActive
= false;
86 orientation_buffer_
->seqlock
.WriteEnd();
87 orientation_buffer_
= nullptr;
90 StopObservingAccelerometer();
94 void SensorManagerChromeOS::OnAccelerometerUpdated(
95 scoped_refptr
<const chromeos::AccelerometerUpdate
> update
) {
96 DCHECK(thread_checker_
.CalledOnValidThread());
97 chromeos::AccelerometerSource source
;
98 if (update
->has(chromeos::ACCELEROMETER_SOURCE_SCREEN
))
99 source
= chromeos::ACCELEROMETER_SOURCE_SCREEN
;
100 else if (update
->has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD
))
101 source
= chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD
;
105 double x
= update
->get(source
).x
;
106 double y
= update
->get(source
).y
;
107 double z
= update
->get(source
).z
;
109 GenerateMotionEvent(x
, y
, z
);
110 GenerateOrientationEvent(x
, y
, z
);
113 void SensorManagerChromeOS::StartObservingAccelerometer() {
114 chromeos::AccelerometerReader::GetInstance()->AddObserver(this);
117 void SensorManagerChromeOS::StopObservingAccelerometer() {
118 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this);
121 void SensorManagerChromeOS::GenerateMotionEvent(double x
, double y
, double z
) {
125 motion_buffer_
->seqlock
.WriteBegin();
126 motion_buffer_
->data
.accelerationIncludingGravityX
= x
;
127 motion_buffer_
->data
.hasAccelerationIncludingGravityX
= true;
128 motion_buffer_
->data
.accelerationIncludingGravityY
= y
;
129 motion_buffer_
->data
.hasAccelerationIncludingGravityY
= true;
130 motion_buffer_
->data
.accelerationIncludingGravityZ
= z
;
131 motion_buffer_
->data
.hasAccelerationIncludingGravityZ
= true;
132 motion_buffer_
->data
.allAvailableSensorsAreActive
= true;
133 motion_buffer_
->seqlock
.WriteEnd();
136 void SensorManagerChromeOS::GenerateOrientationEvent(double x
,
139 if (!orientation_buffer_
)
142 // Create a unit vector for trigonometry
143 gfx::Vector3dF
data(x
, y
, z
);
144 data
.Scale(1.0f
/ data
.Length());
146 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix.
148 // y = -cos(gamma) * sin(beta)
149 // z = cos(beta) * cos(gamma)
150 // With only accelerometer alpha cannot be provided.
151 double beta
= kRad2deg
* atan2(data
.y(), data
.z());
152 double gamma
= kRad2deg
* asin(-data
.x());
154 // Convert beta and gamma to fit the intervals in the specification. Beta is
155 // [-180, 180) and gamma is [-90, 90).
160 orientation_buffer_
->seqlock
.WriteBegin();
161 orientation_buffer_
->data
.beta
= beta
;
162 orientation_buffer_
->data
.hasBeta
= true;
163 orientation_buffer_
->data
.gamma
= gamma
;
164 orientation_buffer_
->data
.hasGamma
= true;
165 orientation_buffer_
->data
.allAvailableSensorsAreActive
= true;
166 orientation_buffer_
->seqlock
.WriteEnd();
169 } // namespace content