Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Super Contributor
lyon819
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.
Accepted Solution

How to show acceleration on screen with continuous update...

The output shows acceleration, but cannot continuous update.

Whenever narrow screen down or make screen to be full-size screen, the data will update once.

 

The code is at below.

If you know where is wrong, pls point out.

 

Thank you,

 

 

#include <bps/accelerometer.h>
#include <bps/bps.h>
#include <bps/navigator.h>
#include <screen/screen.h>
#include <stdio.h>
#include <stdlib.h>
#include <bps/dialog.h>
#include <string.h>
#include <errno.h>

/**
* Milliseconds between accelerometer reads. This is the same
* rate at which the accelerometer data will be updated from
* hardware. The hardware update rate is set below using
* accelerometer_set_update_frequency(FREQ_40_HZ).
*/
static const int ACCELEROMETER_MAX_POLL_INTERVAL = 25;


static screen_context_t screen_ctx;
static screen_window_t screen_win;
dialog_instance_t main_dialog = 0;
/*
* The accelerometer forces
*/
double force_x, force_y, force_z;

/**
* Use the PID to set the window group id.
*/
static char *
get_window_group_id()
{
static char s_window_group_id[16] = "";

if (s_window_group_id[0] == '\0') {
snprintf(s_window_group_id, sizeof(s_window_group_id), "%d", getpid());
}

return s_window_group_id;
}

/**
* Set up a basic screen, so that the navigator will
* send window state events when the window state changes.
*
* @return @c EXIT_SUCCESS or @c EXIT_FAILURE
*/
static int
setup_screen()
{
if (screen_create_context(&screen_ctx, SCREEN_APPLICATION_CONTEXT) != 0) {
return EXIT_FAILURE;
}

if (screen_create_window(&screen_win, screen_ctx) != 0) {
screen_destroy_context(screen_ctx);
return EXIT_FAILURE;
}

int usage = SCREEN_USAGE_NATIVE;
if (screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_USAGE, &usage) != 0) goto fail;

if (screen_create_window_buffers(screen_win, 1) != 0) goto fail;

if (screen_create_window_group(screen_win, get_window_group_id()) != 0) goto fail;

screen_buffer_t buff;
if (screen_get_window_property_pv(screen_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void*)&buff) != 0) goto fail;

int buffer_size[2];
if (screen_get_buffer_property_iv(buff, SCREEN_PROPERTY_BUFFER_SIZE, buffer_size) != 0) goto fail;

int attribs[1] = {SCREEN_BLIT_END};
if (screen_fill(screen_ctx, buff, attribs) != 0) goto fail;

int dirty_rects[4] = {0, 0, buffer_size[0], buffer_size[1]};
if (screen_post_window(screen_win, buff, 1, (const int*)dirty_rects, 0) != 0) goto fail;

return EXIT_SUCCESS;

fail:
screen_destroy_window(screen_win);
screen_destroy_context(screen_ctx);
return EXIT_FAILURE;
}

/**
* Rotates the screen to the specified angle.
*
* @param angle the angle to rotate the screen to. Must be 0, 90, 180 or 270.
*
* @return @c EXIT_SUCCESS upon succes, else @c EXIT_FAILURE
*/
static int
rotate_screen(int angle)
{
if ((angle != 0) && (angle != 90) && (angle != 180) && (angle != 270)) {
fprintf(stderr, "Invalid angle\n");
return EXIT_FAILURE;
}

int rc;
int rotation;
rc = screen_get_window_property_iv(screen_win, SCREEN_PROPERTY_ROTATION, &rotation);
if (rc != 0) {
fprintf(stderr, "screen error getting window rotation: %d\n", rc);
return EXIT_FAILURE;
}

int size[2];
rc = screen_get_window_property_iv(screen_win, SCREEN_PROPERTY_BUFFER_SIZE, size);
if (rc != 0) {
fprintf(stderr, "screen error getting window buffer size: %d\n", rc);
return EXIT_FAILURE;
}

int temp;
switch (angle - rotation) {
case -270:
case -90:
case 90:
case 270:
temp = size[0];
size[0] = size[1];
size[1] = temp;
break;
default:
break;
}

rc = screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_ROTATION, &angle);
if (rc != 0) {
fprintf(stderr, "screen error setting window rotation: %d\n", rc);
return EXIT_FAILURE;
}

