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

Side by Side Diff: device/generic_sensor/ambient_light_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 2015 The Chromium Authors. All rights reserved. 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 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 // This file is based on http://osxbook.com/book/bonus/chapter10/light/ 5 // This file is based on http://osxbook.com/book/bonus/chapter10/light/
6 6
7 #include "content/browser/device_sensors/ambient_light_mac.h" 7 #include "device/generic_sensor/ambient_light_mac.h"
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/mac/scoped_cftyperef.h" 11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/mac/scoped_ioobject.h" 12 #include "base/mac/scoped_ioobject.h"
13 13
14 namespace content { 14 namespace device {
15 15
16 namespace { 16 namespace {
17 enum LmuFunctionIndex { 17 enum LmuFunctionIndex {
18 kGetSensorReadingID = 0, // getSensorReading(int *, int *) 18 kGetSensorReadingID = 0, // getSensorReading(int *, int *)
19 kGetLEDBrightnessID = 1, // getLEDBrightness(int, int *) 19 kGetLEDBrightnessID = 1, // getLEDBrightness(int, int *)
20 kSetLEDBrightnessID = 2, // setLEDBrightness(int, int, int *) 20 kSetLEDBrightnessID = 2, // setLEDBrightness(int, int, int *)
21 kSetLEDFadeID = 3, // setLEDFade(int, int, int, int *) 21 kSetLEDFadeID = 3, // setLEDFade(int, int, int, int *)
22 }; 22 };
23 } // namespace 23 } // namespace
24 24
25 // static 25 // static
26 std::unique_ptr<AmbientLightSensor> AmbientLightSensor::Create() { 26 std::unique_ptr<AmbientLightSensor> AmbientLightSensor::Create() {
27 std::unique_ptr<AmbientLightSensor> light_sensor(new AmbientLightSensor); 27 std::unique_ptr<AmbientLightSensor> light_sensor(new AmbientLightSensor);
28 return light_sensor->Init() ? std::move(light_sensor) : nullptr; 28 return light_sensor->Init() ? std::move(light_sensor) : nullptr;
29 } 29 }
30 30
31 double AmbientLightSensor::LMUvalueToLux(uint64_t raw_value) {
32 // Conversion formula from regression.
33 // https://bugzilla.mozilla.org/show_bug.cgi?id=793728
34 // Let x = raw_value, then
35 // lux = -2.978303814*(10^-27)*x^4 + 2.635687683*(10^-19)*x^3 -
36 // 3.459747434*(10^-12)*x^2 + 3.905829689*(10^-5)*x - 0.1932594532
37
38 static const long double k4 = pow(10.L, -7);
39 static const long double k3 = pow(10.L, -4);
40 static const long double k2 = pow(10.L, -2);
41 static const long double k1 = pow(10.L, 5);
42 long double scaled_value = raw_value / k1;
43
44 long double lux_value =
45 (-3 * k4 * pow(scaled_value, 4)) + (2.6 * k3 * pow(scaled_value, 3)) +
46 (-3.4 * k2 * pow(scaled_value, 2)) + (3.9 * scaled_value) - 0.19;
47
48 double lux = ceil(static_cast<double>(lux_value));
49 return lux > 0 ? lux : 0;
50 }
51
31 AmbientLightSensor::~AmbientLightSensor() { 52 AmbientLightSensor::~AmbientLightSensor() {
32 if (!io_connection_) 53 if (!io_connection_)
33 IOServiceClose(io_connection_); 54 IOServiceClose(io_connection_);
34 } 55 }
35 56
36 AmbientLightSensor::AmbientLightSensor() : io_connection_(IO_OBJECT_NULL) { 57 AmbientLightSensor::AmbientLightSensor() : io_connection_(IO_OBJECT_NULL) {}
37 }
38 58
39 bool AmbientLightSensor::Init() { 59 bool AmbientLightSensor::Init() {
40 // Tested and verified by riju that the following call works on 60 // Tested and verified by riju that the following call works on
41 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) 61 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model)
42 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). 62 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model).
43 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). 63 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model).
44 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). 64 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort).
45 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). 65 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model).
46 // Testing plans : please download the code and follow the comments :- 66 // Testing plans : please download the code and follow the comments :-
47 // https://gist.github.com/riju/74af8c81a665e412d122/ 67 // https://gist.github.com/riju/74af8c81a665e412d122/
(...skipping 22 matching lines...) Expand all
70 90
71 bool AmbientLightSensor::ReadSensorValue(uint64_t lux_values[2]) { 91 bool AmbientLightSensor::ReadSensorValue(uint64_t lux_values[2]) {
72 uint32_t scalar_output_count = 2; 92 uint32_t scalar_output_count = 2;
73 kern_return_t kr = IOConnectCallMethod( 93 kern_return_t kr = IOConnectCallMethod(
74 io_connection_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0, 94 io_connection_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0,
75 nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); 95 nullptr, 0, lux_values, &scalar_output_count, nullptr, 0);
76 96
77 return kr == KERN_SUCCESS; 97 return kr == KERN_SUCCESS;
78 } 98 }
79 99
80 } // namespace content 100 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698