OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "device/generic_sensor/platform_sensor_ambient_light_mac.h" | 5 #include "device/generic_sensor/platform_sensor_ambient_light_mac.h" |
6 | 6 |
7 #include <IOKit/IOMessage.h> | 7 #include <IOKit/IOMessage.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "device/base/synchronization/shared_memory_seqlock_buffer.h" | 10 #include "device/base/synchronization/shared_memory_seqlock_buffer.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz); | 47 default_configuration.set_frequency(kDefaultAmbientLightFrequencyHz); |
48 return default_configuration; | 48 return default_configuration; |
49 } | 49 } |
50 | 50 |
51 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context, | 51 void PlatformSensorAmbientLightMac::IOServiceCallback(void* context, |
52 io_service_t service, | 52 io_service_t service, |
53 natural_t message_type, | 53 natural_t message_type, |
54 void* message_argument) { | 54 void* message_argument) { |
55 PlatformSensorAmbientLightMac* sensor = | 55 PlatformSensorAmbientLightMac* sensor = |
56 static_cast<PlatformSensorAmbientLightMac*>(context); | 56 static_cast<PlatformSensorAmbientLightMac*>(context); |
57 uint32_t scalar_output_count = 2; | 57 sensor->ReadSensorValue(); |
timvolodine
2016/10/18 19:26:13
would it make sense to make ReadSensorValue return
darktears
2016/10/18 19:56:58
The idea here was that at some point we will need
| |
58 uint64_t lux_values[2]; | |
59 kern_return_t kr = IOConnectCallMethod( | |
60 sensor->light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, | |
61 nullptr, 0, nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); | |
62 | |
63 if (kr == KERN_SUCCESS) | |
64 sensor->UpdateReading(lux_values); | |
65 } | 58 } |
66 | 59 |
67 bool PlatformSensorAmbientLightMac::StartSensor( | 60 bool PlatformSensorAmbientLightMac::StartSensor( |
68 const PlatformSensorConfiguration& configuration) { | 61 const PlatformSensorConfiguration& configuration) { |
69 // Tested and verified by riju that the following call works on | 62 // Tested and verified by riju that the following call works on |
70 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) | 63 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model) |
71 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). | 64 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model). |
72 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). | 65 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model). |
73 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). | 66 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort). |
74 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). | 67 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model). |
(...skipping 18 matching lines...) Expand all Loading... | |
93 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); | 86 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); |
94 | 87 |
95 kern_return_t kr = IOServiceAddInterestNotification( | 88 kern_return_t kr = IOServiceAddInterestNotification( |
96 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest, | 89 light_sensor_port_.get(), light_sensor_service_, kIOGeneralInterest, |
97 IOServiceCallback, this, light_sensor_notification_.InitializeInto()); | 90 IOServiceCallback, this, light_sensor_notification_.InitializeInto()); |
98 if (kr != KERN_SUCCESS) | 91 if (kr != KERN_SUCCESS) |
99 return false; | 92 return false; |
100 | 93 |
101 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, | 94 kr = IOServiceOpen(light_sensor_service_, mach_task_self(), 0, |
102 light_sensor_object_.InitializeInto()); | 95 light_sensor_object_.InitializeInto()); |
96 if (kr != KERN_SUCCESS) | |
97 return false; | |
103 | 98 |
104 return kr == KERN_SUCCESS; | 99 return ReadSensorValue(); |
timvolodine
2016/10/18 19:26:13
is it possible that ReadSensorValue returns false
darktears
2016/10/18 19:56:58
Yes then we can clean it up.
| |
105 } | 100 } |
106 | 101 |
107 void PlatformSensorAmbientLightMac::StopSensor() { | 102 void PlatformSensorAmbientLightMac::StopSensor() { |
108 light_sensor_port_.reset(); | 103 light_sensor_port_.reset(); |
109 light_sensor_notification_.reset(); | 104 light_sensor_notification_.reset(); |
110 light_sensor_object_.reset(); | 105 light_sensor_object_.reset(); |
111 current_lux_ = 0.0; | 106 current_lux_ = 0.0; |
112 } | 107 } |
113 | 108 |
114 void PlatformSensorAmbientLightMac::UpdateReading(uint64_t lux_values[2]) { | 109 bool PlatformSensorAmbientLightMac::ReadSensorValue() { |
timvolodine
2016/10/18 19:26:13
maybe better name something like: ReadAndUpdate..
darktears
2016/10/18 19:56:58
I will change.
| |
110 uint32_t scalar_output_count = 2; | |
111 uint64_t lux_values[2]; | |
112 kern_return_t kr = IOConnectCallMethod( | |
113 light_sensor_object_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0, | |
114 nullptr, 0, lux_values, &scalar_output_count, nullptr, 0); | |
115 | |
116 if (kr != KERN_SUCCESS) | |
117 return false; | |
118 | |
115 uint64_t mean = (lux_values[0] + lux_values[1]) / 2; | 119 uint64_t mean = (lux_values[0] + lux_values[1]) / 2; |
116 double lux = LMUvalueToLux(mean); | 120 double lux = LMUvalueToLux(mean); |
117 if (lux == current_lux_) | 121 if (lux == current_lux_) |
118 return; | 122 return true; |
119 current_lux_ = lux; | 123 current_lux_ = lux; |
120 | 124 |
121 SensorReading reading; | 125 SensorReading reading; |
122 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | 126 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
123 reading.values[0] = current_lux_; | 127 reading.values[0] = current_lux_; |
124 UpdateSensorReading(reading, true); | 128 UpdateSensorReading(reading, true); |
129 return true; | |
125 } | 130 } |
126 | 131 |
127 } // namespace device | 132 } // namespace device |
OLD | NEW |