Chromium Code Reviews| Index: device/generic_sensor/platform_sensor_reader_win.cc |
| diff --git a/device/generic_sensor/platform_sensor_reader_win.cc b/device/generic_sensor/platform_sensor_reader_win.cc |
| index 7f292b1fb2a197de00216dad2075d5a865c44c2b..3ef57a743cfb9a31ff01867ddc1897bd9556b4c5 100644 |
| --- a/device/generic_sensor/platform_sensor_reader_win.cc |
| +++ b/device/generic_sensor/platform_sensor_reader_win.cc |
| @@ -31,6 +31,13 @@ struct ReaderInitParams { |
| namespace { |
| +// Required to for conversion from G/s^2 to m/s^2 |
| +constexpr double kMeanGravity = 9.80665; |
| +// Required to for conversion from deg/s^2 to rad/s^2 |
| +constexpr double kRadiansInDegreesPerSecond = 0.0174533; |
| +// Required to for conversion Milligauss to Microtesla |
| +constexpr double kMicroteslaInMilligauss = 0.1; |
| + |
| // Gets value from the report for provided key. |
| bool GetReadingValueForProperty(REFPROPERTYKEY key, |
| ISensorDataReport& report, |
| @@ -51,6 +58,117 @@ bool GetReadingValueForProperty(REFPROPERTYKEY key, |
| return false; |
| } |
| +// Ambient light sensor reader initialization parameters. |
| +std::unique_ptr<ReaderInitParams> CreateAmbientLightReaderInitParams() { |
| + auto params = base::MakeUnique<ReaderInitParams>(); |
| + params->sensor_type_id = SENSOR_TYPE_AMBIENT_LIGHT; |
| + params->reporting_mode = mojom::ReportingMode::ON_CHANGE; |
| + params->reader_func = |
| + base::Bind([](ISensorDataReport& report, SensorReading& reading) { |
| + double lux = 0.0; |
| + if (!GetReadingValueForProperty(SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX, |
| + report, &lux)) { |
| + return E_FAIL; |
| + } |
| + reading.values[0] = lux; |
| + return S_OK; |
| + }); |
| + return params; |
| +} |
| + |
| +// Accelerometer sensor reader initialization parameters. |
| +std::unique_ptr<ReaderInitParams> CreateAccelerometerReaderInitParams() { |
| + auto params = base::MakeUnique<ReaderInitParams>(); |
| + params->sensor_type_id = SENSOR_TYPE_ACCELEROMETER_3D; |
| + params->reporting_mode = mojom::ReportingMode::CONTINUOUS; |
|
Reilly Grant (use Gerrit)
2016/11/09 17:11:28
Is this really continuous? Last time I played with
shalamov
2016/11/10 13:38:10
You are right, I modified code accordingly.
I was
|
| + params->reader_func = |
| + base::Bind([](ISensorDataReport& report, SensorReading& reading) { |
| + double x = 0.0; |
| + double y = 0.0; |
| + double z = 0.0; |
| + if (!GetReadingValueForProperty(SENSOR_DATA_TYPE_ACCELERATION_X_G, |
| + report, &x) || |
| + !GetReadingValueForProperty(SENSOR_DATA_TYPE_ACCELERATION_Y_G, |
| + report, &y) || |
| + !GetReadingValueForProperty(SENSOR_DATA_TYPE_ACCELERATION_Z_G, |
| + report, &z)) { |
| + return E_FAIL; |
| + } |
| + |
| + // Windows uses coordinate system where Z axis points down from device |
| + // screen, therefore, using right hand notation, we have to reverse |
| + // sign for each axis. |
| + reading.values[0] = -x * kMeanGravity; |
| + reading.values[1] = -y * kMeanGravity; |
| + reading.values[2] = -z * kMeanGravity; |
| + return S_OK; |
| + }); |
| + return params; |
| +} |
| + |
| +// Gyroscope sensor reader initialization parameters. |
| +std::unique_ptr<ReaderInitParams> CreateGyroscopeReaderInitParams() { |
| + auto params = base::MakeUnique<ReaderInitParams>(); |
| + params->sensor_type_id = SENSOR_TYPE_GYROMETER_3D; |
| + params->reporting_mode = mojom::ReportingMode::CONTINUOUS; |
| + params->reader_func = base::Bind([](ISensorDataReport& report, |
| + SensorReading& reading) { |
| + double x = 0.0; |
| + double y = 0.0; |
| + double z = 0.0; |
| + if (!GetReadingValueForProperty( |
| + SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_X_DEGREES_PER_SECOND_SQUARED, |
| + report, &x) || |
| + !GetReadingValueForProperty( |
| + SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_Y_DEGREES_PER_SECOND_SQUARED, |
| + report, &y) || |
| + !GetReadingValueForProperty( |
| + SENSOR_DATA_TYPE_ANGULAR_ACCELERATION_Z_DEGREES_PER_SECOND_SQUARED, |
| + report, &z)) { |
| + return E_FAIL; |
| + } |
| + |
| + // Reverse sings for values and convert from deg/s^2 to rad/s^2. |
| + reading.values[0] = -x * kRadiansInDegreesPerSecond; |
| + reading.values[1] = -y * kRadiansInDegreesPerSecond; |
| + reading.values[2] = -z * kRadiansInDegreesPerSecond; |
| + return S_OK; |
| + }); |
| + return params; |
| +} |
| + |
| +// Magnetometer sensor reader initialization parameters. |
| +std::unique_ptr<ReaderInitParams> CreateMagnetometerReaderInitParams() { |
| + auto params = base::MakeUnique<ReaderInitParams>(); |
| + params->sensor_type_id = SENSOR_TYPE_COMPASS_3D; |
| + params->reporting_mode = mojom::ReportingMode::CONTINUOUS; |
| + params->reader_func = |
| + base::Bind([](ISensorDataReport& report, SensorReading& reading) { |
| + double x = 0.0; |
| + double y = 0.0; |
| + double z = 0.0; |
| + if (!GetReadingValueForProperty( |
| + SENSOR_DATA_TYPE_MAGNETIC_FIELD_STRENGTH_X_MILLIGAUSS, report, |
| + &x) || |
| + !GetReadingValueForProperty( |
| + SENSOR_DATA_TYPE_MAGNETIC_FIELD_STRENGTH_Y_MILLIGAUSS, report, |
| + &y) || |
| + !GetReadingValueForProperty( |
| + SENSOR_DATA_TYPE_MAGNETIC_FIELD_STRENGTH_Z_MILLIGAUSS, report, |
| + &z)) { |
| + return E_FAIL; |
| + } |
| + |
| + // Reverse sings for values and convert from Milligaus to |
|
Reilly Grant (use Gerrit)
2016/11/09 17:11:28
s/sings/signs/
Though, I'd prefer a repeat of the
shalamov
2016/11/10 13:38:10
I copied coordinate system note for all sensors th
|
| + // Microtesla. |
| + reading.values[0] = -x * kMicroteslaInMilligauss; |
| + reading.values[1] = -y * kMicroteslaInMilligauss; |
| + reading.values[2] = -z * kMicroteslaInMilligauss; |
| + return S_OK; |
| + }); |
| + return params; |
| +} |
| + |
| // Creates ReaderInitParams params structure. To implement support for new |
| // sensor types, new switch case should be added and appropriate fields must |
| // be set: |
| @@ -60,23 +178,15 @@ bool GetReadingValueForProperty(REFPROPERTYKEY key, |
| // ISensorDataReport data. |
| std::unique_ptr<ReaderInitParams> CreateReaderInitParamsForSensor( |
| mojom::SensorType type) { |
| - auto params = std::make_unique<ReaderInitParams>(); |
| switch (type) { |
| - case mojom::SensorType::AMBIENT_LIGHT: { |
| - params->sensor_type_id = SENSOR_TYPE_AMBIENT_LIGHT; |
| - params->reporting_mode = mojom::ReportingMode::ON_CHANGE; |
| - params->reader_func = |
| - base::Bind([](ISensorDataReport& report, SensorReading& reading) { |
| - double lux = 0.0; |
| - if (!GetReadingValueForProperty(SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX, |
| - report, &lux)) { |
| - return E_FAIL; |
| - } |
| - reading.values[0] = lux; |
| - return S_OK; |
| - }); |
| - return params; |
| - } |
| + case mojom::SensorType::AMBIENT_LIGHT: |
| + return CreateAmbientLightReaderInitParams(); |
| + case mojom::SensorType::ACCELEROMETER: |
| + return CreateAccelerometerReaderInitParams(); |
| + case mojom::SensorType::GYROSCOPE: |
| + return CreateGyroscopeReaderInitParams(); |
| + case mojom::SensorType::MAGNETOMETER: |
| + return CreateMagnetometerReaderInitParams(); |
| default: |
| NOTIMPLEMENTED(); |
| return nullptr; |