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
Contributor
Posts: 19
Registered: ‎06-21-2012
My Device: z10
My Carrier: Bell

About sensor function

Hello

I created an empty project and wrote thread function for my wo sensor reading, but i couldn't use

any sensor function in the project. It said undefined reference. I include all of these

#include <bps/sensor.h>
#include <bps/bps.h>
#include <bps/dialog.h>
#include <bps/navigator.h>
#include <screen/screen.h>

I dont know why I cannot get my code compiled. Deos anybody know the reason and solution as well? Below is part of my code. Thanks.

_____________________________________________________________________________
#include "ThreadAccel.hpp"
#include <errno.h>
#include <unistd.h>
#include <bps/sensor.h>
#include <bps/bps.h>
#include <bps/dialog.h>
#include <bps/navigator.h>
#include <screen/screen.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

static const int ACCELEROMETER_RATE = 25000;
/**
 * Constructor.
 */
ThreadAccel::ThreadAccel()
{
    sName = "accel";
    bps_initialize();
    navigator_request_events(0);
    sensor_set_rate(SENSOR_TYPE_ACCELEROMETER, ACCELEROMETER_RATE);
    sensor_set_skip_duplicates(SENSOR_TYPE_ACCELEROMETER, true);
}

/**
 * Destructor.
 */
ThreadAccel::~ThreadAccel()
{
    // TODO: Add Code Here


}

/**
 * Stops the running thread.
 *
 * @return A '0' value is success, '+' is warning, and '-' is error.
 */
int ThreadAccel::stop(void)
{
    int iReturn = 0;

    // TODO: Add Code Here
    cout<<"Acc_stop event"<<endl;

    return iReturn;
}

/**
 * Thread 'run' function.  This function is called by RTOS.  The function
 * signature is equivalent to the start routine definition.
 *
 * @param pvArg
 *              Void pointer to the ThreadAccel object instance.
 *
 * @return Void pointer return value.
 */
void* ThreadAccel::run(void* pvArg)
{
    void* pvReturn = 0;
    int iCallReturn = 0;
    ThreadAccel *pThrdAccelObj = (ThreadAccel *)pvArg;

    if (NULL != pThrdAccelObj)
    {
        // Thread is running, so indicate this to the user
        pThrdAccelObj->iStatus = STATUS_RUNNING;

        // Now, execute the action
        iCallReturn = execute(pThrdAccelObj);
        if (0 == iCallReturn)
            pThrdAccelObj->iStatus = STATUS_FINISHED;
        else
            pThrdAccelObj->iStatus = STATUS_FINISHED_WITH_ERRORS;
    }

    return pvReturn;
}

/**
 * This function actually performs thread actions.
 *
 * @param pThrdAccelObj
 *              Address to a thread class instance.
 *
 * @return A '0' value is success, '+' is warning, and '-' is error.
 */
int ThreadAccel::execute(ThreadAccel *pThrdAccelObj)
{
    int iReturn = 0;
    int paused = 1;
    float force_x, force_y, force_z;
    while (true == pThrdAccelObj->bFlagContinue)
    {
        sleep((unsigned int) 5);

        printf("Running ThreadAccel::execute(), awake, going back to sleep for 5 seconds!\n");


        // TODO: Add Code Here
        // Place code that reads the accelerometer data
        bps_event_t *event = NULL;
        bps_get_event(&event, -1);

        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) {

                } 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;
                        sensor_request_events(SENSOR_TYPE_ACCELEROMETER);

                       cout<<"Resuming accelerometer reads."<<endl;

                    } 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;
                        sensor_stop_events(SENSOR_TYPE_ACCELEROMETER);
                        cout<<"Pausing accelerometer reads."<<endl;

                    }
                }
            } else if (bps_event_get_domain(event) == sensor_get_domain()) {
                if (SENSOR_ACCELEROMETER_READING == bps_event_get_code(event)) {
                    /*
                     * Extract the accelerometer data from the event and
                     * display it.
                     */
                    sensor_event_get_xyz(event, &force_x, &force_y, &force_z);
                    //cout<<"x:"<<force_x<<"y:"<<force_y<<"z:"<<force_z<<endl;

                }
            }

        cout<<"Read data events"<<endl;
    } // while (1)
    }
    return iReturn;
}

Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: About sensor function

Hi,

Try adding
LIBS += -lbb
to .pro file.

If this won't help, please paste the exact error messages the compiler gives.

Andrey Fidrya, @zmeyc on twitter