Accelerometer

The accelerometer measures the acceleration (including the force of gravity) applied to the device. This sensor is particularly useful if your application is designed to handle motion input from a user. For example, in a racing game, the car's direction could be controlled by the user tilting the device from side-to-side.

Acceleration values are reported in meters per second per second (m/s2).

A BPS event with the code SENSOR_ACCELEROMETER_READING is generated when new accelerometer values are available. You can call the function sensor_event_get_xyz() to retrieve the acceleration values from the event.

BlackBerry device showing the positive and negative x, y, and z axes of the sensor coordinates.

When the device is lying on a flat surface in its natural orientation (e.g., the BlackBerry 10 logo is right-side-up and facing the user), the following are true:
  • Pushing the device to the right results in a positive acceleration value along the x-axis.
  • Pushing the device away from the user results in a positive acceleration value along the y-axis.
  • Pushing the device directly off the flat surface and into the air results in a positive acceleration along the z-axis. This acceleration includes the acceleration caused by the movement off the surface (accelup), minus the force of gravity. That is, accelz = accelup - (-9.81 m/s2) = accelup + 9.81 m/s2. At rest, this means that the acceleration along the z-axis of the device is 9.81 m/s2.

The code sample below demonstrates how you can use the accelerometer sensor in your app:

#include <stddef.h>
#include <bps/bps.h>
#include <bps/sensor.h>

int main(void)
{
    int event_domain;
    unsigned int event_code;

    // Create variables to store an accelerometer sensor reading.
    float x_accel, y_accel, z_accel;

    bps_initialize();

    if (sensor_is_supported(SENSOR_TYPE_ACCELEROMETER))
    {
        sensor_request_events(SENSOR_TYPE_ACCELEROMETER);
    }

    while (1)
    {
        bps_event_t *current_event = NULL;
        bps_get_event(&current_event, -1);

        event_domain = bps_event_get_domain(current_event);
        event_code = bps_event_get_code(current_event);

        if (event_domain == sensor_get_domain())
        {
            switch (event_code)
            {
                case SENSOR_ACCELEROMETER_READING:
                    // Get the current acceleration values.
                    sensor_event_get_xyz(current_event, 
                                         &x_accel, 
                                         &y_accel, 
                                         &z_accel);
                    // Update program based on current
                    // acceleration values.
                    break;

                default:
                    break;
            }
        }
    }

    // Program cleanup
    sensor_stop_events(SENSOR_TYPE_ACCELEROMETER);
    bps_shutdown();
    return 0;
}                
            

Last modified: 2013-12-21



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus