[qcms] Use half float conversion tables
[chromium-blink-merge.git] / chromeos / accelerometer / accelerometer_reader.h
blob29e4e95e153e13e28423477f1b978d6ee8a92541
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_threadsafe.h"
11 #include "chromeos/accelerometer/accelerometer_types.h"
12 #include "chromeos/chromeos_export.h"
14 template <typename T>
15 struct DefaultSingletonTraits;
17 namespace base {
18 class TaskRunner;
21 namespace chromeos {
23 // Reads an accelerometer device and reports data back to an
24 // AccelerometerDelegate.
25 class CHROMEOS_EXPORT AccelerometerReader {
26 public:
27 // The time to wait between reading the accelerometer.
28 static const int kDelayBetweenReadsMs;
30 // Configuration structure for accelerometer device.
31 struct ConfigurationData {
32 ConfigurationData();
33 ~ConfigurationData();
35 // Number of accelerometers on device.
36 size_t count;
38 // Length of accelerometer updates.
39 size_t length;
41 // Which accelerometers are present on device.
42 bool has[ACCELEROMETER_SOURCE_COUNT];
44 // Scale of accelerometers (i.e. raw value * scale = m/s^2).
45 float scale[ACCELEROMETER_SOURCE_COUNT][3];
47 // Index of each accelerometer axis in data stream.
48 int index[ACCELEROMETER_SOURCE_COUNT][3];
50 typedef base::RefCountedData<ConfigurationData> Configuration;
51 typedef base::RefCountedData<char[12]> Reading;
53 // An interface to receive data from the AccelerometerReader.
54 class Observer {
55 public:
56 virtual void OnAccelerometerUpdated(
57 scoped_refptr<const AccelerometerUpdate> update) = 0;
59 protected:
60 virtual ~Observer() {}
63 static AccelerometerReader* GetInstance();
65 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner);
67 // Add/Remove observers.
68 void AddObserver(Observer* observer);
69 void RemoveObserver(Observer* observer);
71 protected:
72 AccelerometerReader();
73 virtual ~AccelerometerReader();
75 private:
76 friend struct DefaultSingletonTraits<AccelerometerReader>;
78 // Dispatched when initialization is complete. If |success|, |configuration|
79 // provides the details of the detected accelerometer.
80 void OnInitialized(scoped_refptr<Configuration> configuration, bool success);
82 // Triggers an asynchronous read from the accelerometer, signalling
83 // OnDataRead with the result.
84 void TriggerRead();
86 // If |success|, converts the raw reading to an AccelerometerUpdate
87 // message and notifies the |delegate_| with the new readings.
88 // Triggers another read from the accelerometer at the current sampling rate.
89 void OnDataRead(scoped_refptr<Reading> reading, bool success);
91 // The task runner to use for blocking tasks.
92 scoped_refptr<base::TaskRunner> task_runner_;
94 // The last seen accelerometer data.
95 scoped_refptr<AccelerometerUpdate> update_;
97 // The accelerometer configuration.
98 scoped_refptr<Configuration> configuration_;
100 scoped_refptr<ObserverListThreadSafe<Observer>> observers_;
102 base::WeakPtrFactory<AccelerometerReader> weak_factory_;
104 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader);
107 } // namespace chromeos
109 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_