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

Side by Side Diff: content/browser/device_sensors/data_fetcher_shared_memory_mac.cc

Issue 2332903002: [sensors] [mac] Implement ambient light sensor for macOS (Closed)
Patch Set: Fix comments from Mikhail Created 4 years, 3 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "content/browser/device_sensors/data_fetcher_shared_memory.h" 5 #include "content/browser/device_sensors/data_fetcher_shared_memory.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "content/browser/device_sensors/ambient_light_mac.h" 12 #include "device/generic_sensor/ambient_light_mac.h"
13 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" 13 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h"
14 14
15 namespace { 15 namespace {
16 16
17 const double kMeanGravity = 9.80665; 17 const double kMeanGravity = 9.80665;
18 18
19 double LMUvalueToLux(uint64_t raw_value) { 19 void FetchLight(device::AmbientLightSensor* sensor,
20 // Conversion formula from regression.
21 // https://bugzilla.mozilla.org/show_bug.cgi?id=793728
22 // Let x = raw_value, then
23 // lux = -2.978303814*(10^-27)*x^4 + 2.635687683*(10^-19)*x^3 -
24 // 3.459747434*(10^-12)*x^2 + 3.905829689*(10^-5)*x - 0.1932594532
25
26 static const long double k4 = pow(10.L, -7);
27 static const long double k3 = pow(10.L, -4);
28 static const long double k2 = pow(10.L, -2);
29 static const long double k1 = pow(10.L, 5);
30 long double scaled_value = raw_value / k1;
31
32 long double lux_value =
33 (-3 * k4 * pow(scaled_value, 4)) + (2.6 * k3 * pow(scaled_value, 3)) +
34 (-3.4 * k2 * pow(scaled_value, 2)) + (3.9 * scaled_value) - 0.19;
35
36 double lux = ceil(static_cast<double>(lux_value));
37 return lux > 0 ? lux : 0;
38 }
39
40 void FetchLight(content::AmbientLightSensor* sensor,
41 content::DeviceLightHardwareBuffer* buffer) { 20 content::DeviceLightHardwareBuffer* buffer) {
42 DCHECK(sensor); 21 DCHECK(sensor);
43 DCHECK(buffer); 22 DCHECK(buffer);
44 // Macbook pro has 2 lux values, left and right, we take the average. 23 // Macbook pro has 2 lux values, left and right, we take the average.
45 // The raw sensor values are converted to lux using LMUvalueToLux(raw_value) 24 // The raw sensor values are converted to lux using LMUvalueToLux(raw_value)
46 // similar to how it is done in Firefox. 25 // similar to how it is done in Firefox.
47 uint64_t lux_value[2]; 26 uint64_t lux_value[2];
48 if (!sensor->ReadSensorValue(lux_value)) 27 if (!sensor->ReadSensorValue(lux_value))
49 return; 28 return;
50 uint64_t mean = (lux_value[0] + lux_value[1]) / 2; 29 uint64_t mean = (lux_value[0] + lux_value[1]) / 2;
51 double lux = LMUvalueToLux(mean); 30 double lux = device::AmbientLightSensor::LMUvalueToLux(mean);
52 buffer->seqlock.WriteBegin(); 31 buffer->seqlock.WriteBegin();
53 buffer->data.value = lux; 32 buffer->data.value = lux;
54 buffer->seqlock.WriteEnd(); 33 buffer->seqlock.WriteEnd();
55 } 34 }
56 35
57 void FetchMotion(SuddenMotionSensor* sensor, 36 void FetchMotion(SuddenMotionSensor* sensor,
58 content::DeviceMotionHardwareBuffer* buffer) { 37 content::DeviceMotionHardwareBuffer* buffer) {
59 DCHECK(sensor); 38 DCHECK(sensor);
60 DCHECK(buffer); 39 DCHECK(buffer);
61 40
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 // Absolute device orientation not available on Mac, let the 189 // Absolute device orientation not available on Mac, let the
211 // implementation fire an all-null event to signal this to blink. 190 // implementation fire an all-null event to signal this to blink.
212 orientation_absolute_buffer_->seqlock.WriteBegin(); 191 orientation_absolute_buffer_->seqlock.WriteBegin();
213 orientation_absolute_buffer_->data.absolute = true; 192 orientation_absolute_buffer_->data.absolute = true;
214 orientation_absolute_buffer_->data.allAvailableSensorsAreActive = true; 193 orientation_absolute_buffer_->data.allAvailableSensorsAreActive = true;
215 orientation_absolute_buffer_->seqlock.WriteEnd(); 194 orientation_absolute_buffer_->seqlock.WriteEnd();
216 return false; 195 return false;
217 } 196 }
218 case CONSUMER_TYPE_LIGHT: { 197 case CONSUMER_TYPE_LIGHT: {
219 if (!ambient_light_sensor_) 198 if (!ambient_light_sensor_)
220 ambient_light_sensor_ = AmbientLightSensor::Create(); 199 ambient_light_sensor_ = device::AmbientLightSensor::Create();
221 bool ambient_light_sensor_available = 200 bool ambient_light_sensor_available =
222 ambient_light_sensor_.get() != nullptr; 201 ambient_light_sensor_.get() != nullptr;
223 202
224 light_buffer_ = static_cast<DeviceLightHardwareBuffer*>(buffer); 203 light_buffer_ = static_cast<DeviceLightHardwareBuffer*>(buffer);
225 if (!ambient_light_sensor_available) { 204 if (!ambient_light_sensor_available) {
226 light_buffer_->seqlock.WriteBegin(); 205 light_buffer_->seqlock.WriteBegin();
227 light_buffer_->data.value = std::numeric_limits<double>::infinity(); 206 light_buffer_->data.value = std::numeric_limits<double>::infinity();
228 light_buffer_->seqlock.WriteEnd(); 207 light_buffer_->seqlock.WriteEnd();
229 } 208 }
230 return ambient_light_sensor_available; 209 return ambient_light_sensor_available;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 light_buffer_ = nullptr; 250 light_buffer_ = nullptr;
272 } 251 }
273 return true; 252 return true;
274 default: 253 default:
275 NOTREACHED(); 254 NOTREACHED();
276 } 255 }
277 return false; 256 return false;
278 } 257 }
279 258
280 } // namespace content 259 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698