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 display data on the screen and save the data into a file in the same time?

The code can show acceleration on the screen of playbook.

But, when fprintf(f,"Accel X  (m/s2),Accel Y (m/s2),Accel Z (m/s2)\n") executes, debug shows no source available.

??

 

Does anyone know how to solve the issue, which writes data into a file?

 

The code is at below.

/*
 * Copyright (c) 2011 Research In Motion Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#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>
#include <bps/sensor.h>
#include <sys/platform.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;

int paused = 0;

//The accelerometer forces
float force_x, force_y, force_z;

//file elements
int _logcounter=0;
char fullname[256];
FILE* f;

/**
 * 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(float force_x, float force_y, float 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);
}

void createafile(){
    sprintf(fullname, "shared/documents/Raw-%d.txt",_logcounter);
    if(f==NULL){
        //file not exist
        f = fopen(fullname, "r");
        while(f!=NULL){
            //file exists
            fclose(f);
            ++_logcounter;
            sprintf(fullname, "shared/documents/Raw-%d.txt",_logcounter);
            f = fopen(fullname, "r");
        }
    }
}

//write data into file
void writedataintofile(){
    f = fopen(fullname, "w");
    fprintf(f,"Accel X  (m/s2),Accel Y (m/s2),Accel Z (m/s2)\n");
    fprintf(f,"%7.3f, %7.3f, %7.3f\n",force_x, force_y, force_z);
    fclose(f);
}

/**
 * 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();
    /*
     * 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);
    dialog_request_events(0);
    /*
     * 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.
     */
    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 accelerometer data.
     */
    show_main_dialog();
    /*
     * Before initializing the accelerometer service we must ensure the device
     * supports it
     */
    if (sensor_is_supported(SENSOR_TYPE_ACCELEROMETER)) {
        /*
         * If the device does not support accelerometer then notify the user,
         * clean up and exit
         */
        static const int SENSOR_RATE = 40;
        sensor_set_rate(SENSOR_TYPE_ACCELEROMETER, SENSOR_RATE);
        sensor_request_events(SENSOR_TYPE_ACCELEROMETER);

    }
    /*
     * 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.
     */
    createafile();
    f = fopen(fullname, "w");

    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) == sensor_get_domain()) {
                /*
                 * We've woken up. See if we are in the paused state. If not,
                 * take an accelerometer reading
                 */
                if (!paused) {
                    sensor_event_get_xyz(event, &force_x, &force_y, &force_z);
                    //display_acceleration_data(force_x, force_y, force_z);
                    fprintf(f,"Accel X  (m/s2),Accel Y (m/s2),Accel Z (m/s2)\n");
                    fprintf(f,"%7.3f, %7.3f, %7.3f\n",force_x, force_y, force_z);
                }//paused
            }
            /* 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()) {
                ;
            }
            /*
            * If it is a NAVIGATOR_EXIT event then set the exit_application
            * flag so the application will stop processing events, clean up and
            * exit.
            */
            else if (bps_event_get_domain(event) == navigator_get_domain()) {
                exit_application = handle_navigator_event(event);
            }
        }//if event
    }//while
    /*
     * Destroy the dialog, if it exists.
     */
    if (main_dialog) {
        dialog_destroy(main_dialog);
    }

    fclose(f);

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


-Fight with BB10
Retired
cdamus
Posts: 45
Registered: ‎10-25-2010
My Device: Bold 9700

Re: How to display data on the screen and save the data into a file in the same time?

Hi,

As I replied on your other thread:

In order to write into the shared/documents directory, your application must request the "access_shared" action and must be granted it by the user. Make sure that you have

<action>access_shared</action>

in your bar-descriptor.xml.

Also, note that the shared documents folder may not be the best place to write application log data. There is a logs/ directory in the app's sandbox for this purpose, or the data/ directory if the information needs to persist. The shared/documents/ folder is intended for documents that the user creates and/or interacts with.

HTH,

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

Re: How to display data on the screen and save the data into a file in the same time?

Thank you very much.

 

I forget to check the bar file.

Thank your mention.

 

Great,

 

-Fight with BB10