Example #1
0
/**
 * 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;


}
Example #2
0
/**
 * A sample application demonstrates the BlackBerry Native APIs for accelerometer.
 * The sample initializes and reads the accelerometer periodically until a
 * NAVIGATOR_EXIT event is received.
 * The application also listens for window state changes from the navigator so that
 * it can stop reading the accelerometer when the application is no longer visible.
 */
int
main(int argc, char *argv[])
{
    if (setup_screen() != EXIT_SUCCESS) {
        printf("Unable to set up the screen. Exiting.");
        return 0;
    }

    /*
     * The accelerometer forces
     */
    double force_x, force_y, force_z;

    /*
     * Before we can listen for events from the BlackBerry Tablet OS platform
     * services, we need to initialize the BPS infrastructure
     */
    bps_initialize();

    /*
     * Once the BPS infrastructure has been initialized we can register for
     * events from the various BlackBerry Tablet OS platform services. The
     * Navigator service manages and delivers application life cycle and
     * visibility events.
     * For this sample, we request Navigator events so we can track when
     * the system is terminating the application (NAVIGATOR_EXIT event). This allows
     * us to clean up application resources.
     */
    navigator_request_events(0);

    /*
     * Before initializing the accelerometer service we must ensure the device
     * supports it
     */
    if (!accelerometer_is_supported()) {
        /*
         * If the device does not support accelerometer then notify the user,
         * clean up and exit
         */
        printf("Accelerometer not supported by device!");
        bps_shutdown();
        return EXIT_FAILURE;
    }

    /*
     * Initialize the accelerometer by setting the rates at which the
     * accelerometer values will be updated from hardware
     */
    accelerometer_set_update_frequency(FREQ_40_HZ);

   /*
    * Process Navigator events and take accelerometer readings periodically
    * until we receive a NAVIGATOR_EXIT event.
    */
    int exit_application = 0;
    int paused = 0;
    while (!exit_application) {
        /*
         * By setting the bps_get_event timeout to ACCELEROMETER_MAX_POLL_INTERVAL,
         * we assign the maximum time (in millis) that we will wait before
         * unblocking so we can take an accelerometer reading.
         */
        bps_event_t *event = NULL;
        bps_get_event(&event, ACCELEROMETER_MAX_POLL_INTERVAL);

        if (event) {
            if (bps_event_get_domain(event) == navigator_get_domain()) {
                /*
                 * If it is a NAVIGATOR_EXIT event then set the
                 * exit_application flag so the application will stop
                 * processing events, clean up and exit.
                 *
                 * If it is a NAVIGATOR_WINDOW_STATE event, and the window
                 * state is NAVIGATOR_WINDOW_INVISIBLE, set paused to true so
                 * that we stop collecting accelerometer data. Otherwise, if
                 * we're currently paused, then set paused back to false to
                 * resume collecting accelerometer data.
                 */
                unsigned int event_code = bps_event_get_code(event);
                if (NAVIGATOR_EXIT == event_code) {
                    exit_application = 1;
                } else if (NAVIGATOR_WINDOW_STATE == event_code) {
                    if (navigator_event_get_window_state(event) == NAVIGATOR_WINDOW_FULLSCREEN) {
                        /*
                         * The application is now full screen.
                         * Resume reading the accelerometer.
                         */
                        paused = 0;
                        printf("Resuming accelerometer reads.\n");
                    } else if (!paused){
                        /*
                         * The application has become thumbnailed or invisible.
                         * If it is not already paused, then pause it.
                         * Otherwise, ignore the window state change.
                         */
                        paused = 1;
                        printf("Resuming accelerometer reads.\n");
                    }
                }
            }
            /*
             * Remember to destroy any delivered events as we need to trigger
             * the release of any event resources when we are done processing
             * it.
             */
            bps_event_destroy(event);
        } else {
            /*
             * We've woken up. See if we are in the paused state. If not,
             * take an accelerometer reading
             */
            if (!paused) {
                accelerometer_read_forces(&force_x, &force_y, &force_z);
                display_accelerometer_reading(
                        ACCELEROMETER_CALCULATE_PITCH(force_x, force_y, force_z),
                        ACCELEROMETER_CALCULATE_ROLL(force_x, force_y, force_z));
            }
        }
    }

    /*
     * Clean up screen resources
     */
    screen_destroy_window(screen_win);
    screen_destroy_context(screen_ctx);

    /*
     * Clean up the BPS infrastructure and exit
     */
    bps_shutdown();
    return 0;
}