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

Side by Side Diff: device/generic_sensor/platform_sensor_reader_win.cc

Issue 2476363003: [sensors][win] Implementation of motion sensors for Win platform (Closed)
Patch Set: Rebased to master Created 4 years, 1 month 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 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_reader_win.h" 5 #include "device/generic_sensor/platform_sensor_reader_win.h"
6 6
7 #include <Sensors.h> 7 #include <Sensors.h>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "base/win/iunknown_impl.h" 11 #include "base/win/iunknown_impl.h"
12 #include "device/generic_sensor/generic_sensor_consts.h"
12 #include "device/generic_sensor/public/cpp/platform_sensor_configuration.h" 13 #include "device/generic_sensor/public/cpp/platform_sensor_configuration.h"
13 #include "device/generic_sensor/public/cpp/sensor_reading.h" 14 #include "device/generic_sensor/public/cpp/sensor_reading.h"
14 15
15 namespace device { 16 namespace device {
16 17
17 // Init params for the PlatformSensorReaderWin. 18 // Init params for the PlatformSensorReaderWin.
18 struct ReaderInitParams { 19 struct ReaderInitParams {
19 // ISensorDataReport::GetSensorValue is not const, therefore, report 20 // ISensorDataReport::GetSensorValue is not const, therefore, report
20 // cannot be passed as const ref. 21 // cannot be passed as const ref.
21 // ISensorDataReport& report - report that contains new sensor data. 22 // ISensorDataReport& report - report that contains new sensor data.
22 // SensorReading& reading - out parameter that must be populated. 23 // SensorReading& reading - out parameter that must be populated.
23 // Returns HRESULT - S_OK on success, otherwise error code. 24 // Returns HRESULT - S_OK on success, otherwise error code.
24 using ReaderFunctor = base::Callback<HRESULT(ISensorDataReport& report, 25 using ReaderFunctor = base::Callback<HRESULT(ISensorDataReport& report,
25 SensorReading& reading)>; 26 SensorReading& reading)>;
26 SENSOR_TYPE_ID sensor_type_id; 27 SENSOR_TYPE_ID sensor_type_id;
27 mojom::ReportingMode reporting_mode;
28 ReaderFunctor reader_func; 28 ReaderFunctor reader_func;
29 unsigned long min_reporting_interval_ms = 0; 29 unsigned long min_reporting_interval_ms = 0;
30 }; 30 };
31 31
32 namespace { 32 namespace {
33 33
34 // Gets value from the report for provided key. 34 // Gets value from the report for provided key.
35 bool GetReadingValueForProperty(REFPROPERTYKEY key, 35 bool GetReadingValueForProperty(REFPROPERTYKEY key,
36 ISensorDataReport& report, 36 ISensorDataReport& report,
37 double* value) { 37 double* value) {
38 DCHECK(value); 38 DCHECK(value);
39 PROPVARIANT variant_value = {}; 39 PROPVARIANT variant_value = {};
40 if (SUCCEEDED(report.GetSensorValue(key, &variant_value))) { 40 if (SUCCEEDED(report.GetSensorValue(key, &variant_value))) {
41 if (variant_value.vt == VT_R8) 41 if (variant_value.vt == VT_R8)
42 *value = variant_value.dblVal; 42 *value = variant_value.dblVal;
43 else if (variant_value.vt == VT_R4) 43 else if (variant_value.vt == VT_R4)
44 *value = variant_value.fltVal; 44 *value = variant_value.fltVal;
45 else 45 else
46 return false; 46 return false;
47 return true; 47 return true;
48 } 48 }
49 49
50 *value = 0; 50 *value = 0;
51 return false; 51 return false;
52 } 52 }
53 53
54 // Ambient light sensor reader initialization parameters.
55 std::unique_ptr<ReaderInitParams> CreateAmbientLightReaderInitParams() {
56 auto params = base::MakeUnique<ReaderInitParams>();
57 params->sensor_type_id = SENSOR_TYPE_AMBIENT_LIGHT;
58 params->reader_func =
59 base::Bind([](ISensorDataReport& report, SensorReading& reading) {
60 double lux = 0.0;
61 if (!GetReadingValueForProperty(SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX,
62 report, &lux)) {
63 return E_FAIL;
64 }
65 reading.values[0] = lux;
66 return S_OK;
67 });
68 return params;
69 }
70
71 // Accelerometer sensor reader initialization parameters.
72 std::unique_ptr<ReaderInitParams> CreateAccelerometerReaderInitParams() {
73 auto params = base::MakeUnique<ReaderInitParams>();
74 params->sensor_type_id = SENSOR_TYPE_ACCELEROMETER_3D;
75 params->reader_func =
76 base::Bind([](ISensorDataReport& report, SensorReading& reading) {
77 double x = 0.0;
78 double y = 0.0;
79 double z = 0.0;
80 if (!GetReadingValueForProperty(SENSOR_DATA_TYPE_ACCELERATION_X_G,
81 report, &x) ||
82 !GetReadingValueForProperty(SENSOR_DATA_TYPE_ACCELERATION_Y_G,
83 report, &y) ||
84 !GetReadingValueForProperty(SENSOR_DATA_TYPE_ACCELERATION_Z_G,
85 report, &z)) {
86 return E_FAIL;
87 }
88
89 // Windows uses coordinate system where Z axis points down from device
90 // screen, therefore, using right hand notation, we have to reverse
91 // sign for each axis. Values are converted from G/s^2 to m/s^2.
92 reading.values[0] = -x * kMeanGravity;
93 reading.values[1] = -y * kMeanGravity;
94 reading.values[2] = -z * kMeanGravity;
95 return S_OK;
96 });
97 return params;
98 }
99
100 // Gyroscope sensor reader initialization parameters.
101 std::unique_ptr<ReaderInitParams> CreateGyroscopeReaderInitParams() {
102 auto params = base::MakeUnique<ReaderInitParams>();
103 params->sensor_type_id = SENSOR_TYPE_GYROMETER_3D;
104 params->reader_func = base::Bind([](ISensorDataReport& report,
105 SensorReading& reading) {
106 double x = 0.0;
107 double y = 0.0;
108 double z = 0.0;
109 if (!GetReadingValueForProperty(
110 SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_X_DEGREES_PER_SECOND_SQUARED,
111 report, &x) ||
112 !GetReadingValueForProperty(
113 SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_Y_DEGREES_PER_SECOND_SQUARED,
114 report, &y) ||
115 !GetReadingValueForProperty(
116 SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_Z_DEGREES_PER_SECOND_SQUARED,
117 report, &z)) {
118 return E_FAIL;
119 }
120
121 // Windows uses coordinate system where Z axis points down from device
122 // screen, therefore, using right hand notation, we have to reverse
123 // sign for each axis. Values are converted from deg/s^2 to rad/s^2.
124 reading.values[0] = -x * kRadiansInDegreesPerSecond;
125 reading.values[1] = -y * kRadiansInDegreesPerSecond;
126 reading.values[2] = -z * kRadiansInDegreesPerSecond;
127 return S_OK;
128 });
129 return params;
130 }
131
132 // Magnetometer sensor reader initialization parameters.
133 std::unique_ptr<ReaderInitParams> CreateMagnetometerReaderInitParams() {
134 auto params = base::MakeUnique<ReaderInitParams>();
135 params->sensor_type_id = SENSOR_TYPE_COMPASS_3D;
136 params->reader_func =
137 base::Bind([](ISensorDataReport& report, SensorReading& reading) {
138 double x = 0.0;
139 double y = 0.0;
140 double z = 0.0;
141 if (!GetReadingValueForProperty(
142 SENSOR_DATA_TYPE_MAGNETIC_FIELD_STRENGTH_X_MILLIGAUSS, report,
143 &x) ||
144 !GetReadingValueForProperty(
145 SENSOR_DATA_TYPE_MAGNETIC_FIELD_STRENGTH_Y_MILLIGAUSS, report,
146 &y) ||
147 !GetReadingValueForProperty(
148 SENSOR_DATA_TYPE_MAGNETIC_FIELD_STRENGTH_Z_MILLIGAUSS, report,
149 &z)) {
150 return E_FAIL;
151 }
152
153 // Windows uses coordinate system where Z axis points down from device
154 // screen, therefore, using right hand notation, we have to reverse
155 // sign for each axis. Values are converted from Milligaus to
156 // Microtesla.
157 reading.values[0] = -x * kMicroteslaInMilligauss;
158 reading.values[1] = -y * kMicroteslaInMilligauss;
159 reading.values[2] = -z * kMicroteslaInMilligauss;
160 return S_OK;
161 });
162 return params;
163 }
164
54 // Creates ReaderInitParams params structure. To implement support for new 165 // Creates ReaderInitParams params structure. To implement support for new
55 // sensor types, new switch case should be added and appropriate fields must 166 // sensor types, new switch case should be added and appropriate fields must
56 // be set: 167 // be set:
57 // sensor_type_id - GUID of the sensor supported by Windows. 168 // sensor_type_id - GUID of the sensor supported by Windows.
58 // reporting_mode - mode of reporting (ON_CHANGE | CONTINUOUS).
59 // reader_func - Functor that is responsible to populate SensorReading from 169 // reader_func - Functor that is responsible to populate SensorReading from
60 // ISensorDataReport data. 170 // ISensorDataReport data.
61 std::unique_ptr<ReaderInitParams> CreateReaderInitParamsForSensor( 171 std::unique_ptr<ReaderInitParams> CreateReaderInitParamsForSensor(
62 mojom::SensorType type) { 172 mojom::SensorType type) {
63 auto params = std::make_unique<ReaderInitParams>();
64 switch (type) { 173 switch (type) {
65 case mojom::SensorType::AMBIENT_LIGHT: { 174 case mojom::SensorType::AMBIENT_LIGHT:
66 params->sensor_type_id = SENSOR_TYPE_AMBIENT_LIGHT; 175 return CreateAmbientLightReaderInitParams();
67 params->reporting_mode = mojom::ReportingMode::ON_CHANGE; 176 case mojom::SensorType::ACCELEROMETER:
68 params->reader_func = 177 return CreateAccelerometerReaderInitParams();
69 base::Bind([](ISensorDataReport& report, SensorReading& reading) { 178 case mojom::SensorType::GYROSCOPE:
70 double lux = 0.0; 179 return CreateGyroscopeReaderInitParams();
71 if (!GetReadingValueForProperty(SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX, 180 case mojom::SensorType::MAGNETOMETER:
72 report, &lux)) { 181 return CreateMagnetometerReaderInitParams();
73 return E_FAIL;
74 }
75 reading.values[0] = lux;
76 return S_OK;
77 });
78 return params;
79 }
80 default: 182 default:
81 NOTIMPLEMENTED(); 183 NOTIMPLEMENTED();
82 return nullptr; 184 return nullptr;
83 } 185 }
84 } 186 }
85 187
86 } // namespace 188 } // namespace
87 189
88 // Class that implements ISensorEvents and IUnknown interfaces and used 190 // Class that implements ISensorEvents and IUnknown interfaces and used
89 // by ISensor interface to dispatch state and data change events. 191 // by ISensor interface to dispatch state and data change events.
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 426
325 void PlatformSensorReaderWin::SensorError() { 427 void PlatformSensorReaderWin::SensorError() {
326 if (client_) 428 if (client_)
327 client_->OnSensorError(); 429 client_->OnSensorError();
328 } 430 }
329 431
330 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const { 432 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const {
331 return init_params_->min_reporting_interval_ms; 433 return init_params_->min_reporting_interval_ms;
332 } 434 }
333 435
334 mojom::ReportingMode PlatformSensorReaderWin::GetReportingMode() const {
335 return init_params_->reporting_mode;
336 }
337
338 } // namespace device 436 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor_reader_win.h ('k') | device/generic_sensor/platform_sensor_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698