1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
39 #include <android/sensor.h>
40 #include <android_native_app_glue.h>
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;`}
57 extern class ASensorManager `{ASensorManager*`}
59 new get_instance
: ASensorManager `{return ASensorManager_getInstance();`}
61 # Returns the list of available sensors
62 fun get_sensor_list: Pointer `{
64 ASensorManager_getSensorList(recv
, list
);
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);
73 # Returns the default sensor of the given type
74 fun get_default_sensor(sensortype: ASensorType): ASensor `{
75 return ASensorManager_getDefaultSensor(recv
, sensortype
);
78 # Destroys the event queue and free all resources associated to it
79 fun destroy_event_queue(queue: ASensorEventQueue) `{
80 ASensorManager_destroyEventQueue(recv
, queue
);
84 # Manages the sensors events
85 extern class ASensorEventQueue `{ASensorEventQueue*`}
87 # Enable the selected sensor, returns a negative value on error
88 fun enable_sensor
(sensor
: ASensor): Int `{
89 return ASensorEventQueue_enableSensor(recv, sensor);
92 # Disable the selected sensor, returns a negative value on error
93 fun disable_sensor
(sensor
: ASensor): Int `{
94 return ASensorEventQueue_disableSensor(recv, sensor);
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);
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);
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);
115 # Extern class referencing a ASensor
116 extern class ASensor `{ASensorRef`}
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);`}
126 # NIT representation of an Android Sensor used in android_app to initialize sensors
129 var asensor writable = new ASensor
130 var enabled writable = false
131 var event_rate writable = 100000
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
140 # Extern class referencing a ASensorEvent
141 extern class ASensorEvent `{ASensorEvent*`}
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;`}
150 extern class FullSensor `{ASensorEvent*`}
152 super ASensorProximity
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);`}
162 # Extern class referencing a ASensorVector, attribute of ASensorRef
163 extern class ASensorVector `{ASensorVector*`}
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;`}
176 # Sensor event returned by the Accelerometer sensor
177 extern class ASensorAccelerometer `{ASensorEvent*`}
180 fun x
: Float `{return recv->acceleration.x;`}
181 fun y: Float `{return recv->acceleration.y;`}
182 fun z
: Float `{return recv->acceleration.z;`}
185 # Sensor event returned by the Magnetic Field sensor
186 extern class ASensorMagneticField `{ASensorEvent*`}
189 fun x
: Float `{return recv->magnetic.x;`}
190 fun y: Float `{return recv->magnetic.y;`}
191 fun z
: Float `{ return recv->magnetic.z;`}
194 # Sensor event returned by the gyroscope sensor
195 extern class ASensorGyroscope `{ASensorEvent*`}
198 fun x
: Float `{return recv->vector.x;`}
199 fun y: Float `{return recv->vector.y;`}
200 fun z
: Float `{return recv->vector.y;`}
203 # Sensor event returned by the Light sensor
204 extern class ASensorLight `{ASensorEvent*`}
207 fun light
: Float `{return recv->light;`}
210 # sensor event returned by the Proximity Sensor
211 extern class ASensorProximity `{ASensorEvent*`}
214 fun distance
: Float `{return recv->distance;`}
217 # Array of SensorEvents
218 extern class ASensorEvents `{ASensorEvent*`}
220 new (length
: Int) `{return malloc(sizeof(ASensorEvent)*length);`}
222 fun [](index: Int): ASensorEvent `{