051d17cf64c701e0637ee696756bc12b6a191810
[nit.git] / lib / mnit_android / android_sensor.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # This module is used to manipulate android sensors
18 # The sensor support is implemented in android_app module, so the user can enable the type of sensor he wants to use.
19 # There is an example of how you can use the android sensors in nit/examples/mnit_ballz :
20 # `var app = new MyApp
21 # app.sensors_support_enabled = true
22 # app.accelerometer.enabled = true
23 # app.accelerometer.eventrate = 10000
24 # app.magnetic_field.enabled = true
25 # app.gyroscope.enabled = true
26 # app.light.enabled = true
27 # app.proximity.enabled = true
28 # app.main_loop`
29 #
30 # In this example, we enable the sensor support, then enable all types of sensors supported, before running the app.
31 # The result is you get all type of SensorEvent (ASensorAccelerometer, ASensorMagneticField ...) in the input method of your app
32 module android_sensor
33
34 import mnit
35
36 in "C header" `{
37 #include <jni.h>
38 #include <android/sensor.h>
39 #include <android_native_app_glue.h>
40 `}
41
42 in "C" `{
43 extern struct android_app *mnit_java_app;
44 `}
45
46 extern class ASensorType `{int`}
47 new accelerometer: ASensorType `{return ASENSOR_TYPE_ACCELEROMETER;`}
48 fun is_accelerometer: Bool `{return recv == ASENSOR_TYPE_ACCELEROMETER;`}
49 new magnetic_field: ASensorType `{return ASENSOR_TYPE_MAGNETIC_FIELD;`}
50 fun is_magnetic_field: Bool `{return recv == ASENSOR_TYPE_MAGNETIC_FIELD;`}
51 new gyroscope:ASensorType `{return ASENSOR_TYPE_GYROSCOPE;`}
52 fun is_gyroscope: Bool `{ return recv == ASENSOR_TYPE_GYROSCOPE;`}
53 new light: ASensorType `{return ASENSOR_TYPE_LIGHT;`}
54 fun is_light: Bool `{return recv == ASENSOR_TYPE_LIGHT;`}
55 new proximity: ASensorType `{return ASENSOR_TYPE_PROXIMITY;`}
56 fun is_proximity:Bool `{return recv == ASENSOR_TYPE_PROXIMITY;`}
57 end
58
59 # Manages the sensors
60 extern class ASensorManager `{ASensorManager*`}
61
62 new get_instance: ASensorManager `{return ASensorManager_getInstance();`}
63
64 # Returns the list of available sensors
65 fun get_sensor_list: Pointer `{
66 ASensorList *list;
67 ASensorManager_getSensorList(recv, list);
68 return list;
69 `}
70
71 # Create a new sensor event queue and associate it with a looper
72 fun create_event_queue(app: NdkAndroidApp): ASensorEventQueue `{
73 return ASensorManager_createEventQueue(recv, app->looper, LOOPER_ID_USER, NULL, NULL);
74 `}
75
76 # Returns the default sensor of the given type
77 fun get_default_sensor(sensortype: ASensorType): ASensor `{
78 return ASensorManager_getDefaultSensor(recv, sensortype);
79 `}
80
81 # Destroys the event queue and free all resources associated to it
82 fun destroy_event_queue(queue: ASensorEventQueue) `{
83 ASensorManager_destroyEventQueue(recv, queue);
84 `}
85 end
86
87 # Manages the sensors events
88 extern class ASensorEventQueue `{ASensorEventQueue*`}
89
90 # Enable the selected sensor, returns a negative value on error
91 fun enable_sensor(sensor: ASensor): Int `{
92 return ASensorEventQueue_enableSensor(recv, sensor);
93 `}
94
95 # Disable the selected sensor, returns a negative value on error
96 fun disable_sensor(sensor: ASensor): Int `{
97 return ASensorEventQueue_disableSensor(recv, sensor);
98 `}
99
100 # Set the delivery rate of events in microseconds for the given sensor
101 fun set_event_rate(sensor: ASensor, usec: Int): Int `{
102 return ASensorEventQueue_setEventRate(recv, sensor, usec);
103 `}
104 # Returns 1 if the queue has events, 0 if it does not have events,
105 # and a negative value if there is an error
106 fun has_events: Int `{
107 return ASensorEventQueue_hasEvents(recv);
108 `}
109
110 # Returns the next available events from the queue.
111 # Returns a negative value if no events are available or an error has occured
112 # otherwise the number of events returned
113 fun get_events(events: ASensorEvents, count: Int): Int `{
114 return ASensorEventQueue_getEvents(recv, events, (size_t)count);
115 `}
116 end
117
118 # Extern class referencing a ASensor
119 extern class ASensor `{ASensorRef`}
120
121 new `{return malloc(sizeof(ASensorRef));`}
122 fun name: NativeString `{return (char*)ASensor_getName(recv);`}
123 fun vendor: NativeString `{return (char*)ASensor_getVendor(recv);`}
124 fun sensor_type: ASensorType `{return ASensor_getType(recv);`}
125 fun resolution: Float `{return ASensor_getResolution(recv);`}
126 fun min_delay: Int `{return ASensor_getMinDelay(recv);`}
127 end
128
129 # NIT representation of an Android Sensor used in android_app to initialize sensors
130 class AndroidSensor
131
132 var asensor writable = new ASensor
133 var enabled writable = false
134 var event_rate writable = 100000
135
136 fun name: String do return asensor.name.to_s
137 fun vendor: String do return asensor.vendor.to_s
138 fun sensor_type: ASensorType do return asensor.sensor_type
139 fun resolution: Float do return asensor.resolution
140 fun min_delay: Int do return asensor.min_delay
141 end
142
143 # Extern class referencing a ASensorEvent
144 extern class ASensorEvent `{ASensorEvent*`}
145 super SensorEvent
146
147 fun version: Int `{return recv->version;`}
148 fun sensor: ASensor `{return (ASensorRef)recv->sensor;`}
149 fun sensor_type: ASensorType `{return recv->type;`}
150 fun timestamp: Int `{return recv->timestamp;`}
151 end
152
153 extern class FullSensor `{ASensorEvent*`}
154 super ASensorLight
155 super ASensorProximity
156
157 fun temperature: Float `{return recv->temperature;`}
158 fun pressure: Float `{return recv->pressure;`}
159 fun data: Pointer `{return recv->data;`}
160 fun vector: ASensorVector `{return &(recv->vector);`}
161 fun acceleration: ASensorVector `{return &(recv->acceleration);`}
162 fun magnetic: ASensorVector `{return &(recv->magnetic);`}
163 end
164
165 # Extern class referencing a ASensorVector, attribute of ASensorRef
166 extern class ASensorVector `{ASensorVector*`}
167
168 fun v: Pointer `{return recv->v;`}
169 fun x: Float `{ return recv->x;`}
170 fun y: Float `{return recv->y;`}
171 fun z: Float `{return recv->z;`}
172 fun azimuth: Float `{return recv->azimuth;`}
173 fun pitch: Float `{return recv->pitch;`}
174 fun roll: Float `{return recv->roll;`}
175 fun status: Int `{return recv->status;`}
176 fun reserved: Pointer `{return recv->reserved;`}
177 end
178
179 # Sensor event returned by the Accelerometer sensor
180 extern class ASensorAccelerometer `{ASensorEvent*`}
181 super ASensorEvent
182
183 fun x: Float `{return recv->acceleration.x;`}
184 fun y: Float `{return recv->acceleration.y;`}
185 fun z: Float `{return recv->acceleration.z;`}
186 end
187
188 # Sensor event returned by the Magnetic Field sensor
189 extern class ASensorMagneticField `{ASensorEvent*`}
190 super ASensorEvent
191
192 fun x: Float `{return recv->magnetic.x;`}
193 fun y: Float `{return recv->magnetic.y;`}
194 fun z: Float `{ return recv->magnetic.z;`}
195 end
196
197 # Sensor event returned by the gyroscope sensor
198 extern class ASensorGyroscope `{ASensorEvent*`}
199 super ASensorEvent
200
201 fun x: Float `{return recv->vector.x;`}
202 fun y: Float `{return recv->vector.y;`}
203 fun z: Float `{return recv->vector.y;`}
204 end
205
206 # Sensor event returned by the Light sensor
207 extern class ASensorLight `{ASensorEvent*`}
208 super ASensorEvent
209
210 fun light: Float `{return recv->light;`}
211 end
212
213 # sensor event returned by the Proximity Sensor
214 extern class ASensorProximity `{ASensorEvent*`}
215 super ASensorEvent
216
217 fun distance: Float `{return recv->distance;`}
218 end
219
220 # Array of SensorEvents
221 extern class ASensorEvents `{ASensorEvent*`}
222
223 new (length: Int) `{return malloc(sizeof(ASensorEvent)*length);`}
224
225 fun [](index: Int): ASensorEvent `{
226 return recv+index;
227 `}
228 end
229
230 extern class NdkAndroidApp `{struct android_app*`}
231
232 # FIXME: remove when android_app fixed
233 new `{
234 return mnit_java_app;
235 `}
236 end