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.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 // Configuration structure for accelerometer device.
28 struct ConfigurationData
{
32 // Number of accelerometers on device.
35 // Length of accelerometer updates.
38 // Which accelerometers are present on device.
39 bool has
[ACCELEROMETER_SOURCE_COUNT
];
41 // Scale of accelerometers (i.e. raw value * scale = m/s^2).
42 float scale
[ACCELEROMETER_SOURCE_COUNT
][3];
44 // Index of each accelerometer axis in data stream.
45 int index
[ACCELEROMETER_SOURCE_COUNT
][3];
47 typedef base::RefCountedData
<ConfigurationData
> Configuration
;
48 typedef base::RefCountedData
<char[12]> Reading
;
50 // An interface to receive data from the AccelerometerReader.
53 virtual void OnAccelerometerUpdated(const AccelerometerUpdate
& update
) = 0;
56 virtual ~Observer() {}
59 static AccelerometerReader
* GetInstance();
61 void Initialize(scoped_refptr
<base::TaskRunner
> blocking_task_runner
);
63 // Add/Remove observers.
64 void AddObserver(Observer
* observer
);
65 void RemoveObserver(Observer
* observer
);
68 AccelerometerReader();
69 virtual ~AccelerometerReader();
72 friend struct DefaultSingletonTraits
<AccelerometerReader
>;
74 // Dispatched when initialization is complete. If |success|, |configuration|
75 // provides the details of the detected accelerometer.
76 void OnInitialized(scoped_refptr
<Configuration
> configuration
, bool success
);
78 // Triggers an asynchronous read from the accelerometer, signalling
79 // OnDataRead with the result.
82 // If |success|, converts the raw reading to an AccelerometerUpdate
83 // message and notifies the |delegate_| with the new readings.
84 // Triggers another read from the accelerometer at the current sampling rate.
85 void OnDataRead(scoped_refptr
<Reading
> reading
, bool success
);
87 // The task runner to use for blocking tasks.
88 scoped_refptr
<base::TaskRunner
> task_runner_
;
90 // The last seen accelerometer data.
91 AccelerometerUpdate update_
;
93 // The accelerometer configuration.
94 scoped_refptr
<Configuration
> configuration_
;
96 ObserverList
<Observer
, true> observers_
;
98 base::WeakPtrFactory
<AccelerometerReader
> weak_factory_
;
100 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader
);
103 } // namespace chromeos
105 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_