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.
5 #ifndef CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_
6 #define CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list_threadsafe.h"
11 #include "chromeos/accelerometer/accelerometer_types.h"
12 #include "chromeos/chromeos_export.h"
15 struct DefaultSingletonTraits
;
23 // Reads an accelerometer device and reports data back to an
24 // AccelerometerDelegate.
25 class CHROMEOS_EXPORT AccelerometerReader
{
27 // The time to wait between reading the accelerometer.
28 static const int kDelayBetweenReadsMs
;
30 // Configuration structure for accelerometer device.
31 struct ConfigurationData
{
35 // Number of accelerometers on device.
38 // Length of accelerometer updates.
41 // Which accelerometers are present on device.
42 bool has
[ACCELEROMETER_SOURCE_COUNT
];
44 // Scale of accelerometers (i.e. raw value * scale = m/s^2).
45 float scale
[ACCELEROMETER_SOURCE_COUNT
][3];
47 // Index of each accelerometer axis in data stream.
48 int index
[ACCELEROMETER_SOURCE_COUNT
][3];
50 typedef base::RefCountedData
<ConfigurationData
> Configuration
;
51 typedef base::RefCountedData
<char[12]> Reading
;
53 // An interface to receive data from the AccelerometerReader.
56 virtual void OnAccelerometerUpdated(
57 scoped_refptr
<const AccelerometerUpdate
> update
) = 0;
60 virtual ~Observer() {}
63 static AccelerometerReader
* GetInstance();
65 void Initialize(scoped_refptr
<base::TaskRunner
> blocking_task_runner
);
67 // Add/Remove observers.
68 void AddObserver(Observer
* observer
);
69 void RemoveObserver(Observer
* observer
);
72 AccelerometerReader();
73 virtual ~AccelerometerReader();
76 friend struct DefaultSingletonTraits
<AccelerometerReader
>;
78 // Dispatched when initialization is complete. If |success|, |configuration|
79 // provides the details of the detected accelerometer.
80 void OnInitialized(scoped_refptr
<Configuration
> configuration
, bool success
);
82 // Triggers an asynchronous read from the accelerometer, signalling
83 // OnDataRead with the result.
86 // If |success|, converts the raw reading to an AccelerometerUpdate
87 // message and notifies the |delegate_| with the new readings.
88 // Triggers another read from the accelerometer at the current sampling rate.
89 void OnDataRead(scoped_refptr
<Reading
> reading
, bool success
);
91 // The task runner to use for blocking tasks.
92 scoped_refptr
<base::TaskRunner
> task_runner_
;
94 // The last seen accelerometer data.
95 scoped_refptr
<AccelerometerUpdate
> update_
;
97 // The accelerometer configuration.
98 scoped_refptr
<Configuration
> configuration_
;
100 scoped_refptr
<base::ObserverListThreadSafe
<Observer
>> observers_
;
102 base::WeakPtrFactory
<AccelerometerReader
> weak_factory_
;
104 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader
);
107 } // namespace chromeos
109 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_