Ocean
Loading...
Searching...
No Matches
AndroidSensor.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_SENSOR_H
9#define META_OCEAN_DEVICES_ANDROID_ANDROID_SENSOR_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 AndroidSensor :
40 virtual public AndroidDevice,
41 virtual public Sensor
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 the instance of the sensor manager.
164 * @return The sensor manager instance
165 */
166 static ASensorManager* sensorManager();
167
168 protected:
169
170 /**
171 * Creates a new sensor by its name and type.
172 * @param name The name of the sensor
173 * @param type Major and minor device type of the sensor
174 */
175 AndroidSensor(const std::string& name, const DeviceType type);
176
177 /**
178 * Registers this sensor for the event function.
179 * @param sensorManager The sensor manager which has been used to create the sensor, must be valid
180 */
181 bool registerForEventFunction(ASensorManager* sensorManager);
182
183 /**
184 * Returns the relative and unix timestamp of an Android sensor event.
185 * @param sensorEvent The Android sensor event
186 * @param relativeTimestamp The resulting relative timestamp
187 * @return The resulting unix timestamp
188 */
189 Timestamp convertTimestamp(const ASensorEvent& sensorEvent, Timestamp& relativeTimestamp);
190
191 /**
192 * The actual event function of this device.
193 * @return 1 to receive further events, 0 to stop receiving events
194 */
195 virtual int onEventFunction() = 0;
196
197 private:
198
199 /**
200 * The static event function.
201 * @param fd File descriptor
202 * @param events The number of events
203 * @param data The event data information, if any
204 * @return 1 to receive further events, 0 to stop receiving events
205 */
206 static inline int onEventFunctionStatic(int fd, int events, void* data);
207
208 /**
209 * Returns the timestamp converter for all android sensors.
210 * @return The timestamp converter
211 */
213
214 protected:
215
216 /// The object id of this sensor.
217 ObjectId sensorObjectId_ = invalidObjectId();
218
219 /// The sensor manager of this device.
220 ASensorManager* sensorManager_ = nullptr;
221
222 /// The Android NDK sensor of this device.
223 const ASensor* sensor_ = nullptr;
224
225 /// The Android NDK event queue of this device.
226 ASensorEventQueue* eventQueue_ = nullptr;
227
228 /// True, if this sensor is started.
229 bool isStarted_ = false;
230
231 /// The timestamp converter of this sensor.
232 Timestamp::TimestampConverter& timestampConverter_;
233};
234
235inline int AndroidSensor::onEventFunctionStatic(int fd, int events, void* data)
236{
237 if (data == nullptr)
238 {
239 return 0;
240 }
241
242 AndroidDevice* androidDevice = static_cast<AndroidDevice*>(data);
243
244 if (androidDevice == nullptr)
245 {
246 return 0;
247 }
248
249 AndroidSensor* androidSensor = dynamic_cast<AndroidSensor*>(androidDevice);
250 ocean_assert(androidSensor != nullptr);
251
252 if (androidSensor)
253 {
254 return androidSensor->onEventFunction();
255 }
256
257 return 0;
258}
259
260}
261
262}
263
264}
265
266#endif // META_OCEAN_DEVICES_ANDROID_ANDROID_SENSOR_H
This class implements a device for the Android library.
Definition AndroidDevice.h:29
This class implements the manager for the looper thread.
Definition AndroidSensor.h:51
ALooper * looper()
Returns the looper of the manager.
LooperManager()
Creates a new manager object and creates a looper.
void threadRun() override
Thread run function.
This class implements a sensor for the Android library.
Definition AndroidSensor.h:42
static Timestamp::TimestampConverter & timestampConverter()
Returns the timestamp converter for all android sensors.
AndroidSensor(const std::string &name, const DeviceType type)
Creates a new sensor by its name and type.
bool pause() override
Pauses the device.
static int onEventFunctionStatic(int fd, int events, void *data)
The static event function.
Definition AndroidSensor.h:235
bool start() override
Starts the device.
bool stop() override
Stops the device.
bool registerForEventFunction(ASensorManager *sensorManager)
Registers this sensor for the event function.
AndroidSensorType
Definition of Android sensor types as defined by the Android API.
Definition AndroidSensor.h:88
virtual int onEventFunction()=0
The actual event function of this device.
Timestamp convertTimestamp(const ASensorEvent &sensorEvent, Timestamp &relativeTimestamp)
Returns the relative and unix timestamp of an Android sensor event.
static ASensorManager * sensorManager()
Returns the instance of the sensor manager.
Definition of a class holding the major and minor device type.
Definition devices/Device.h:62
unsigned int ObjectId
Definition of an object id.
Definition Measurement.h:46
This class implements the base class for all sensors.
Definition Sensor.h:36
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:54
This class implements a timestamp.
Definition Timestamp.h:47
The namespace covering the entire Ocean framework.
Definition Accessor.h:15