Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Side by Side Diff: device/sensors/data_fetcher_shared_memory_mac.cc

Issue 2819273006: Move //device/sensor impl to be part of the internal implemenation of the Device Service. (Closed)
Patch Set: Move //device/sensor impl to be part of the internal implemenation of the Device Service Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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/sensors/data_fetcher_shared_memory.h"
6
7 #include <stdint.h>
8
9 #include "base/logging.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/single_thread_task_runner.h"
12 #include "device/sensors/ambient_light_mac.h"
13 #include "device/sensors/public/cpp/device_util_mac.h"
14 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h"
15
16 namespace device {
17
18 const double kMeanGravity = 9.80665;
19
20 void FetchLight(AmbientLightSensor* sensor, DeviceLightHardwareBuffer* buffer) {
21 DCHECK(sensor);
22 DCHECK(buffer);
23 // Macbook pro has 2 lux values, left and right, we take the average.
24 // The raw sensor values are converted to lux using LMUvalueToLux(raw_value)
25 // similar to how it is done in Firefox.
26 uint64_t lux_value[2];
27 if (!sensor->ReadSensorValue(lux_value))
28 return;
29 uint64_t mean = (lux_value[0] + lux_value[1]) / 2;
30 double lux = LMUvalueToLux(mean);
31 buffer->seqlock.WriteBegin();
32 buffer->data.value = lux;
33 buffer->seqlock.WriteEnd();
34 }
35
36 void FetchMotion(SuddenMotionSensor* sensor,
37 DeviceMotionHardwareBuffer* buffer) {
38 DCHECK(sensor);
39 DCHECK(buffer);
40
41 float axis_value[3];
42 if (!sensor->ReadSensorValues(axis_value))
43 return;
44
45 buffer->seqlock.WriteBegin();
46 buffer->data.acceleration_including_gravity_x = axis_value[0] * kMeanGravity;
47 buffer->data.has_acceleration_including_gravity_x = true;
48 buffer->data.acceleration_including_gravity_y = axis_value[1] * kMeanGravity;
49 buffer->data.has_acceleration_including_gravity_y = true;
50 buffer->data.acceleration_including_gravity_z = axis_value[2] * kMeanGravity;
51 buffer->data.has_acceleration_including_gravity_z = true;
52 buffer->data.all_available_sensors_are_active = true;
53 buffer->seqlock.WriteEnd();
54 }
55
56 void FetchOrientation(SuddenMotionSensor* sensor,
57 DeviceOrientationHardwareBuffer* buffer) {
58 DCHECK(sensor);
59 DCHECK(buffer);
60
61 // Retrieve per-axis calibrated values.
62 float axis_value[3];
63 if (!sensor->ReadSensorValues(axis_value))
64 return;
65
66 // Transform the accelerometer values to W3C draft angles.
67 //
68 // Accelerometer values are just dot products of the sensor axes
69 // by the gravity vector 'g' with the result for the z axis inverted.
70 //
71 // To understand this transformation calculate the 3rd row of the z-x-y
72 // Euler angles rotation matrix (because of the 'g' vector, only 3rd row
73 // affects to the result). Note that z-x-y matrix means R = Ry * Rx * Rz.
74 // Then, assume alpha = 0 and you get this:
75 //
76 // x_acc = sin(gamma)
77 // y_acc = - cos(gamma) * sin(beta)
78 // z_acc = cos(beta) * cos(gamma)
79 //
80 // After that the rest is just a bit of trigonometry.
81 //
82 // Also note that alpha can't be provided but it's assumed to be always zero.
83 // This is necessary in order to provide enough information to solve
84 // the equations.
85 //
86 const double kRad2deg = 180.0 / M_PI;
87 double beta = kRad2deg * atan2(-axis_value[1], axis_value[2]);
88 double gamma = kRad2deg * asin(axis_value[0]);
89
90 // Make sure that the interval boundaries comply with the specification. At
91 // this point, beta is [-180, 180] and gamma is [-90, 90], but the spec has
92 // the upper bound open on both.
93 if (beta == 180.0)
94 beta = -180; // -180 == 180 (upside-down)
95 if (gamma == 90.0)
96 gamma = nextafter(90, 0);
97
98 // At this point, DCHECKing is paranoia. Never hurts.
99 DCHECK_GE(beta, -180.0);
100 DCHECK_LT(beta, 180.0);
101 DCHECK_GE(gamma, -90.0);
102 DCHECK_LT(gamma, 90.0);
103
104 buffer->seqlock.WriteBegin();
105 buffer->data.beta = beta;
106 buffer->data.has_beta = true;
107 buffer->data.gamma = gamma;
108 buffer->data.has_gamma = true;
109 buffer->data.all_available_sensors_are_active = true;
110 buffer->seqlock.WriteEnd();
111 }
112
113 DataFetcherSharedMemory::DataFetcherSharedMemory() {}
114
115 DataFetcherSharedMemory::~DataFetcherSharedMemory() {}
116
117 void DataFetcherSharedMemory::Fetch(unsigned consumer_bitmask) {
118 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread());
119 DCHECK(consumer_bitmask & CONSUMER_TYPE_ORIENTATION ||
120 consumer_bitmask & CONSUMER_TYPE_MOTION ||
121 consumer_bitmask & CONSUMER_TYPE_LIGHT);
122
123 if (consumer_bitmask & CONSUMER_TYPE_ORIENTATION)
124 FetchOrientation(sudden_motion_sensor_.get(), orientation_buffer_);
125 if (consumer_bitmask & CONSUMER_TYPE_MOTION)
126 FetchMotion(sudden_motion_sensor_.get(), motion_buffer_);
127 if (consumer_bitmask & CONSUMER_TYPE_LIGHT)
128 FetchLight(ambient_light_sensor_.get(), light_buffer_);
129 }
130
131 DataFetcherSharedMemory::FetcherType DataFetcherSharedMemory::GetType() const {
132 return FETCHER_TYPE_POLLING_CALLBACK;
133 }
134
135 bool DataFetcherSharedMemory::Start(ConsumerType consumer_type, void* buffer) {
136 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread());
137 DCHECK(buffer);
138
139 switch (consumer_type) {
140 case CONSUMER_TYPE_MOTION: {
141 if (!sudden_motion_sensor_)
142 sudden_motion_sensor_.reset(SuddenMotionSensor::Create());
143 bool sudden_motion_sensor_available =
144 sudden_motion_sensor_.get() != nullptr;
145
146 motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer);
147 UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionMacAvailable",
148 sudden_motion_sensor_available);
149 if (!sudden_motion_sensor_available) {
150 // No motion sensor available, fire an all-null event.
151 motion_buffer_->seqlock.WriteBegin();
152 motion_buffer_->data.all_available_sensors_are_active = true;
153 motion_buffer_->seqlock.WriteEnd();
154 }
155 return sudden_motion_sensor_available;
156 }
157 case CONSUMER_TYPE_ORIENTATION: {
158 if (!sudden_motion_sensor_)
159 sudden_motion_sensor_.reset(SuddenMotionSensor::Create());
160 bool sudden_motion_sensor_available =
161 sudden_motion_sensor_.get() != nullptr;
162
163 orientation_buffer_ =
164 static_cast<DeviceOrientationHardwareBuffer*>(buffer);
165 UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationMacAvailable",
166 sudden_motion_sensor_available);
167 if (sudden_motion_sensor_available) {
168 // On Mac we cannot provide absolute orientation.
169 orientation_buffer_->seqlock.WriteBegin();
170 orientation_buffer_->data.absolute = false;
171 orientation_buffer_->seqlock.WriteEnd();
172 } else {
173 // No motion sensor available, fire an all-null event.
174 orientation_buffer_->seqlock.WriteBegin();
175 orientation_buffer_->data.all_available_sensors_are_active = true;
176 orientation_buffer_->seqlock.WriteEnd();
177 }
178 return sudden_motion_sensor_available;
179 }
180 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: {
181 orientation_absolute_buffer_ =
182 static_cast<DeviceOrientationHardwareBuffer*>(buffer);
183 // Absolute device orientation not available on Mac, let the
184 // implementation fire an all-null event to signal this to blink.
185 orientation_absolute_buffer_->seqlock.WriteBegin();
186 orientation_absolute_buffer_->data.absolute = true;
187 orientation_absolute_buffer_->data.all_available_sensors_are_active =
188 true;
189 orientation_absolute_buffer_->seqlock.WriteEnd();
190 return false;
191 }
192 case CONSUMER_TYPE_LIGHT: {
193 if (!ambient_light_sensor_)
194 ambient_light_sensor_ = AmbientLightSensor::Create();
195 bool ambient_light_sensor_available =
196 ambient_light_sensor_.get() != nullptr;
197
198 light_buffer_ = static_cast<DeviceLightHardwareBuffer*>(buffer);
199 if (!ambient_light_sensor_available) {
200 light_buffer_->seqlock.WriteBegin();
201 light_buffer_->data.value = std::numeric_limits<double>::infinity();
202 light_buffer_->seqlock.WriteEnd();
203 }
204 return ambient_light_sensor_available;
205 }
206 default:
207 NOTREACHED();
208 }
209 return false;
210 }
211
212 bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) {
213 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread());
214
215 switch (consumer_type) {
216 case CONSUMER_TYPE_MOTION:
217 if (motion_buffer_) {
218 motion_buffer_->seqlock.WriteBegin();
219 motion_buffer_->data.all_available_sensors_are_active = false;
220 motion_buffer_->seqlock.WriteEnd();
221 motion_buffer_ = nullptr;
222 }
223 return true;
224 case CONSUMER_TYPE_ORIENTATION:
225 if (orientation_buffer_) {
226 orientation_buffer_->seqlock.WriteBegin();
227 orientation_buffer_->data.all_available_sensors_are_active = false;
228 orientation_buffer_->seqlock.WriteEnd();
229 orientation_buffer_ = nullptr;
230 }
231 return true;
232 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE:
233 if (orientation_absolute_buffer_) {
234 orientation_absolute_buffer_->seqlock.WriteBegin();
235 orientation_absolute_buffer_->data.all_available_sensors_are_active =
236 false;
237 orientation_absolute_buffer_->seqlock.WriteEnd();
238 orientation_absolute_buffer_ = nullptr;
239 }
240 return true;
241 case CONSUMER_TYPE_LIGHT:
242 if (light_buffer_) {
243 light_buffer_->seqlock.WriteBegin();
244 light_buffer_->data.value = -1;
245 light_buffer_->seqlock.WriteEnd();
246 light_buffer_ = nullptr;
247 }
248 return true;
249 default:
250 NOTREACHED();
251 }
252 return false;
253 }
254
255 } // namespace device
OLDNEW
« no previous file with comments | « device/sensors/data_fetcher_shared_memory_default.cc ('k') | device/sensors/data_fetcher_shared_memory_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698