From 56a903e91b589023b68e3a4961891e4945fca618 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 23 Nov 2008 21:00:45 +0000 Subject: Make it easier to add other accelerometer types --- src/accelerometers.c | 108 +++++++++++++++++++++++---------------------------- src/types.h | 29 +++++++------- 2 files changed, 62 insertions(+), 75 deletions(-) diff --git a/src/accelerometers.c b/src/accelerometers.c index 464134b..3440fae 100644 --- a/src/accelerometers.c +++ b/src/accelerometers.c @@ -51,70 +51,40 @@ AccelHandle *accelerometer_open() { AccelHandle *accel; + /* Initialise accelerometer data structure */ accel = malloc(sizeof(AccelHandle)); if ( accel == NULL ) return NULL; + accel->x = 0; + accel->y = 0; + accel->z = 0; + accel->lx = 0; + accel->ly = 0; + accel->lz = 0; + accel->type = ACCEL_UNKNOWN; + + /* Determine accelerometer type */ + accel->fh = fopen("/dev/input/event3", "rb"); + if ( accel->fh != NULL ) { + accel->type = ACCEL_FREERUNNER; + printf("Neo Freerunner detected\n"); + return accel; + } - accel->fh1 = fopen("/dev/input/event2", "rb"); - accel->fh2 = fopen("/dev/input/event3", "rb"); - accel->ax = 0; - accel->ay = 0; - accel->az = 0; - accel->bx = 0; - accel->by = 0; - accel->bz = 0; + /* Try other types here */ + fprintf(stderr, "Couldn't determine accelerometer type\n"); return accel; } -void accelerometer_update(AccelHandle *accel) { +void accelerometer_update_freerunner(AccelHandle *accel) { - if ( accel->fh1 != NULL ) { - - int i; - - for ( i=1; i<=4; i++ ) { - - struct input_event ev; - size_t rval; - - rval = fread(&ev, sizeof(struct input_event), 1, accel->fh1); - if ( rval != 1 ) { - fprintf(stderr, "Couldn't read accelerometer data"); - return; - } - - if ( ev.type == EV_REL ) { - if ( ev.code == REL_X ) { - accel->lax = ev.value; - } - if ( ev.code == REL_Y ) { - accel->lay = ev.value; - } - if ( ev.code == REL_Z ) { - accel->laz = ev.value; - } - } - - if ( ev.type == EV_SYN ) { - if ( ev.code == SYN_REPORT ) { - accel->ax = accel->lax; - accel->ay = accel->lay; - accel->az = accel->laz; - accel->lval = accel->ax + accel->ay; - } - } - - } - - } - - if ( accel->fh2 != NULL ) { + if ( accel->fh != NULL ) { struct input_event ev; size_t rval; - rval = fread(&ev, sizeof(struct input_event), 1, accel->fh2); + rval = fread(&ev, sizeof(struct input_event), 1, accel->fh); if ( rval != 1 ) { fprintf(stderr, "Couldn't read accelerometer data"); return; @@ -122,21 +92,21 @@ void accelerometer_update(AccelHandle *accel) { if ( ev.type == EV_REL ) { if ( ev.code == REL_X ) { - accel->lbx = ev.value; + accel->lx = ev.value; } if ( ev.code == REL_Y ) { - accel->lby = ev.value; + accel->ly = ev.value; } if ( ev.code == REL_Z ) { - accel->lbz = ev.value; + accel->lz = ev.value; } } if ( ev.type == EV_SYN ) { if ( ev.code == SYN_REPORT ) { - accel->bx = accel->lbx; - accel->by = accel->lby; - accel->bz = accel->lbz; + accel->x = accel->lx; + accel->y = accel->ly; + accel->z = accel->lz; } } } @@ -144,6 +114,21 @@ void accelerometer_update(AccelHandle *accel) { } +void accelerometer_update(AccelHandle *accel) { + + switch ( accel->type ) { + case ACCEL_UNKNOWN : { + return; + } + case ACCEL_FREERUNNER : { + accelerometer_update_freerunner(accel); + break; + } + /* Add other types here */ + } + +} + /* The accelerometer work thread */ static void *accel_work(void *data) { @@ -157,13 +142,16 @@ static void *accel_work(void *data) { accelerometer_update(accel); - if ( accel->lval > 1000 ) pos = 1000; - if ( (accel->lval < -1000) && (pos == 1000) ) { - pos = 0; + if ( (accel->y < -500) && (pos > -1000) ) { + pos = -1000; + audio_trigger_moo(); + } else if ( (accel->y > 500) && (pos < 1000) ) { + pos = 1000; audio_trigger_moo(); } usleep(25000); + } audio_shutdown(); diff --git a/src/types.h b/src/types.h index 7837ff2..2ec6aac 100644 --- a/src/types.h +++ b/src/types.h @@ -30,26 +30,25 @@ #include #include +typedef enum { + ACCEL_UNKNOWN, + ACCEL_FREERUNNER, /* Openmoko Neo Freerunner */ +} AccelType; + typedef struct { - FILE *fh1; - FILE *fh2; + FILE *fh; - int ax; - int ay; - int az; - int bx; - int by; - int bz; + AccelType type; - int lval; /* The "mooing" contribution */ + int x; + int y; + int z; - int lax; - int lay; - int laz; - int lbx; - int lby; - int lbz; + /* Temporary values for use during reading */ + int lx; + int ly; + int lz; } AccelHandle; -- cgit v1.2.3