/* * accelerometers.c * * Accelerometer stuff * * (c) 2008 Thomas White * * This file is part of OpenMooCow - accelerometer moobox simulator * * OpenMooCow is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * OpenMooCow is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with OpenMooCow. If not, see . * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "types.h" #include "audio.h" struct input_event { struct timeval time; uint16_t type; uint16_t code; int32_t value; }; #define EV_SYN (0x00) #define EV_REL (0x02) #define SYN_REPORT (0x00) #define REL_X (0x00) #define REL_Y (0x01) #define REL_Z (0x02) AccelHandle *accelerometer_open() { AccelHandle *accel; accel = malloc(sizeof(AccelHandle)); if ( accel == NULL ) return NULL; 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; return accel; } void accelerometer_update(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 ) { struct input_event ev; size_t rval; rval = fread(&ev, sizeof(struct input_event), 1, accel->fh2); if ( rval != 1 ) { fprintf(stderr, "Couldn't read accelerometer data"); return; } if ( ev.type == EV_REL ) { if ( ev.code == REL_X ) { accel->lbx = ev.value; } if ( ev.code == REL_Y ) { accel->lby = ev.value; } if ( ev.code == REL_Z ) { accel->lbz = ev.value; } } if ( ev.type == EV_SYN ) { if ( ev.code == SYN_REPORT ) { accel->bx = accel->lbx; accel->by = accel->lby; accel->bz = accel->lbz; } } } } /* The accelerometer work thread */ static void *accel_work(void *data) { AccelHandle *accel; int pos = 0; accel = accelerometer_open(); audio_setup(); while ( 1 ) { accelerometer_update(accel); if ( accel->lval > 1000 ) pos = 1000; if ( (accel->lval < -1000) && (pos == 1000) ) { pos = 0; audio_trigger_moo(); } usleep(25000); } audio_shutdown(); return NULL; } void accelerometer_start() { GThread *work_thread; work_thread = g_thread_create(accel_work, NULL, TRUE, NULL); }