Chromium Code Reviews| 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 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 43 | 43 |
| 44 // Index of each accelerometer axis in data stream. | 44 // Index of each accelerometer axis in data stream. |
| 45 int index[ACCELEROMETER_SOURCE_COUNT][3]; | 45 int index[ACCELEROMETER_SOURCE_COUNT][3]; |
| 46 }; | 46 }; |
| 47 typedef base::RefCountedData<ConfigurationData> Configuration; | 47 typedef base::RefCountedData<ConfigurationData> Configuration; |
| 48 typedef base::RefCountedData<char[12]> Reading; | 48 typedef base::RefCountedData<char[12]> Reading; |
| 49 | 49 |
| 50 // An interface to receive data from the AccelerometerReader. | 50 // An interface to receive data from the AccelerometerReader. |
| 51 class Observer { | 51 class Observer { |
| 52 public: | 52 public: |
| 53 virtual void OnAccelerometerUpdated(const AccelerometerUpdate& update) = 0; | 53 virtual void OnAccelerometerUpdated( |
| 54 scoped_refptr<const AccelerometerUpdate> update) = 0; | |
| 54 | 55 |
| 55 protected: | 56 protected: |
| 56 virtual ~Observer() {} | 57 virtual ~Observer() {} |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 static AccelerometerReader* GetInstance(); | 60 static AccelerometerReader* GetInstance(); |
| 60 | 61 |
| 61 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner); | 62 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner); |
| 62 | 63 |
| 63 // Add/Remove observers. | 64 // Add/Remove observers. |
| 64 void AddObserver(Observer* observer); | 65 void AddObserver(Observer* observer); |
| 65 void RemoveObserver(Observer* observer); | 66 void RemoveObserver(Observer* observer); |
| 67 bool HasObserver(Observer* observer); | |
| 68 | |
| 69 // The delay between reads of the accelerometer. | |
| 70 int DelayBetweenReadsMs() const; | |
|
flackr
2015/03/05 19:59:45
Until we resolve increasing the update rate, I thi
jonross
2015/03/05 21:13:39
Done.
| |
| 66 | 71 |
| 67 protected: | 72 protected: |
| 68 AccelerometerReader(); | 73 AccelerometerReader(); |
| 69 virtual ~AccelerometerReader(); | 74 virtual ~AccelerometerReader(); |
| 70 | 75 |
| 71 private: | 76 private: |
| 72 friend struct DefaultSingletonTraits<AccelerometerReader>; | 77 friend struct DefaultSingletonTraits<AccelerometerReader>; |
| 73 | 78 |
| 74 // Dispatched when initialization is complete. If |success|, |configuration| | 79 // Dispatched when initialization is complete. If |success|, |configuration| |
| 75 // provides the details of the detected accelerometer. | 80 // provides the details of the detected accelerometer. |
| 76 void OnInitialized(scoped_refptr<Configuration> configuration, bool success); | 81 void OnInitialized(scoped_refptr<Configuration> configuration, bool success); |
| 77 | 82 |
| 78 // Triggers an asynchronous read from the accelerometer, signalling | 83 // Triggers an asynchronous read from the accelerometer, signalling |
| 79 // OnDataRead with the result. | 84 // OnDataRead with the result. |
| 80 void TriggerRead(); | 85 void TriggerRead(); |
| 81 | 86 |
| 82 // If |success|, converts the raw reading to an AccelerometerUpdate | 87 // If |success|, converts the raw reading to an AccelerometerUpdate |
| 83 // message and notifies the |delegate_| with the new readings. | 88 // message and notifies the |delegate_| with the new readings. |
| 84 // Triggers another read from the accelerometer at the current sampling rate. | 89 // Triggers another read from the accelerometer at the current sampling rate. |
| 85 void OnDataRead(scoped_refptr<Reading> reading, bool success); | 90 void OnDataRead(scoped_refptr<Reading> reading, bool success); |
| 86 | 91 |
| 87 // The task runner to use for blocking tasks. | 92 // The task runner to use for blocking tasks. |
| 88 scoped_refptr<base::TaskRunner> task_runner_; | 93 scoped_refptr<base::TaskRunner> task_runner_; |
| 89 | 94 |
| 90 // The last seen accelerometer data. | 95 // The last seen accelerometer data. |
| 91 AccelerometerUpdate update_; | 96 scoped_refptr<AccelerometerUpdate> update_; |
| 92 | |
| 93 // True if a valid accelerometer update is available. | |
| 94 bool has_update_; | |
| 95 | 97 |
| 96 // The accelerometer configuration. | 98 // The accelerometer configuration. |
| 97 scoped_refptr<Configuration> configuration_; | 99 scoped_refptr<Configuration> configuration_; |
| 98 | 100 |
| 99 ObserverList<Observer, true> observers_; | 101 scoped_refptr<ObserverListThreadSafe<Observer>> observers_; |
| 100 | 102 |
| 101 base::WeakPtrFactory<AccelerometerReader> weak_factory_; | 103 base::WeakPtrFactory<AccelerometerReader> weak_factory_; |
| 102 | 104 |
| 103 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); | 105 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); |
| 104 }; | 106 }; |
| 105 | 107 |
| 106 } // namespace chromeos | 108 } // namespace chromeos |
| 107 | 109 |
| 108 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 110 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
| OLD | NEW |