| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 5 #ifndef CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
| 6 #define CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 6 #define CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/observer_list.h" | 10 #include "base/observer_list_threadsafe.h" |
| 11 #include "chromeos/accelerometer/accelerometer_types.h" | 11 #include "chromeos/accelerometer/accelerometer_types.h" |
| 12 #include "chromeos/chromeos_export.h" | 12 #include "chromeos/chromeos_export.h" |
| 13 | 13 |
| 14 template <typename T> | 14 template <typename T> |
| 15 struct DefaultSingletonTraits; | 15 struct DefaultSingletonTraits; |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class TaskRunner; | 18 class TaskRunner; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace chromeos { | 21 namespace chromeos { |
| 22 | 22 |
| 23 // Reads an accelerometer device and reports data back to an | 23 // Reads an accelerometer device and reports data back to an |
| 24 // AccelerometerDelegate. | 24 // AccelerometerDelegate. |
| 25 class CHROMEOS_EXPORT AccelerometerReader { | 25 class CHROMEOS_EXPORT AccelerometerReader { |
| 26 public: | 26 public: |
| 27 // The time to wait between reading the accelerometer. |
| 28 static const int kDelayBetweenReadsMs; |
| 29 |
| 27 // Configuration structure for accelerometer device. | 30 // Configuration structure for accelerometer device. |
| 28 struct ConfigurationData { | 31 struct ConfigurationData { |
| 29 ConfigurationData(); | 32 ConfigurationData(); |
| 30 ~ConfigurationData(); | 33 ~ConfigurationData(); |
| 31 | 34 |
| 32 // Number of accelerometers on device. | 35 // Number of accelerometers on device. |
| 33 size_t count; | 36 size_t count; |
| 34 | 37 |
| 35 // Length of accelerometer updates. | 38 // Length of accelerometer updates. |
| 36 size_t length; | 39 size_t length; |
| 37 | 40 |
| 38 // Which accelerometers are present on device. | 41 // Which accelerometers are present on device. |
| 39 bool has[ACCELEROMETER_SOURCE_COUNT]; | 42 bool has[ACCELEROMETER_SOURCE_COUNT]; |
| 40 | 43 |
| 41 // Scale of accelerometers (i.e. raw value * scale = m/s^2). | 44 // Scale of accelerometers (i.e. raw value * scale = m/s^2). |
| 42 float scale[ACCELEROMETER_SOURCE_COUNT][3]; | 45 float scale[ACCELEROMETER_SOURCE_COUNT][3]; |
| 43 | 46 |
| 44 // Index of each accelerometer axis in data stream. | 47 // Index of each accelerometer axis in data stream. |
| 45 int index[ACCELEROMETER_SOURCE_COUNT][3]; | 48 int index[ACCELEROMETER_SOURCE_COUNT][3]; |
| 46 }; | 49 }; |
| 47 typedef base::RefCountedData<ConfigurationData> Configuration; | 50 typedef base::RefCountedData<ConfigurationData> Configuration; |
| 48 typedef base::RefCountedData<char[12]> Reading; | 51 typedef base::RefCountedData<char[12]> Reading; |
| 49 | 52 |
| 50 // An interface to receive data from the AccelerometerReader. | 53 // An interface to receive data from the AccelerometerReader. |
| 51 class Observer { | 54 class Observer { |
| 52 public: | 55 public: |
| 53 virtual void OnAccelerometerUpdated(const AccelerometerUpdate& update) = 0; | 56 virtual void OnAccelerometerUpdated( |
| 57 scoped_refptr<const AccelerometerUpdate> update) = 0; |
| 54 | 58 |
| 55 protected: | 59 protected: |
| 56 virtual ~Observer() {} | 60 virtual ~Observer() {} |
| 57 }; | 61 }; |
| 58 | 62 |
| 59 static AccelerometerReader* GetInstance(); | 63 static AccelerometerReader* GetInstance(); |
| 60 | 64 |
| 61 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner); | 65 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner); |
| 62 | 66 |
| 63 // Add/Remove observers. | 67 // Add/Remove observers. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 81 | 85 |
| 82 // If |success|, converts the raw reading to an AccelerometerUpdate | 86 // If |success|, converts the raw reading to an AccelerometerUpdate |
| 83 // message and notifies the |delegate_| with the new readings. | 87 // message and notifies the |delegate_| with the new readings. |
| 84 // Triggers another read from the accelerometer at the current sampling rate. | 88 // Triggers another read from the accelerometer at the current sampling rate. |
| 85 void OnDataRead(scoped_refptr<Reading> reading, bool success); | 89 void OnDataRead(scoped_refptr<Reading> reading, bool success); |
| 86 | 90 |
| 87 // The task runner to use for blocking tasks. | 91 // The task runner to use for blocking tasks. |
| 88 scoped_refptr<base::TaskRunner> task_runner_; | 92 scoped_refptr<base::TaskRunner> task_runner_; |
| 89 | 93 |
| 90 // The last seen accelerometer data. | 94 // The last seen accelerometer data. |
| 91 AccelerometerUpdate update_; | 95 scoped_refptr<AccelerometerUpdate> update_; |
| 92 | |
| 93 // True if a valid accelerometer update is available. | |
| 94 bool has_update_; | |
| 95 | 96 |
| 96 // The accelerometer configuration. | 97 // The accelerometer configuration. |
| 97 scoped_refptr<Configuration> configuration_; | 98 scoped_refptr<Configuration> configuration_; |
| 98 | 99 |
| 99 ObserverList<Observer, true> observers_; | 100 scoped_refptr<ObserverListThreadSafe<Observer>> observers_; |
| 100 | 101 |
| 101 base::WeakPtrFactory<AccelerometerReader> weak_factory_; | 102 base::WeakPtrFactory<AccelerometerReader> weak_factory_; |
| 102 | 103 |
| 103 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); | 104 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); |
| 104 }; | 105 }; |
| 105 | 106 |
| 106 } // namespace chromeos | 107 } // namespace chromeos |
| 107 | 108 |
| 108 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 109 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
| OLD | NEW |