rc = screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_SIZE, size);
if (rc != 0) {
fprintf(stderr, "screen error setting window size: %d\n", rc);
return EXIT_FAILURE;
}

rc = screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_SOURCE_SIZE, size);
if (rc != 0) {
fprintf(stderr, "screen error setting window size: %d\n", rc);
return EXIT_FAILURE;
}

rc = screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_BUFFER_SIZE, size);
if (rc != 0) {
fprintf(stderr, "screen error setting window buffer size: %d\n", rc);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

/**
* Handle a navigator event.
*
* @return @c true if application should exit, else false.
*/
static bool
handle_navigator_event(bps_event_t *event)
{
bool should_exit = false;

switch (bps_event_get_code(event)) {
case NAVIGATOR_EXIT:
should_exit = true;
break;
case NAVIGATOR_ORIENTATION_CHECK:
navigator_orientation_check_response(event, true);
break;
case NAVIGATOR_ORIENTATION:
{
int angle = navigator_event_get_orientation_angle(event);
if (rotate_screen(angle) == EXIT_FAILURE) {
should_exit = true;
}
navigator_done_orientation(event);
break;
}
}

return should_exit;
}
/**
* Show an alert dialog that will contain the geolocation data.
*/
static void
show_main_dialog()
{
if (main_dialog) {
return;
}

dialog_create_alert(&main_dialog);
dialog_set_alert_message_text(main_dialog, "Acceleration getting first fix");
dialog_set_size(main_dialog, DIALOG_SIZE_FULL);
dialog_set_group_id(main_dialog, get_window_group_id());
dialog_set_cancel_required(main_dialog, true);
dialog_show(main_dialog);
}

/**
* Displays geolocation data in the main dialog.
*/
static void
display_acceleration_data(double force_x, double force_y, double force_z){
char buf[1024];
snprintf(buf, sizeof buf,
"\tX: % 7.3f m\n"
"\tY: % 7.3f m\n"
"\tZ: % 7.3f m\n",
force_x, force_y, force_z);

dialog_set_alert_message_text(main_dialog, buf);
dialog_update(main_dialog);
}


/**
* 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[])
{
bool exit_application = false;

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

/*
* Initialize the screen so that the window group Id is properly set, to allow
* the dialogs to be displayed.
*/
if (setup_screen() != EXIT_SUCCESS) {
fprintf(stderr, "Unable to initialize screen.");
exit(-1);
}

/*
* 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 that we can track when
* the system is terminating the application (NAVIGATOR_EXIT event).
*
* We request dialog events so we can be notified when the dialog service
* responds to our requests/queries.
*
* We request geolocation events so that we can be notified of our current
* geolocation.
*/
if (BPS_SUCCESS != navigator_request_events(0)) {
fprintf(stderr, "Error requesting navigator events: %s", strerror(errno));
exit(-1);
}
if (BPS_SUCCESS != dialog_request_events(0)) {
fprintf(stderr, "Error requesting dialog events: %s", strerror(errno));
exit(-1);
}

/*
* Create and display the dialog that will show the geolocation data.
*/
show_main_dialog();

/*
* 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) {
/*
* 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);
// printf("X: % 7.3f m/s2 Y: % 7.3f m/s2 Z: % 7.3f m/s2\n", force_x, force_y, force_z);
//display_acceleration_data(force_x, force_y, force_z);

char buf[80];

//different style to show text
snprintf(buf, sizeof buf,"X: % 7.3f m/s2 Y: % 7.3f m/s2 Z: % 7.3f m/s2\n", force_x, force_y, force_z);

/*
snprintf(buf, sizeof buf,
"\tX: % 7.3f m/s2\n"
"\tY: % 7.3f m/s2\n"
"\tZ: % 7.3f m/s2\n",
force_x, force_y, force_z);
*/
//fprintf(stderr, buf);
dialog_set_alert_message_text(main_dialog, buf);
dialog_update(main_dialog);
}
else if (bps_event_get_domain(event) == navigator_get_domain()) {
exit_application = handle_navigator_event(event);
}
/* If it is a dialog event, determine the response code and handle
* the event accordingly.
*/
else if (bps_event_get_domain(event) == dialog_get_domain()) {
/* We have no buttons so we don't need to do anything. */
;
}
}
}
/*
* Destroy the dialog, if it exists.
*/
if (main_dialog) {
dialog_destroy(main_dialog);
}

