Ocean
Loading...
Searching...
No Matches
AndroidEventDevice.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_DEVICES_ANDROID_ANDROID_EVENT_DEVICE_H
9#define META_OCEAN_DEVICES_ANDROID_ANDROID_EVENT_DEVICE_H
10
13
16#include "ocean/base/Thread.h"
17
19
20#include <android/sensor.h>
21
22namespace Ocean
23{
24
25namespace Devices
26{
27
28namespace Android
29{
30
31/**
32 * This class implements a sensor for the Android library.
33 * The coordinate system of each Android sensor is defined so that the origin of the coordinate system is located in the center of the device.<br>
34 * The x-axis is horizontal and pointing to the right of the device (if the device is held in default orientation).<br>
35 * The y-axis is vertical and pointing to the top of the device.<br>
36 * The z-axis is perpendicular to the screen plane and pointing towards the user (a right handed coordinate system).
37 * @ingroup devicesandroid
38 */
39class OCEAN_DEVICES_ANDROID_EXPORT AndroidEventDevice :
40 virtual public AndroidDevice,
41 virtual public Measurement
42{
43 protected:
44
45 /**
46 * This class implements the manager for the looper thread.
47 */
49 public Singleton<LooperManager>,
50 protected Thread
51 {
52 friend class Singleton<LooperManager>;
53
54 public:
55
56 /**
57 * Returns the looper of the manager.
58 * @return Looper of the manager, will be valid
59 */
60 ALooper* looper();
61
62 protected:
63
64 /**
65 * Creates a new manager object and creates a looper.
66 */
68
69 /**
70 * Thread run function.
71 * @see Thread::threadRun().
72 */
73 void threadRun() override;
74
75 protected:
76
77 /// The looper of this manager.
78 ALooper* looper_ = nullptr;
79 };
80
81 public:
82
83 /**
84 * Definition of Android sensor types as defined by the Android API.
85 * The values are defined by the NDK Android API and thus must not changed.
86 */
88 {
89 /// Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.
90 AST_ACCELEROMETER = 1,
91 /// Measures the ambient geomagnetic field for all three physical axes (x, y, z) in microT.
92 AST_MAGNETIC_FIELD = 2,
93 /// Measures degrees of rotation that a device makes around all three physical axes (x, y, z), deprecated since 2.2.
94 AST_ORIENTATION = 3,
95 /// Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).
96 AST_GYROSCOPE = 4,
97 /// Measures the ambient light level (illumination) in lx.
98 AST_LIGHT = 5,
99 /// Measures the ambient air pressure in hPa or mbar.
100 AST_PRESSURE = 6,
101 /// Measures the temperature of the device in degrees Celsius. This sensor implementation varies across devices and this sensor was replaced with the TYPE_AMBIENT_TEMPERATURE sensor in API Level 14
102 AST_TEMPERATURE = 7,
103 /// Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically used to determine whether a handset is being held up to a person's ear.
104 AST_PROXIMITY = 8,
105 /// Measures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z).
106 AST_GRAVITY = 9,
107 /// Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity.
108 AST_LINEAR_ACCELERATION = 10,
109 /// Measures the orientation of a device by providing the three elements of the device's rotation vector.
110 AST_ROTATION_VECTOR = 11,
111 /// Measures the relative ambient humidity in percent (%).
112 AST_RELATIVE_HUMIDITY = 12,
113 /// Measures the ambient room temperature in degrees Celsius.
114 AST_AMBIENT_TEMPERATURE = 13,
115 AST_MAGNETIC_FIELD_UNCALIBRATED = 14,
116 AST_GAME_ROTATION_VECTOR = 15,
117 AST_GYROSCOPE_UNCALIBRATED = 16,
118 AST_SIGNIFICANT_MOTION = 17,
119 AST_STEP_DETECTOR = 18,
120 AST_STEP_COUNTER = 19,
121 AST_GEOMAGNETIC_ROTATION_VECTOR = 20,
122 AST_HEART_RATE = 21,
123 AST_POSE_6DOF = 28,
124 AST_STATIONARY_DETECT = 29,
125 AST_MOTION_DETECT = 30,
126 AST_HEART_BEAT = 31,
127 AST_DYNAMIC_SENSOR_META = 32,
128 AST_ADDITIONAL_INFO = 33,
129 AST_LOW_LATENCY_OFFBODY_DETECT = 34,
130 AST_ACCELEROMETER_UNCALIBRATED = 35,
131 AST_HINGE_ANGLE = 36,
132 AST_HEAD_TRACKER = 37,
133 AST_ACCELEROMETER_LIMITED_AXES = 38,
134 AST_GYROSCOPE_LIMITED_AXES = 39,
135 AST_ACCELEROMETER_LIMITED_AXES_UNCALIBRATED = 40,
136 AST_GYROSCOPE_LIMITED_AXES_UNCALIBRATED = 41,
137 AST_HEADING = 42,
138
139 AST_END = 43
140 };
141
142 public:
143
144 /**
145 * Starts the device.
146 * @see Device::start().
147 */
148 bool start() override;
149
150 /**
151 * Pauses the device.
152 * @see Device::pause().
153 */
154 bool pause() override;
155
156 /**
157 * Stops the device.
158 * @see Device::stop().
159 */
160 bool stop() override;
161
162 /**
163 * Returns whether this device is active.
164 * @see Device::isStarted().
165 */
166 bool isStarted() const override;
167
168 /**
169 * Returns the instance of the sensor manager.
170 * @return The sensor manager instance
171 */
172 static ASensorManager* sensorManager();
173
174 protected:
175
176 /**
177 * Creates a new event device by its name and type.
178 * @param name The name of the sensor
179 * @param type Major and minor device type of the sensor
180 */
181 AndroidEventDevice(const std::string& name, const DeviceType type);
182
183 /**
184 * Registers this sensor for the event function.
185 * @param sensorManager The sensor manager which has been used to create the sensor, must be valid
186 */
187 bool registerForEventFunction(ASensorManager* sensorManager);
188
189 /**
190 * Returns the relative and unix timestamp of an Android sensor event.
191 * @param sensorEvent The Android sensor event
192 * @param relativeTimestamp The resulting relative timestamp
193 * @return The resulting unix timestamp
194 */
195 Timestamp convertTimestamp(const ASensorEvent& sensorEvent, Timestamp& relativeTimestamp);
196
197 /**
198 * The actual event function of this device.
199 * @return 1 to receive further events, 0 to stop receiving events
200 */
201 virtual int onEventFunction() = 0;
202
203 private:
204
205 /**
206 * The static event function.
207 * @param fd File descriptor
208 * @param events The number of events
209 * @param data The event data information, if any
210 * @return 1 to receive further events, 0 to stop receiving events
211 */
212 static inline int onEventFunctionStatic(int fd, int events, void* data);
213
214 /**
215 * Returns the timestamp converter for all android sensors.
216 * @return The timestamp converter
217 */
219
220 protected:
221
222 /// The object id of this sensor.
223 ObjectId sensorObjectId_ = invalidObjectId();
224
225 /// The sensor manager of this device.
226 ASensorManager* sensorManager_ = nullptr;
227
228 /// The Android NDK sensor of this device.
229 const ASensor* sensor_ = nullptr;
230
231 /// The Android NDK event queue of this device.
232 ASensorEventQueue* eventQueue_ = nullptr;
233
234 /// True, if this sensor is started.
235 bool isStarted_ = false;
236
237 /// The timestamp converter of this sensor.
238 TimestampConverter& timestampConverter_;
239};
240
241inline int AndroidEventDevice::onEventFunctionStatic(int fd, int events, void* data)
242{
243 if (data == nullptr)
244 {
245 return 0;
246 }
247
248 AndroidDevice* androidDevice = static_cast<AndroidDevice*>(data);
249
250 if (androidDevice == nullptr)
251 {
252 return 0;
253 }
254
255 AndroidEventDevice* androidEventDevice = dynamic_cast<AndroidEventDevice*>(androidDevice);
256 ocean_assert(androidEventDevice != nullptr);
257
258 if (androidEventDevice)
259 {
260 return androidEventDevice->onEventFunction();
261 }
262
263 return 0;
264}
265
266}
267
268}
269
270}
271
272#endif // META_OCEAN_DEVICES_ANDROID_ANDROID_EVENT_DEVICE_H
This class implements a device for the Android library.
Definition AndroidDevice.h:29
This class implements the manager for the looper thread.
Definition AndroidEventDevice.h:51
LooperManager()
Creates a new manager object and creates a looper.
ALooper * looper()
Returns the looper of the manager.
This class implements a sensor for the Android library.
Definition AndroidEventDevice.h:42
bool registerForEventFunction(ASensorManager *sensorManager)
Registers this sensor for the event function.
virtual int onEventFunction()=0
The actual event function of this device.
AndroidEventDevice(const std::string &name, const DeviceType type)
Creates a new event device by its name and type.
Timestamp convertTimestamp(const ASensorEvent &sensorEvent, Timestamp &relativeTimestamp)
Returns the relative and unix timestamp of an Android sensor event.
static int onEventFunctionStatic(int fd, int events, void *data)
The static event function.
Definition AndroidEventDevice.h:241
AndroidSensorType
Definition of Android sensor types as defined by the Android API.
Definition AndroidEventDevice.h:88
bool start() override
Starts the device.
bool pause() override
Pauses the device.
bool stop() override
Stops the device.
static ASensorManager * sensorManager()
Returns the instance of the sensor manager.
static TimestampConverter & timestampConverter()
Returns the timestamp converter for all android sensors.
bool isStarted() const override
Returns whether this device is active.
Definition of a class holding the major and minor device type.
Definition devices/Device.h:62
This class implements the base class for all devices providing measurement samples.
Definition Measurement.h:41
uint32_t ObjectId
Definition of an object id.
Definition Measurement.h:47
This template class is the base class for all singleton objects.
Definition Singleton.h:71
This class implements a thread.
Definition Thread.h:115
This class is a helper class allowing to converter timestamps defined in a specific time domain to un...
Definition Timestamp.h:313
This class implements a timestamp.
Definition Timestamp.h:64
The namespace covering the entire Ocean framework.
Definition Accessor.h:15