# HG changeset patch # User John Tsiombikas # Date 1434424679 -10800 # Node ID 2712c5da2e00bc7bfb64445236563d39b6232e25 # Parent a94af102872fa8d759b1c13b64c35016cf0b0bf0 getting sensor input (hack) diff -r a94af102872f -r 2712c5da2e00 android/Makefile --- a/android/Makefile Sat Jun 13 05:35:43 2015 +0300 +++ b/android/Makefile Tue Jun 16 06:17:59 2015 +0300 @@ -66,7 +66,7 @@ rm -f $(obj) $(lib) $(apk-release) $(apk-debug) $(ant_files) rm -rf $(ant_dirs) rm -f assets/data assets/sdr - rmdir assets + [ -d assets ] && rmdir assets || true .PHONY: install install: install-debug diff -r a94af102872f -r 2712c5da2e00 src/android/amain.c --- a/src/android/amain.c Sat Jun 13 05:35:43 2015 +0300 +++ b/src/android/amain.c Tue Jun 16 06:17:59 2015 +0300 @@ -5,11 +5,15 @@ #include #include "amain.h" #include "native_glue.h" +#include +#include "sensordef.h" #include "logger.h" #include "game.h" #include "camera.h" #include "timer.h" +#define EVID_SENS_ACCEL (LOOPER_ID_USER + 0) +#define EVID_SENS_ROT (LOOPER_ID_USER + 1) static void handle_command(struct android_app *app, int32_t cmd); static int handle_input(struct android_app *app, AInputEvent *ev); @@ -21,6 +25,8 @@ static EGLSurface surf; static EGLContext ctx; +static int redisp_pending = 1; /* TODO stop busy-looping */ + static int width, height; static int init_done; @@ -28,7 +34,11 @@ static JNIEnv *jni; static jclass activity_class; - +static ASensorManager *sensorman; +static ASensor const *sens_accel; +static ASensor const *sens_rot; +static ASensorEventQueue *sens_evq_accel; +static ASensorEventQueue *sens_evq_rot; void android_main(struct android_app *app_ptr) { @@ -47,13 +57,96 @@ } activity_class = (*jni)->GetObjectClass(jni, app->activity->clazz); + { + int i, num_sensors; + ASensorList sensors; + + sensorman = ASensorManager_getInstance(); + num_sensors = ASensorManager_getSensorList(sensorman, &sensors); + printf("found %d sensors:\n", num_sensors); + + for(i=0; ilooper, + EVID_SENS_ACCEL, 0, 0))) { + fprintf(stderr, "failed to create acceleration sensor event queue\n"); + return; + } + + if(!(sens_evq_rot = ASensorManager_createEventQueue(sensorman, app->looper, + EVID_SENS_ROT, 0, 0))) { + fprintf(stderr, "failed to create rotation sensor event queue\n"); + return; + } + } + + for(;;) { - int num_events; + int num_events, evid; struct android_poll_source *pollsrc; + ASensorEvent sens_ev; - while(ALooper_pollAll(0, 0, &num_events, (void**)&pollsrc) >= 0) { - if(pollsrc) { - pollsrc->process(app, pollsrc); + while((evid = ALooper_pollAll(redisp_pending ? 0 : -1, 0, &num_events, (void**)&pollsrc)) >= 0) { + + switch(evid) { + case EVID_SENS_ACCEL: + if(sens_accel) { + float accel[3]; + while(ASensorEventQueue_getEvents(sens_evq_accel, &sens_ev, 1) > 0) { + accel[0] = sens_ev.acceleration.x; + accel[1] = sens_ev.acceleration.y; + accel[2] = sens_ev.acceleration.z; + } + + // TODO: integrate over time + game_6dof_translation(accel[0], accel[1], accel[2]); + } + break; + + case EVID_SENS_ROT: + if(sens_rot) { + float qrot[4]; + while(ASensorEventQueue_getEvents(sens_evq_rot, &sens_ev, 1) > 0) { + int i; + for(i=0; i<4; i++) { + qrot[i] = sens_ev.data[i]; + } + } + + game_6dof_rotation(qrot[0], qrot[1], qrot[2], qrot[3]); + } + break; + + default: + if(pollsrc) { + pollsrc->process(app, pollsrc); + } } } @@ -62,7 +155,7 @@ return; } - if(init_done) { + if(init_done && redisp_pending) { game_display(get_time_msec()); eglSwapBuffers(dpy, surf); } @@ -115,11 +208,27 @@ case APP_CMD_GAINED_FOCUS: /* app focused */ + printf("Command: APP_CMD_GAINED_FOCUS\n"); + if(sens_accel) { + ASensorEventQueue_enableSensor(sens_evq_accel, sens_accel); + ASensorEventQueue_setEventRate(sens_evq_accel, sens_accel, 1000000 / 60); + } + if(sens_rot) { + ASensorEventQueue_enableSensor(sens_evq_rot, sens_rot); + ASensorEventQueue_setEventRate(sens_evq_rot, sens_rot, 1000000 / 60); + } //cam_start_video(); break; case APP_CMD_LOST_FOCUS: /* app lost focus */ + printf("Command: APP_CMD_LOST_FOCUS\n"); + if(sens_accel) { + ASensorEventQueue_disableSensor(sens_evq_accel, sens_accel); + } + if(sens_rot) { + ASensorEventQueue_disableSensor(sens_evq_rot, sens_rot); + } //cam_stop_video(); break; diff -r a94af102872f -r 2712c5da2e00 src/android/sensordef.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/android/sensordef.c Tue Jun 16 06:17:59 2015 +0300 @@ -0,0 +1,41 @@ +#include "sensordef.h" + +static const char *names[] = { + "unknown", + "accelerometer", + "magnetic field", + "orientation", + "gyroscope", + "light", + "pressure", + "temperature", + "proximity", + "gravity", + "linear acceleration", + "rotation vector", + "relative humidity", + "ambient temperature", + "magnetic field (uncalibrated)", + "game rotation vector", + "gyroscope (uncalibrated)", + "significant motion", + "step detector", + "step counter", + "geomagnetic rotation", + "heart rate", + "tilt detector", + "wake gesture", + "glance gesture", + "pick up gesture", + "wrist tilt gesture" +}; + +#define NUM_SENSOR_TYPES (sizeof names / sizeof *names) + +const char *sensor_typestr(int type) +{ + if(type < 0 || type >= NUM_SENSOR_TYPES) { + type = 0; + } + return names[type]; +} diff -r a94af102872f -r 2712c5da2e00 src/android/sensordef.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/android/sensordef.h Tue Jun 16 06:17:59 2015 +0300 @@ -0,0 +1,47 @@ +#ifndef SENSORDEF_H_ +#define SENSORDEF_H_ + +#include + +/* the sensor.h header file in NDK 10d is missing a lot of sensor types, + * here is the full list. + */ +#define SENSOR_TYPE_ACCELEROMETER 1 +#define SENSOR_TYPE_MAGNETIC_FIELD 2 +#define SENSOR_TYPE_ORIENTATION 3 +#define SENSOR_TYPE_GYROSCOPE 4 +#define SENSOR_TYPE_LIGHT 5 +#define SENSOR_TYPE_PRESSURE 6 +#define SENSOR_TYPE_TEMPERATURE 7 +#define SENSOR_TYPE_PROXIMITY 8 +#define SENSOR_TYPE_GRAVITY 9 +#define SENSOR_TYPE_LINEAR_ACCELERATION 10 +#define SENSOR_TYPE_ROTATION_VECTOR 11 +#define SENSOR_TYPE_RELATIVE_HUMIDITY 12 +#define SENSOR_TYPE_AMBIENT_TEMPERATURE 13 +#define SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED 14 +#define SENSOR_TYPE_GAME_ROTATION_VECTOR 15 +#define SENSOR_TYPE_GYROSCOPE_UNCALIBRATED 16 +#define SENSOR_TYPE_SIGNIFICANT_MOTION 17 +#define SENSOR_TYPE_STEP_DETECTOR 18 +#define SENSOR_TYPE_STEP_COUNTER 19 +#define SENSOR_TYPE_GEOMAGNETIC_ROTATION 20 +#define SENSOR_TYPE_HEART_RATE 21 +#define SENSOR_TYPE_TILT_DETECTOR 22 +#define SENSOR_TYPE_WAKE_GESTURE 23 +#define SENSOR_TYPE_GLANCE_GESTURE 24 +#define SENSOR_TYPE_PICK_UP_GESTURE 25 +#define SENSOR_TYPE_WRIST_TILT_GESTURE 26 + +#ifdef __cplusplus +extern "C" { +#endif + +const char *sensor_typestr(int type); + +#ifdef __cplusplus +} +#endif + + +#endif /* SENSORDEF_H_ */ diff -r a94af102872f -r 2712c5da2e00 src/game.cc --- a/src/game.cc Sat Jun 13 05:35:43 2015 +0300 +++ b/src/game.cc Tue Jun 16 06:17:59 2015 +0300 @@ -9,6 +9,7 @@ #include "texture.h" #include "shader.h" #include "text.h" +#include "vmath/vmath.h" static void draw_quad(float hsz, float vsz); @@ -21,6 +22,9 @@ static SdrProg *sdr_cam, *sdr_tex; +static Quaternion qrot; +static Vector3 trans; + extern "C" int game_init(void) { //glEnable(GL_DEPTH_TEST); @@ -109,12 +113,12 @@ gl_load_identity(); - // draw some text + // print the rotation quaternion text_color(1, 1, 1, 1); - for(int i=0; i<25; i++) { - text_position(0, i); - text_printf("%d%s line of text", i, i == 1 ? "st" : (i == 2 ? "nd" : (i == 3 ? "rd" : "th"))); - } + text_position(0, 0); + text_printf("translation (% .3f, % .3f, % .3f)", trans.x, trans.y, trans.z); + text_position(0, 1); + text_printf("Rotation quat ([% 1.3f, % 1.3f, % 1.3f], % 1.3f)", qrot.v.x, qrot.v.y, qrot.v.z, qrot.s); } static void draw_quad(float hsz, float vsz) @@ -217,3 +221,17 @@ */ } +void game_6dof_translation(float dx, float dy, float dz) +{ + trans.x = dx; + trans.y = dy; + trans.z = dz; +} + +void game_6dof_rotation(float qx, float qy, float qz, float qw) +{ + qrot.v.x = qx; + qrot.v.y = qy; + qrot.v.z = qz; + qrot.s = qw; +} diff -r a94af102872f -r 2712c5da2e00 src/game.h --- a/src/game.h Sat Jun 13 05:35:43 2015 +0300 +++ b/src/game.h Tue Jun 16 06:17:59 2015 +0300 @@ -17,6 +17,9 @@ void game_mouse_button(int id, int bn, int press, int x, int y); void game_mouse_motion(int id, int x, int y); +void game_6dof_translation(float dx, float dy, float dz); +void game_6dof_rotation(float qx, float qy, float qz, float qw); + /* provided by the system frontend */ void set_mouse_pos(int x, int y); void set_mouse_cursor(int enable); diff -r a94af102872f -r 2712c5da2e00 src/sdr.c --- a/src/sdr.c Sat Jun 13 05:35:43 2015 +0300 +++ b/src/sdr.c Tue Jun 16 06:17:59 2015 +0300 @@ -149,7 +149,6 @@ filesize = ass_fseek(fp, 0, SEEK_END); /*filesize = ass_ftell(fp);*/ - printf(" filesize: %ld\n", filesize); ass_fseek(fp, 0, SEEK_SET); if(!(src = malloc(filesize + 1))) {