# See the License for the specific language governing permissions and
# limitations under the License.
-# This module is used to manipulate android sensors
-# The sensor support is implemented in android_app module, so the user can enable the type of sensor he wants to use.
-# There is an example of how you can use the android sensors in nit/examples/mnit_ballz :
+# Access Android sensors
+#
+# Sensors are to be enabled when `App` is created.
+# The following example enables all sensors.
+# The events (`SensorEvent`, `ASensorAccelerometer`, `ASensorMagneticField`...)
+# are sent to the `input` callback of `App`
#
# ~~~~nitish
-# #FIXME rewrite the example
-# var app = new MyApp
-# app.sensors_support_enabled = true
-# app.accelerometer.enabled = true
-# app.accelerometer.eventrate = 10000
-# app.magnetic_field.enabled = true
-# app.gyroscope.enabled = true
-# app.light.enabled = true
-# app.proximity.enabled = true
-# app.main_loop
+# redef class App
+# init
+# do
+# sensors_support_enabled = true
+# accelerometer.enabled = true
+# accelerometer.eventrate = 10000
+# magnetic_field.enabled = true
+# gyroscope.enabled = true
+# light.enabled = true
+# proximity.enabled = true
+# end
+# end
# ~~~~
-#
-# In this example, we enable the sensor support, then enable all types of sensors supported, before running the app.
-# The result is you get all type of SensorEvent (ASensorAccelerometer, ASensorMagneticField ...) in the input method of your app
module sensors
-import android
+import game
import mnit
in "C header" `{
extern class ASensor `{ASensorRef`}
new `{return malloc(sizeof(ASensorRef));`}
- fun name: NativeString `{return (char*)ASensor_getName(self);`}
- fun vendor: NativeString `{return (char*)ASensor_getVendor(self);`}
+ fun name: CString `{return (char*)ASensor_getName(self);`}
+ fun vendor: CString `{return (char*)ASensor_getVendor(self);`}
fun sensor_type: ASensorType `{return ASensor_getType(self);`}
fun resolution: Float `{return ASensor_getResolution(self);`}
fun min_delay: Int `{return ASensor_getMinDelay(self);`}
private fun enable_accelerometer
do
accelerometer.asensor = sensormanager.get_default_sensor(new ASensorType.accelerometer)
- if accelerometer.asensor.address_is_null then
- print "Accelerometer sensor unavailable"
+ if accelerometer.asensor.address_is_null then
+ print "Accelerometer sensor unavailable"
else
if eventqueue.enable_sensor(accelerometer.asensor) < 0 then print "Accelerometer enabling failed"
eventqueue.set_event_rate(accelerometer.asensor, accelerometer.event_rate)
private fun enable_proximity
do
proximity.asensor = sensormanager.get_default_sensor(new ASensorType.proximity)
- if proximity.asensor.address_is_null then
+ if proximity.asensor.address_is_null then
print "Proximity sensor unavailable"
else
if eventqueue.enable_sensor(proximity.asensor) < 0 then print "Proximity enabling failed"