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_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "chromeos/chromeos_export.h"
14 #include "ui/gfx/geometry/vector3d_f.h"
22 // Reads an accelerometer device and reports data back to an
23 // AccelerometerDelegate.
24 class CHROMEOS_EXPORT AccelerometerReader
{
26 // Configuration structure for accelerometer device.
27 struct ConfigurationData
{
31 // Scale of accelerometers (i.e. raw value * 1.0f / scale = G's).
32 unsigned int base_scale
;
33 unsigned int lid_scale
;
35 // Index of each accelerometer axis in data stream.
36 std::vector
<unsigned int> index
;
38 typedef base::RefCountedData
<ConfigurationData
> Configuration
;
39 typedef base::RefCountedData
<char[12]> Reading
;
41 // An interface to receive data from the AccelerometerReader.
44 virtual void HandleAccelerometerReading(const gfx::Vector3dF
& base
,
45 const gfx::Vector3dF
& lid
) = 0;
48 AccelerometerReader(base::TaskRunner
* blocking_task_runner
,
50 ~AccelerometerReader();
53 // Dispatched when initialization is complete. If |success|, |configuration|
54 // provides the details of the detected accelerometer.
55 void OnInitialized(scoped_refptr
<Configuration
> configuration
, bool success
);
57 // Triggers an asynchronous read from the accelerometer, signalling
58 // OnDataRead with the result.
61 // If |success|, converts the raw reading to a pair of Vector3dF
62 // values and notifies the |delegate_| with the new readings.
63 // Triggers another read from the accelerometer at the current sampling rate.
64 void OnDataRead(scoped_refptr
<Reading
> reading
, bool success
);
66 // The task runner to use for blocking tasks.
67 scoped_refptr
<base::TaskRunner
> task_runner_
;
69 // A weak pointer to the delegate to send accelerometer readings to.
72 // The accelerometer configuration.
73 scoped_refptr
<Configuration
> configuration_
;
75 base::WeakPtrFactory
<AccelerometerReader
> weak_factory_
;
77 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader
);
80 } // namespace chromeos
82 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_