blob: 4291d3e88fc25bc9b6ae57f2d829a38d9de6ea6d [file] [log] [blame]
Mathias Agopiane5580102010-07-12 20:05:06 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#ifndef ANDROID_SENSOR_H
19#define ANDROID_SENSOR_H
20
21/******************************************************************
22 *
23 * IMPORTANT NOTICE:
24 *
25 * This file is part of Android's set of stable system headers
26 * exposed by the Android NDK (Native Development Kit).
27 *
28 * Third-party source AND binary code relies on the definitions
29 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
30 *
31 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
32 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
33 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
34 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
35 */
36
37/*
38 * Structures and functions to receive and process sensor events in
39 * native code.
40 *
41 */
42
43#include <sys/types.h>
44
45#include <android/looper.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51
52/*
53 * Sensor types
54 * (keep in sync with hardware/sensor.h)
55 */
56
57enum {
58 ASENSOR_TYPE_ACCELEROMETER = 1,
59 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
60 ASENSOR_TYPE_GYROSCOPE = 4,
61 ASENSOR_TYPE_LIGHT = 5,
62 ASENSOR_TYPE_PROXIMITY = 8
63};
64
65/*
66 * Sensor accuracy measure
67 */
68enum {
69 ASENSOR_STATUS_UNRELIABLE = 0,
70 ASENSOR_STATUS_ACCURACY_LOW = 1,
71 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
72 ASENSOR_STATUS_ACCURACY_HIGH = 3
73};
74
75/*
76 * A few useful constants
77 */
78
79/* Earth's gravity in m/s^2 */
80#define ASENSOR_STANDARD_GRAVITY (9.80665f)
81/* Maximum magnetic field on Earth's surface in uT */
82#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
83/* Minimum magnetic field on Earth's surface in uT*/
84#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
85
86/*
87 * A sensor event.
88 */
89
90typedef struct ASensorVector {
91 union {
92 float v[3];
93 struct {
94 float x;
95 float y;
96 float z;
97 };
98 };
99 int8_t status;
100 uint8_t reserved[3];
101} ASensorVector;
102
103typedef struct ASensorEvent {
104 int sensor;
105 int32_t reserved0;
106 union {
107 float data[16];
108 ASensorVector acceleration;
109 ASensorVector magnetic;
110 float temperature;
111 float distance;
112 float light;
113 };
114 int64_t timestamp;
115 int32_t reserved1[4];
116} ASensorEvent;
117
118
119struct ASensorManager;
120typedef struct ASensorManager ASensorManager;
121
122struct ASensorEventQueue;
123typedef struct ASensorEventQueue ASensorEventQueue;
124
125struct ASensor;
126typedef struct ASensor ASensor;
127
128/*****************************************************************************/
129
130/*
131 * Get a reference to the sensor manager. ASensorManager is a singleton.
132 *
133 * Example:
134 *
135 * ASensorManager* sensorManager = ASensorManager_getInstance();
136 *
137 */
138ASensorManager* ASensorManager_getInstance();
139
140
141/*
142 * Returns the list of available sensors.
143 */
144int ASensorManager_getSensorList(ASensorManager* manager, ASensor** list);
145
146/*
147 * Returns the default sensor for the given type, or NULL if no sensor
148 * of that type exist.
149 */
150ASensor* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
151
152/*
153 * Creates a new sensor event queue and associate it with a looper.
154 */
155ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
156 ALooper* looper, ALooper_callbackFunc* callback, void* data);
157
158/*
159 * Destroys the event queue and free all resources associated to it.
160 */
161int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
162
163
164/*****************************************************************************/
165
166/*
167 * Enable the selected sensor. Returns a negative error code on failure.
168 */
169int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor* sensor);
170
171/*
172 * Disable the selected sensor. Returns a negative error code on failure.
173 */
174int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor* sensor);
175
176/*
177 * Sets the delivery rate of events in microseconds for the given sensor.
178 * Note that this is a hint only, generally event will arrive at a higher
179 * rate.
180 * Returns a negative error code on failure.
181 */
182int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor* sensor, int32_t usec);
183
184/*
185 * Returns true if there are one or more events available in the
186 * sensor queue. Returns 1 if the queue has events; 0 if
187 * it does not have events; and a negative value if there is an error.
188 */
189int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
190
191/*
192 * Returns the next available events from the queue. Returns a negative
193 * value if no events are available or an error has occurred, otherwise
194 * the number of events returned.
195 *
196 * Examples:
197 * ASensorEvent event;
198 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
199 *
200 * ASensorEvent eventBuffer[8];
201 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
202 *
203 */
204ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
205 ASensorEvent* events, size_t count);
206
207
208/*****************************************************************************/
209
210/*
211 * Returns this sensor's name (non localized)
212 */
213const char* ASensor_getName(ASensor* sensor);
214
215/*
216 * Returns this sensor's vendor's name (non localized)
217 */
218const char* ASensor_getVendor(ASensor* sensor);
219
220/*
221 * Return this sensor's type
222 */
223int ASensor_getType(ASensor* sensor);
224
225/*
226 * Returns this sensors's resolution
227 */
228float ASensor_getResolution(ASensor* sensor);
229
230
231#ifdef __cplusplus
232};
233#endif
234
235#endif // ANDROID_SENSOR_H