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

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: Update commit message Created 4 years, 2 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
« no previous file with comments | « content/browser/device_sensors/DEPS ('k') | content/renderer/DEPS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/browser/device_sensors/ambient_light_mac.h"
13 #include "device/sensors/public/cpp/device_util_mac.h"
13 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" 14 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h"
14 15
15 namespace { 16 namespace {
16 17
17 const double kMeanGravity = 9.80665; 18 const double kMeanGravity = 9.80665;
18 19
19 double LMUvalueToLux(uint64_t raw_value) {
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, 20 void FetchLight(content::AmbientLightSensor* sensor,
41 content::DeviceLightHardwareBuffer* buffer) { 21 content::DeviceLightHardwareBuffer* buffer) {
42 DCHECK(sensor); 22 DCHECK(sensor);
43 DCHECK(buffer); 23 DCHECK(buffer);
44 // Macbook pro has 2 lux values, left and right, we take the average. 24 // 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) 25 // The raw sensor values are converted to lux using LMUvalueToLux(raw_value)
46 // similar to how it is done in Firefox. 26 // similar to how it is done in Firefox.
47 uint64_t lux_value[2]; 27 uint64_t lux_value[2];
48 if (!sensor->ReadSensorValue(lux_value)) 28 if (!sensor->ReadSensorValue(lux_value))
49 return; 29 return;
50 uint64_t mean = (lux_value[0] + lux_value[1]) / 2; 30 uint64_t mean = (lux_value[0] + lux_value[1]) / 2;
51 double lux = LMUvalueToLux(mean); 31 double lux = device::LMUvalueToLux(mean);
52 buffer->seqlock.WriteBegin(); 32 buffer->seqlock.WriteBegin();
53 buffer->data.value = lux; 33 buffer->data.value = lux;
54 buffer->seqlock.WriteEnd(); 34 buffer->seqlock.WriteEnd();
55 } 35 }
56 36
57 void FetchMotion(SuddenMotionSensor* sensor, 37 void FetchMotion(SuddenMotionSensor* sensor,
58 content::DeviceMotionHardwareBuffer* buffer) { 38 content::DeviceMotionHardwareBuffer* buffer) {
59 DCHECK(sensor); 39 DCHECK(sensor);
60 DCHECK(buffer); 40 DCHECK(buffer);
61 41
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 light_buffer_ = nullptr; 251 light_buffer_ = nullptr;
272 } 252 }
273 return true; 253 return true;
274 default: 254 default:
275 NOTREACHED(); 255 NOTREACHED();
276 } 256 }
277 return false; 257 return false;
278 } 258 }
279 259
280 } // namespace content 260 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/device_sensors/DEPS ('k') | content/renderer/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698