platform_sensor_provider_linux_base.cc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/device/generic_sensor/platform_sensor_provider_linux_base.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "services/device/generic_sensor/absolute_orientation_euler_angles_fusion_algorithm_using_accelerometer_and_magnetometer.h"
  8. #include "services/device/generic_sensor/gravity_fusion_algorithm_using_accelerometer.h"
  9. #include "services/device/generic_sensor/linear_acceleration_fusion_algorithm_using_accelerometer.h"
  10. #include "services/device/generic_sensor/orientation_quaternion_fusion_algorithm_using_euler_angles.h"
  11. #include "services/device/generic_sensor/platform_sensor_fusion.h"
  12. #include "services/device/generic_sensor/relative_orientation_euler_angles_fusion_algorithm_using_accelerometer.h"
  13. #include "services/device/generic_sensor/relative_orientation_euler_angles_fusion_algorithm_using_accelerometer_and_gyroscope.h"
  14. namespace device {
  15. bool PlatformSensorProviderLinuxBase::IsFusionSensorType(
  16. mojom::SensorType type) const {
  17. switch (type) {
  18. case mojom::SensorType::LINEAR_ACCELERATION:
  19. case mojom::SensorType::GRAVITY:
  20. case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
  21. case mojom::SensorType::ABSOLUTE_ORIENTATION_QUATERNION:
  22. case mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
  23. case mojom::SensorType::RELATIVE_ORIENTATION_QUATERNION:
  24. return true;
  25. default:
  26. return false;
  27. }
  28. }
  29. void PlatformSensorProviderLinuxBase::CreateFusionSensor(
  30. mojom::SensorType type,
  31. SensorReadingSharedBuffer* reading_buffer,
  32. CreateSensorCallback callback) {
  33. DCHECK(IsFusionSensorType(type));
  34. std::unique_ptr<PlatformSensorFusionAlgorithm> fusion_algorithm;
  35. switch (type) {
  36. case mojom::SensorType::LINEAR_ACCELERATION:
  37. fusion_algorithm = std::make_unique<
  38. LinearAccelerationFusionAlgorithmUsingAccelerometer>();
  39. break;
  40. case mojom::SensorType::GRAVITY:
  41. fusion_algorithm =
  42. std::make_unique<GravityFusionAlgorithmUsingAccelerometer>();
  43. break;
  44. case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
  45. fusion_algorithm = std::make_unique<
  46. AbsoluteOrientationEulerAnglesFusionAlgorithmUsingAccelerometerAndMagnetometer>();
  47. break;
  48. case mojom::SensorType::ABSOLUTE_ORIENTATION_QUATERNION:
  49. fusion_algorithm = std::make_unique<
  50. OrientationQuaternionFusionAlgorithmUsingEulerAngles>(
  51. true /* absolute */);
  52. break;
  53. case mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
  54. if (IsSensorTypeAvailable(mojom::SensorType::GYROSCOPE)) {
  55. fusion_algorithm = std::make_unique<
  56. RelativeOrientationEulerAnglesFusionAlgorithmUsingAccelerometerAndGyroscope>();
  57. } else {
  58. fusion_algorithm = std::make_unique<
  59. RelativeOrientationEulerAnglesFusionAlgorithmUsingAccelerometer>();
  60. }
  61. break;
  62. case mojom::SensorType::RELATIVE_ORIENTATION_QUATERNION:
  63. fusion_algorithm = std::make_unique<
  64. OrientationQuaternionFusionAlgorithmUsingEulerAngles>(
  65. false /* absolute */);
  66. break;
  67. default:
  68. NOTREACHED();
  69. }
  70. DCHECK(fusion_algorithm);
  71. PlatformSensorFusion::Create(
  72. reading_buffer, this, std::move(fusion_algorithm), std::move(callback));
  73. }
  74. } // namespace device