/*
* Clean up the bps infrastructure and exit
*/
bps_shutdown();
screen_destroy_window(screen_win);
screen_destroy_context(screen_ctx);
return 0;
}

-Fight with BB10
BlackBerry Development Advisor
anhu
Posts: 54
Registered: ‎10-26-2011
My Device: Playbook

Re: How to show acceleration on screen with continuous update...

Hi, 

 

At first glance, I think you're missing a call to request sensor events; specifically, you want to request accelerometer events from the sensor service. Please see the accelerometer sample and search for the word "request". 

 

Please let me know if this helps. 

BlackBerry Development Advisor
anhu
Posts: 54
Registered: ‎10-26-2011
My Device: Playbook

Re: How to show acceleration on screen with continuous update...

....more specifically, the accelerometer service is deprecated.  Please see bps/accelerometer.h header file. 

 

You should be using the sensor API which includes accelerometer data.  

 

Please see the accelerometer sample app that can be found at github. 

 

Thanks, 

Anthony 

 

Super Contributor
lyon819
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.

Re: How to show acceleration on screen with continuous update...

add request at below, but the situation is the same.

 

 navigator_request_events(0);

 

When I run the program with console of Native SDK, acceleraion will continuously update.

However,

when I run the program on the screen of Playbook, acceleration will not update.

need to narrow or expand the screen, acceleration will update.

 

 

-Fight with BB10
BlackBerry Development Advisor
anhu
Posts: 54
Registered: ‎10-26-2011
My Device: Playbook

Re: How to show acceleration on screen with continuous update...

So, we've had a closer look at your code.  

 

You check for whether the event is NULL and if not, only then do you proceed to query the accelerometer.  

 

What you should be doing is querying for the accelerometer regardless of whether event is NULL. 

 

Note that you set an event timeout in your query for bps events. 

When bps_get_event returns because of a timeout, event will be NULL.

At this point you can read accelerometer forces regardless of whether the event is NULL or not. 

 

Hope this helps....

Anthony 

 

 

 

Super Contributor
lyon819
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.

Re: How to show acceleration on screen with continuous update...

Tried to check event ==NULL or not,

or move loop sequency.

However, the result is the same.

Whenever acceleration shows on console of native SDK, acceleration will continuously update.

But,

When acceleration shows on dialog of playbook, acceleration will not update.

 

 

-Fight with BB10
BlackBerry Development Advisor
anhu
Posts: 54
Registered: ‎10-26-2011
My Device: Playbook

Re: How to show acceleration on screen with continuous update...

You're close if you're getting the console to continuously update. 

For dialogs, are you sure youre calling dialog_set_alert_message_text() and then calling dialog_update() ? 

BlackBerry Development Advisor
anhu
Posts: 54
Registered: ‎10-26-2011
My Device: Playbook

Re: How to show acceleration on screen with continuous update...

I'd like to re-iterate my original suggestion of starting with the Accelerometer sample that can be found on the BlackBerry section of GitHub. It uses the sensor's API and is quite easy to follow. Accelerometer.h is a deprecated header file.
Super Contributor
lyon819
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.

Re: How to show acceleration on screen with continuous update...

This is the code, tried to follow it.

https://github.com/blackberry/NDK-Samples/blob/master/AccelerometerSample/main.c

 

But, I still could not find out why acceleration will not update when program run on playbook??

Even bypass dialog function, I tried to use fprintf to record accelerations.

But, result is the same.

 

 

-Fight with BB10
BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer

Re: How to show acceleration on screen with continuous update...

[ Edited ]

I just compiled and ran the accelerometer sample code you linked to on github.

 

from command-line:

# ./acceltest
Pitch: -5.142555 deg  Roll: 3.798700 deg
Pitch: -5.543998 deg  Roll: 3.766752 deg
Pitch: -5.996861 deg  Roll: 3.636749 deg
Pitch: -5.871497 deg  Roll: 3.692958 deg
Pitch: -5.342711 deg  Roll: 4.004488 deg
Pitch: -5.301732 deg  Roll: 3.973812 deg

.....

 

If packaged as an app, installed, and run from the navigator, the stdout readings were correctly output to the appdata logs/log file.

 

What OS version are you running on your PlayBook?

 

Cheers,

Sean