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/chromeos_export.h"
12 #include "ui/accelerometer/accelerometer_types.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
[ui::ACCELEROMETER_SOURCE_COUNT
];
41 // Scale of accelerometers (i.e. raw value * scale = m/s^2).
42 float scale
[ui::ACCELEROMETER_SOURCE_COUNT
][3];
44 // Index of each accelerometer axis in data stream.
45 int index
[ui::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(
54 const ui::AccelerometerUpdate
& update
) = 0;
57 virtual ~Observer() {}
60 static AccelerometerReader
* GetInstance();
62 void Initialize(scoped_refptr
<base::TaskRunner
> blocking_task_runner
);
64 // Add/Remove observers.
65 void AddObserver(Observer
* observer
);
66 void RemoveObserver(Observer
* observer
);
68 // A reading is considered stable if its deviation from gravity is small. This
69 // returns false if the deviation is too higher, or if |source| is not present
71 static bool IsReadingStable(const ui::AccelerometerUpdate
& update
,
72 ui::AccelerometerSource source
);
75 AccelerometerReader();
76 virtual ~AccelerometerReader();
79 friend struct DefaultSingletonTraits
<AccelerometerReader
>;
81 // Dispatched when initialization is complete. If |success|, |configuration|
82 // provides the details of the detected accelerometer.
83 void OnInitialized(scoped_refptr
<Configuration
> configuration
, bool success
);
85 // Triggers an asynchronous read from the accelerometer, signalling
86 // OnDataRead with the result.
89 // If |success|, converts the raw reading to an AccelerometerUpdate
90 // message and notifies the |delegate_| with the new readings.
91 // Triggers another read from the accelerometer at the current sampling rate.
92 void OnDataRead(scoped_refptr
<Reading
> reading
, bool success
);
94 // The task runner to use for blocking tasks.
95 scoped_refptr
<base::TaskRunner
> task_runner_
;
97 // The last seen accelerometer data.
98 ui::AccelerometerUpdate update_
;
100 // The accelerometer configuration.
101 scoped_refptr
<Configuration
> configuration_
;
103 ObserverList
<Observer
, true> observers_
;
105 base::WeakPtrFactory
<AccelerometerReader
> weak_factory_
;
107 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader
);
110 } // namespace chromeos
112 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_