tests: add error_toplevel.nit
[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 android
35 import mnit
36
37 in "C header" `{
38 #include <jni.h>
39 #include <android/sensor.h>
40 #include <android_native_app_glue.h>
41 `}
42
43 extern class ASensorType `{int`}
44 new accelerometer: ASensorType `{return ASENSOR_TYPE_ACCELEROMETER;`}
45 fun is_accelerometer: Bool `{return recv == ASENSOR_TYPE_ACCELEROMETER;`}
46 new magnetic_field: ASensorType `{return ASENSOR_TYPE_MAGNETIC_FIELD;`}
47 fun is_magnetic_field: Bool `{return recv == ASENSOR_TYPE_MAGNETIC_FIELD;`}
48 new gyroscope:ASensorType `{return ASENSOR_TYPE_GYROSCOPE;`}
49 fun is_gyroscope: Bool `{ return recv == ASENSOR_TYPE_GYROSCOPE;`}
50 new light: ASensorType `{return ASENSOR_TYPE_LIGHT;`}
51 fun is_light: Bool `{return recv == ASENSOR_TYPE_LIGHT;`}
52 new proximity: ASensorType `{return ASENSOR_TYPE_PROXIMITY;`}
53 fun is_proximity:Bool `{return recv == ASENSOR_TYPE_PROXIMITY;`}
54 end
55
56 # Manages the sensors
57 extern class ASensorManager `{ASensorManager*`}
58
59 new get_instance: ASensorManager `{return ASensorManager_getInstance();`}
60
61 # Returns the list of available sensors
62 fun get_sensor_list: Pointer `{
63 ASensorList *list;
64 ASensorManager_getSensorList(recv, list);
65 return list;
66 `}
67
68 # Create a new sensor event queue and associate it with a looper
69 fun create_event_queue(app: NativeAppGlue): ASensorEventQueue `{
70 return ASensorManager_createEventQueue(recv, app->looper, LOOPER_ID_USER, NULL, NULL);
71 `}
72
73 # Returns the default sensor of the given type
74 fun get_default_sensor(sensortype: ASensorType): ASensor `{
75 return ASensorManager_getDefaultSensor(recv, sensortype);
76 `}
77
78 # Destroys the event queue and free all resources associated to it
79 fun destroy_event_queue(queue: ASensorEventQueue) `{
80 ASensorManager_destroyEventQueue(recv, queue);
81 `}
82 end
83
84 # Manages the sensors events
85 extern class ASensorEventQueue `{ASensorEventQueue*`}
86
87 # Enable the selected sensor, returns a negative value on error
88 fun enable_sensor(sensor: ASensor): Int `{
89 return ASensorEventQueue_enableSensor(recv, sensor);
90 `}
91
92 # Disable the selected sensor, returns a negative value on error
93 fun disable_sensor(sensor: ASensor): Int `{
94 return ASensorEventQueue_disableSensor(recv, sensor);
95 `}
96
97 # Set the delivery rate of events in microseconds for the given sensor
98 fun set_event_rate(sensor: ASensor, usec: Int): Int `{
99 return ASensorEventQueue_setEventRate(recv, sensor, usec);
100 `}
101 # Returns 1 if the queue has events, 0 if it does not have events,
102 # and a negative value if there is an error
103 fun has_events: Int `{
104 return ASensorEventQueue_hasEvents(recv);
105 `}
106
107 # Returns the next available events from the queue.
108 # Returns a negative value if no events are available or an error has occured
109 # otherwise the number of events returned
110 fun get_events(events: ASensorEvents, count: Int): Int `{
111 return ASensorEventQueue_getEvents(recv, events, (size_t)count);
112 `}
113 end
114
115 # Extern class referencing a ASensor
116 extern class ASensor `{ASensorRef`}
117
118 new `{return malloc(sizeof(ASensorRef));`}
119 fun name: NativeString `{return (char*)ASensor_getName(recv);`}
120 fun vendor: NativeString `{return (char*)ASensor_getVendor(recv);`}
121 fun sensor_type: ASensorType `{return ASensor_getType(recv);`}
122 fun resolution: Float `{return ASensor_getResolution(recv);`}
123 fun min_delay: Int `{return ASensor_getMinDelay(recv);`}
124 end
125
126 # NIT representation of an Android Sensor used in android_app to initialize sensors
127 class AndroidSensor
128
129 var asensor writable = new ASensor
130 var enabled writable = false
131 var event_rate writable = 100000
132
133 fun name: String do return asensor.name.to_s
134 fun vendor: String do return asensor.vendor.to_s
135 fun sensor_type: ASensorType do return asensor.sensor_type
136 fun resolution: Float do return asensor.resolution
137 fun min_delay: Int do return asensor.min_delay
138 end
139
140 # Extern class referencing a ASensorEvent
141 extern class ASensorEvent `{ASensorEvent*`}
142 super SensorEvent
143
144 fun version: Int `{return recv->version;`}
145 fun sensor: ASensor `{return (ASensorRef)recv->sensor;`}
146 fun sensor_type: ASensorType `{return recv->type;`}
147 fun timestamp: Int `{return recv->timestamp;`}
148 end
149
150 extern class FullSensor `{ASensorEvent*`}
151 super ASensorLight
152 super ASensorProximity
153
154 fun temperature: Float `{return recv->temperature;`}
155 fun pressure: Float `{return recv->pressure;`}
156 fun data: Pointer `{return recv->data;`}
157 fun vector: ASensorVector `{return &(recv->vector);`}
158 fun acceleration: ASensorVector `{return &(recv->acceleration);`}
159 fun magnetic: ASensorVector `{return &(recv->magnetic);`}
160 end
161
162 # Extern class referencing a ASensorVector, attribute of ASensorRef
163 extern class ASensorVector `{ASensorVector*`}
164
165 fun v: Pointer `{return recv->v;`}
166 fun x: Float `{ return recv->x;`}
167 fun y: Float `{return recv->y;`}
168 fun z: Float `{return recv->z;`}
169 fun azimuth: Float `{return recv->azimuth;`}
170 fun pitch: Float `{return recv->pitch;`}
171 fun roll: Float `{return recv->roll;`}
172 fun status: Int `{return recv->status;`}
173 fun reserved: Pointer `{return recv->reserved;`}
174 end
175
176 # Sensor event returned by the Accelerometer sensor
177 extern class ASensorAccelerometer `{ASensorEvent*`}
178 super ASensorEvent
179
180 fun x: Float `{return recv->acceleration.x;`}
181 fun y: Float `{return recv->acceleration.y;`}
182 fun z: Float `{return recv->acceleration.z;`}
183 end
184
185 # Sensor event returned by the Magnetic Field sensor
186 extern class ASensorMagneticField `{ASensorEvent*`}
187 super ASensorEvent
188
189 fun x: Float `{return recv->magnetic.x;`}
190 fun y: Float `{return recv->magnetic.y;`}
191 fun z: Float `{ return recv->magnetic.z;`}
192 end
193
194 # Sensor event returned by the gyroscope sensor
195 extern class ASensorGyroscope `{ASensorEvent*`}
196 super ASensorEvent
197
198 fun x: Float `{return recv->vector.x;`}
199 fun y: Float `{return recv->vector.y;`}
200 fun z: Float `{return recv->vector.y;`}
201 end
202
203 # Sensor event returned by the Light sensor
204 extern class ASensorLight `{ASensorEvent*`}
205 super ASensorEvent
206
207 fun light: Float `{return recv->light;`}
208 end
209
210 # sensor event returned by the Proximity Sensor
211 extern class ASensorProximity `{ASensorEvent*`}
212 super ASensorEvent
213
214 fun distance: Float `{return recv->distance;`}
215 end
216
217 # Array of SensorEvents
218 extern class ASensorEvents `{ASensorEvent*`}
219
220 new (length: Int) `{return malloc(sizeof(ASensorEvent)*length);`}
221
222 fun [](index: Int): ASensorEvent `{
223 return recv+index;
224 `}
225 end