Don't schedule more invokeFunctors than necessary.
[chromium-blink-merge.git] / chromeos / accelerometer / accelerometer_reader.h
blob3cf473f72cf44346c83714a3a9f2fdea6f0e4861
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"
14 namespace base {
15 class TaskRunner;
18 namespace chromeos {
20 // Reads an accelerometer device and reports data back to an
21 // AccelerometerDelegate.
22 class CHROMEOS_EXPORT AccelerometerReader {
23 public:
24 // Configuration structure for accelerometer device.
25 struct ConfigurationData {
26 ConfigurationData();
27 ~ConfigurationData();
29 // Number of accelerometers on device.
30 size_t count;
32 // Length of accelerometer updates.
33 size_t length;
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.
48 class Delegate {
49 public:
50 virtual ~Delegate() {}
52 virtual void HandleAccelerometerUpdate(
53 const ui::AccelerometerUpdate& update) = 0;
56 AccelerometerReader(scoped_refptr<base::TaskRunner> blocking_task_runner,
57 Delegate* delegate);
58 ~AccelerometerReader();
60 private:
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.
67 void TriggerRead();
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.
78 Delegate* delegate_;
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_