Basic example: How to retrieve sensor data
응답
16. 6. 13 오전 9:36

Hi everyone,

I`m a beginner with the XDK and working my way into programming the device to my desire. I ran into some problems understanding how to obtain data from the sensors as the demos provided are too complex to comprehend. What I sort of hoped for is a function like:

double getLightSensorValue() 

But for the purpose of a basic showcase let`s assume I want to modify the LedsAndButtons-demo in a way that when pressing Button 1 it also prints the value from the light sensor:

if (BUTTON_isPressed(handle))
        {
            uint8_t returnValue = ZERO_VALUE;

            returnValue += LED_setState(redLedHandle, LED_SET_ON);
            returnValue += LED_setState(orangeLedHandle, LED_SET_ON);
            returnValue += LED_setState(yellowLedHandle, LED_SET_OFF);

            if (returnValue == ZERO_VALUE)
            {
                printf("PB2 pressed\n\r");
                // print light sensor data here ...
            }
            else
            {
                printf("PB2 pressed but setting LED state failed\n\r");
            }
        }

What would I have to write there? Thank you very much in advance.

Regards, Felix

0 (0 투표)
RE: Basic example: How to retrieve sensor data
응답
16. 6. 13 오전 11:23 as a reply to Felix Gandha Salentin.

Hello Felix,

I wrote some example code to help you with issue. Before I explain it in detail, I would suggest to try out the example code here to get a basic understanding how to initialize and read sensors of the XDK.

First of all, you need to include this headerfile in your implementation (.c) file:

#include "XdkSensorHandle.h"

 

In addition, you have to make an initalization of the light sensor. This init block need to be run once and therefore it should be inserted in the ledAndButtonsInit function.

return_t ledAndButtonInit(void)
{
    return_t returnValue = FAILURE;

    /* initialize light sensor */
    returnValue = RETCODE_FAILURE;
    returnValue = LightSensor_init(xdkLightSensor_MAX44009_Handle);
    if ( RETCODE_OK != returnValue){
        printf("Light Sensor initialization Failed\n\r");
    }
...
}

 

Finally you can insert this codeblock to read and print the measured values.

if (BUTTON_isPressed(handle)){
    uint8_t returnValue = ZERO_VALUE;

    returnValue += LED_setState(redLedHandle, LED_SET_ON);
    returnValue += LED_setState(orangeLedHandle, LED_SET_ON);
    returnValue += LED_setState(yellowLedHandle, LED_SET_OFF);

    if (returnValue == ZERO_VALUE){
        printf("PB2 pressed\n\r");
        // print light sensor data here ...
        /* Read and print light sensor data */
        uint32_t milliLuxData = UINT32_C(0);
        returnValue = LightSensor_readLuxData(
xdkLightSensor_MAX44009_Handle,&milliLuxData);

        if (RETCODE_OK == returnValue){
            printf("Light sensor data obtained in milli 
lux :%d \n\r",(unsigned int) milliLuxData);
        }
    }
...
}

 

Please tell me if this example brought some insight to you.

Kind regards,
Franjo

0 (0 투표)
RE: Basic example: How to retrieve sensor data
응답
16. 6. 13 오후 12:42 as a reply to Franjo Stjepandic.

Hello Franjo,

Thank you for getting back to me. After going through the example I was able to get the code running. However, I had to include this as well:

#include "BCDS_LightSensor.h"

Before, I was looking at the StreamSensorDataOverUsb-Demo for reference and it´s weird because it doesn´t process the includes. This renders it difficult to trace the used variables but also causes the IDE to mark large chunks of the code as erroneous. When flashing the project onto the device however it seems to work just fine. Anyways, maybe that´s a bug in my Workbench.

0 (0 투표)
RE: Basic example: How to retrieve sensor data
응답
16. 6. 13 오후 1:00 as a reply to Felix Gandha Salentin.

Hello Felix,

the code should work without the include, because the light sensor functions can be resolved from the XdkSensorHandle.h headerfile. Nevertheless I will further investigate it. Thank you very much for this hint.

Kind regards,

Franjo

0 (0 투표)