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"
20 // Reads an accelerometer device and reports data back to an
21 // AccelerometerDelegate.
22 class CHROMEOS_EXPORT AccelerometerReader
{
24 // Configuration structure for accelerometer device.
25 struct ConfigurationData
{
29 // Number of accelerometers on device.
32 // Length of accelerometer updates.
35 // Which accelerometers are present on device.
36 bool has
[ui::ACCELEROMETER_SOURCE_COUNT
];
38 // Scale of accelerometers (i.e. raw value * scale = m/s^2).
39 float scale
[ui::ACCELEROMETER_SOURCE_COUNT
][3];
41 // Index of each accelerometer axis in data stream.
42 int index
[ui::ACCELEROMETER_SOURCE_COUNT
][3];
44 typedef base::RefCountedData
<ConfigurationData
> Configuration
;
45 typedef base::RefCountedData
<char[12]> Reading
;
47 // An interface to receive data from the AccelerometerReader.
50 virtual ~Delegate() {}
52 virtual void HandleAccelerometerUpdate(
53 const ui::AccelerometerUpdate
& update
) = 0;
56 AccelerometerReader(scoped_refptr
<base::TaskRunner
> blocking_task_runner
,
58 ~AccelerometerReader();
61 // Dispatched when initialization is complete. If |success|, |configuration|
62 // provides the details of the detected accelerometer.
63 void OnInitialized(scoped_refptr
<Configuration
> configuration
, bool success
);
65 // Triggers an asynchronous read from the accelerometer, signalling
66 // OnDataRead with the result.
69 // If |success|, converts the raw reading to an AccelerometerUpdate
70 // message and notifies the |delegate_| with the new readings.
71 // Triggers another read from the accelerometer at the current sampling rate.
72 void OnDataRead(scoped_refptr
<Reading
> reading
, bool success
);
74 // The task runner to use for blocking tasks.
75 scoped_refptr
<base::TaskRunner
> task_runner_
;
77 // A weak pointer to the delegate to send accelerometer readings to.
80 // The last seen accelerometer data.
81 ui::AccelerometerUpdate update_
;
83 // The accelerometer configuration.
84 scoped_refptr
<Configuration
> configuration_
;
86 base::WeakPtrFactory
<AccelerometerReader
> weak_factory_
;
88 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader
);
91 } // namespace chromeos
93 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_