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 3 // found in the LICENSE file. 4 5 #ifndef CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ 6 #define CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ 7 8 #include "base/memory/ref_counted.h" 9 #include "base/memory/weak_ptr.h" 10 #include "base/observer_list.h" 11 #include "chromeos/chromeos_export.h" 12 #include "ui/accelerometer/accelerometer_types.h" 13 14 namespace base { 15 class TaskRunner; 16 } 17 18 namespace chromeos { 19 20 // Reads an accelerometer device and reports data back to an 21 // AccelerometerDelegate. 22 class CHROMEOS_EXPORT AccelerometerReader { 23 public: 24 // Configuration structure for accelerometer device. 25 struct ConfigurationData { 26 ConfigurationData(); 27 ~ConfigurationData(); 28 29 // Number of accelerometers on device. 30 size_t count; 31 32 // Length of accelerometer updates. 33 size_t length; 34 35 // Which accelerometers are present on device. 36 bool has[ui::ACCELEROMETER_SOURCE_COUNT]; 37 38 // Scale of accelerometers (i.e. raw value * scale = m/s^2). 39 float scale[ui::ACCELEROMETER_SOURCE_COUNT][3]; 40 41 // Index of each accelerometer axis in data stream. 42 int index[ui::ACCELEROMETER_SOURCE_COUNT][3]; 43 }; 44 typedef base::RefCountedData<ConfigurationData> Configuration; 45 typedef base::RefCountedData<char[12]> Reading; 46 47 // An interface to receive data from the AccelerometerReader. 48 class Delegate { 49 public: 50 virtual void HandleAccelerometerUpdate( 51 const ui::AccelerometerUpdate& update) = 0; 52 }; 53 54 AccelerometerReader(scoped_refptr<base::TaskRunner> blocking_task_runner, 55 Delegate* delegate); 56 ~AccelerometerReader(); 57 58 private: 59 // Dispatched when initialization is complete. If |success|, |configuration| 60 // provides the details of the detected accelerometer. 61 void OnInitialized(scoped_refptr<Configuration> configuration, bool success); 62 63 // Triggers an asynchronous read from the accelerometer, signalling 64 // OnDataRead with the result. 65 void TriggerRead(); 66 67 // If |success|, converts the raw reading to an AccelerometerUpdate 68 // message and notifies the |delegate_| with the new readings. 69 // Triggers another read from the accelerometer at the current sampling rate. 70 void OnDataRead(scoped_refptr<Reading> reading, bool success); 71 72 // The task runner to use for blocking tasks. 73 scoped_refptr<base::TaskRunner> task_runner_; 74 75 // A weak pointer to the delegate to send accelerometer readings to. 76 Delegate* delegate_; 77 78 // The last seen accelerometer data. 79 ui::AccelerometerUpdate update_; 80 81 // The accelerometer configuration. 82 scoped_refptr<Configuration> configuration_; 83 84 base::WeakPtrFactory<AccelerometerReader> weak_factory_; 85 86 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); 87 }; 88 89 } // namespace chromeos 90 91 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ 92