void* eventLoop(void* data) { FREContext ctx = (FREContext) data; float x, y, z; bps_event_t *event = NULL; char buffer[256]; // Initialize BPS bps_initialize(); // Start the gyroscope if (sensor_request_events(SENSOR_TYPE_GYROSCOPE) != BPS_SUCCESS) { shutdown = true; } // Receive events through the event channel while (!shutdown) { event = NULL; if (bps_get_event(&event, 50) != BPS_SUCCESS) return NULL; if (event) { if (bps_event_get_domain(event) == sensor_get_domain()) { if (bps_event_get_code(event) == SENSOR_GYROSCOPE_READING) { if (sensor_event_get_xyz(event, &x, &y, &z) == BPS_SUCCESS) { sprintf(buffer, "%f&%f&%f", x, y, z); fprintf(stdout, "Sensor event: %f&%f&%f\n", x, y, z); fflush(stdout); if(ctx != NULL) { FREDispatchStatusEventAsync(ctx, (uint8_t*)"CHANGE", (uint8_t*)buffer); } } } } } } if (sensor_stop_events(SENSOR_TYPE_GYROSCOPE) != BPS_SUCCESS) { fprintf(stdout, "Unable to stop sensor\n"); fflush(stdout); } // Stop BPS bps_shutdown(); return NULL; }
/** * Thread that handles accelerometer events and * sends relevant information to a dialog. * * @param p Unused. */ static void * accel_main (void *p) { bool run = true; bps_event_t *event; float force_x, force_y, force_z; bps_initialize(); /* * Each thread that calls bps_initialize() will have its * own unique channel ID. Protect it inside a mutex and * condition variable to avoid race condition where main * thread tries to use it before we assign it. */ pthread_mutex_lock(&chidMutex); accel_chid = bps_channel_get_active(); pthread_cond_signal(&chidCond); pthread_mutex_unlock(&chidMutex); /* * Create and display a dialog that will show the data. */ create_bottom_dialog(); show_bottom_dialog_message("\n\nThis is the Accelerometer Dialog"); if (BPS_SUCCESS != sensor_request_events(SENSOR_TYPE_ACCELEROMETER)) { fprintf(stderr, "Error requesting sensor's accelerometer events: %s", strerror(errno)); bps_shutdown(); return NULL; } sensor_set_rate(SENSOR_TYPE_ACCELEROMETER, ACCELEROMETER_RATE); sensor_set_skip_duplicates(SENSOR_TYPE_ACCELEROMETER, true); while (run) { /* * Block, at the very least we'll get the "STOP" event */ bps_get_event(&event, -1); if (bps_event_get_domain(event) == local_event_domain) { if (bps_event_get_code(event) == STOP_REQUEST) { run = false; } } if (bps_event_get_domain(event) == sensor_get_domain()) { if (SENSOR_ACCELEROMETER_READING == bps_event_get_code(event)) { sensor_event_get_xyz(event, &force_x, &force_y, &force_z); display_accelerometer_reading(force_x, force_y, force_z); } } } sensor_stop_events(0); destroy_bottom_dialog(); bps_shutdown(); fprintf(stderr, "Exiting accelerometer thread\n"); return NULL; }