accelerometer_samples_observer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef ASH_ACCELEROMETER_ACCELEROMETER_SAMPLES_OBSERVER_H_
  5. #define ASH_ACCELEROMETER_ACCELEROMETER_SAMPLES_OBSERVER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "ash/accelerometer/accelerometer_constants.h"
  11. #include "ash/ash_export.h"
  12. #include "base/sequence_checker.h"
  13. #include "base/task/sequenced_task_runner.h"
  14. #include "chromeos/components/sensors/mojom/sensor.mojom.h"
  15. #include "mojo/public/cpp/bindings/receiver.h"
  16. #include "mojo/public/cpp/bindings/remote.h"
  17. namespace ash {
  18. // A SamplesObserver for an accelerometer device. When a sample is updated from
  19. // IIO Service, it's sent to the AccelerometerProviderMojo via the callback
  20. // |on_sample_udpated_callback_| registered in the constructor.
  21. // AccelerometerSamplesObserver should only be used on the UI thread.
  22. class ASH_EXPORT AccelerometerSamplesObserver
  23. : public chromeos::sensors::mojom::SensorDeviceSamplesObserver {
  24. public:
  25. using OnSampleUpdatedCallback =
  26. base::RepeatingCallback<void(int iio_device_id,
  27. std::vector<float> sample)>;
  28. AccelerometerSamplesObserver(
  29. int iio_device_id,
  30. mojo::Remote<chromeos::sensors::mojom::SensorDevice> sensor_device_remote,
  31. float scale,
  32. OnSampleUpdatedCallback on_sample_updated_callback);
  33. AccelerometerSamplesObserver(const AccelerometerSamplesObserver&) = delete;
  34. AccelerometerSamplesObserver& operator=(const AccelerometerSamplesObserver&) =
  35. delete;
  36. ~AccelerometerSamplesObserver() override;
  37. // Sets the observer |enabled| by setting the frequency to iioservice.
  38. // Should be called on |task_runner_|.
  39. void SetEnabled(bool enabled);
  40. // chromeos::sensors::mojom::SensorDeviceSamplesObserver overrides:
  41. void OnSampleUpdated(const base::flat_map<int32_t, int64_t>& sample) override;
  42. void OnErrorOccurred(
  43. chromeos::sensors::mojom::ObserverErrorType type) override;
  44. private:
  45. void Reset();
  46. void GetAllChannelIdsCallback(
  47. const std::vector<std::string>& iio_channel_ids);
  48. void StartReading();
  49. // Update this sensor device's frequency to kReadFrequencyInHz if |enabled_|
  50. // is true, and to 0 if |enabled_| is false.
  51. void UpdateSensorDeviceFrequency();
  52. mojo::PendingRemote<chromeos::sensors::mojom::SensorDeviceSamplesObserver>
  53. GetPendingRemote();
  54. void OnObserverDisconnect();
  55. void SetFrequencyCallback(bool enabled, double result_frequency);
  56. void SetChannelsEnabledCallback(const std::vector<int32_t>& failed_indices);
  57. int iio_device_id_;
  58. mojo::Remote<chromeos::sensors::mojom::SensorDevice> sensor_device_remote_;
  59. double scale_;
  60. // Callback to send samples to the owner of this class.
  61. OnSampleUpdatedCallback on_sample_updated_callback_;
  62. // Boolean to indicate if this accelerometer should set a valid frequency and
  63. // keep reading samples.
  64. bool enabled_ = false;
  65. // The list of channel ids retrieved from iioservice. Use channels' indices
  66. // in this list to identify them.
  67. std::vector<std::string> iio_channel_ids_;
  68. // Channel indices (of accel_x, accel_y, and accel_z respectively) to
  69. // enable.
  70. int32_t channel_indices_[kNumberOfAxes];
  71. mojo::Receiver<chromeos::sensors::mojom::SensorDeviceSamplesObserver>
  72. receiver_{this};
  73. SEQUENCE_CHECKER(sequence_checker_);
  74. base::WeakPtrFactory<AccelerometerSamplesObserver> weak_factory_{this};
  75. };
  76. } // namespace ash
  77. #endif // ASH_ACCELEROMETER_ACCELEROMETER_SAMPLES_OBSERVER_H_