Example #1
0
void OSBB10::handle_accelerometer() {

	if (!accel_supported)
		return;

	if (!fullscreen)
		return;

	double force_x, force_y, force_z;
	accelerometer_read_forces(&force_x, &force_y, &force_z);
	Vector3 accel = Vector3(force_x, flip_accelerometer ? force_y : -force_y, force_z);
	input->set_accelerometer(accel);
	// rotate 90 degrees
	//input->set_accelerometer(Vector3(force_y, flip_accelerometer?force_x:(-force_x), force_z));
};
	void CCAccelerometer::update(long timeStamp)
	{
		if ( m_pAccelDelegate != NULL)
		{
			double x, y, z;

			accelerometer_read_forces(&x, &y, &z);

			m_accelerationValue.x = x;
			m_accelerationValue.y = y;
			m_accelerationValue.z = z;
			m_accelerationValue.timestamp = (double)timeStamp;

			m_pAccelDelegate->didAccelerate(&m_accelerationValue);
		}
	}
Example #3
0
	bool GetAcceleration (double &outX, double &outY, double &outZ) {
		
		int result = accelerometer_read_forces (&outX, &outY, &outZ);
		
		if (getenv ("FORCE_PORTRAIT") != NULL) {
			
			int cache = outX;
			outX = outY;
			outY = -cache;
			
		}
		
		outZ = -outZ;
		
		return (result == BPS_SUCCESS);
		//return (accelerometer_read_forces (&outX, &outY, &outZ) == BPS_SUCCESS);
		
	}
Example #4
0
void Platform::getAccelerometerValues(float* pitch, float* roll)
{
    double tx, ty, tz;
    accelerometer_read_forces(&tx, &ty, &tz);

    // Hack landscape adjustment only.
    if (__orientationAngle == 0)
    {
        tx = -tx;
        ty = -ty;
        tz = -tz;
    }

    if (pitch != NULL)
        *pitch = atan(ty / sqrt(tx * tx + tz * tz)) * 180.0f * M_1_PI;
    if (roll != NULL)
        *roll = atan(tx / sqrt(ty * ty + tz * tz)) * 180.0f * M_1_PI;
}
Example #5
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;
}