Light Sensor Example

  1. General
  2. Initializing
  3. Reading
  4. Full Code Example

General

Note: The following code snippets are intended for XDK-Workbench versions 3.4.0 and higher.

In this section, an example for sensor measurement will be implemented based on the light sensor. With the light sensor, the advantage is easy troubleshooting, since the implementation is fairly simple and straight-forward. As described before, the example will be implemented in a new XdkApplicationTemplate project.

Note: The functions AppControllerSetup() and AppControllerEnable() are required in every project, and should not be removed or have its signature altered. The function AppControllerFire() is optional and could be replaced with a timer task.

To provide all necessary functions for the light sensor, such as initializing, reading sensor values and settings, the interface XdkSensorHandle.h has to be included, as shown in the following code.

/* Include this code in XdkApplicationTemplate.c */
#include "XdkSensorHandle.h"

Furthermore, the interface holds references to all sensors of the XDK (including the Light Sensor) which makes all of them accessible. Please note that this interface only needs to be included once, no matter how many sensors are used.

Initializing

For the next step the light sensor has to be initialized

/* Include this code in XdkApplicationTemplate.c */
static void initLightSensor(void)
{
  /* initialize light sensor */
  LightSensor_init(xdkLightSensor_MAX44009_Handle);
}

The light sensor is initialized by using the function LightSensor_init(), which receives the handle to the sensor as its input

Reading

After initializing the sensor successfully, it is possible to read its sensor values. The following code snippet demonstrates how the sensor data can be read and stored in a variable and then printed to the console.

/* Replace this code snippet with the AppControllerFire() function in XdkApplicationTemplate.c */

static void AppControllerFire(void* pvParameters) {
    BCDS_UNUSED(pvParameters);
    uint32_t milliLuxData = UINT32_C(0);
    while (1)
    {
        vTaskDelay(1000);
        /* Read and print light sensor data */

        LightSensor_readLuxData(xdkLightSensor_MAX44009_Handle,&milliLuxData);

        printf("Light sensor data obtained in milli lux :%d \n\r",(unsigned int) milliLuxData);
    }
}

The implemented function AppControllerFire() reads and prints the light sensor data. The first expression prevents a compiler-warning regarding unused variables and can be ignored. The variable milliLuxData is an unsigned 32-bit variable, declared to store the light sensor data. Then a while loop is used to run the operating task function AppControllerFire() for infinite time. A call of the function vTaskDelay() is used to set a delay of one second during the readings of the light sensor. The next line of code shows the actual reading of the light sensor data via LightSensor_readLuxData(). The function printf(), for printing a formatted string to the console, is called right after reading. The printf() is also used as a means to see whether the light sensor was correctly used. If it wasn’t, the printf() would show a value of zero for the data.

The initialization of the light sensor and a function that reads its data are implemented. Now the initialization function initLightSensor() only needs to be called in AppControllerSetup(). The code snippet below shows where.

/* Replace this code snippet with the AppControllerSetup() function in XdkApplicationTemplate.c */

static void AppControllerSetup(void * param1, uint32_t param2) {
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    initLightSensor();

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

Finally the first sensor application is ready to be built and flashed to the XDK.

Full Code Example

/* --------------------------------------------------------------------------- |
 * INCLUDES & DEFINES ******************************************************** |
 * -------------------------------------------------------------------------- */

/* own header files */
#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID  /* Module ID define before including Basics package*/
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER
#include "AppController.h"

/* system header files */
#include <stdio.h>

/* additional interface header files */
#include "BCDS_CmdProcessor.h"
#include "XDK_Utils.h"
#include "FreeRTOS.h"
#include "task.h"
#include "XdkSensorHandle.h"

/* --------------------------------------------------------------------------- |
 * HANDLES ******************************************************************* |
 * -------------------------------------------------------------------------- */

static CmdProcessor_T * AppCmdProcessor;/**< Handle to store the main Command processor handle to be used by run-time event driven threads */
static xTaskHandle AppControllerHandle = NULL;/**< OS thread handle for Application controller to be used by run-time blocking threads */

/* --------------------------------------------------------------------------- |
 * EXECUTING FUNCTIONS ******************************************************* |
 * -------------------------------------------------------------------------- */

static void initLightSensor(void)
{
  /* initialize light sensor */
  LightSensor_init(xdkLightSensor_MAX44009_Handle);
}

static void AppControllerFire(void* pvParameters) {
    BCDS_UNUSED(pvParameters);
    uint32_t milliLuxData = UINT32_C(0);
    while (1)
    {
        vTaskDelay(1000);
        /* Read and print light sensor data */

        LightSensor_readLuxData(xdkLightSensor_MAX44009_Handle,&milliLuxData);

        printf("Light sensor data obtained in milli lux :%d \n\r",(unsigned int) milliLuxData);
    }
}

/* --------------------------------------------------------------------------- |
 * BOOTING- AND SETUP FUNCTIONS ********************************************** |
 * -------------------------------------------------------------------------- */

static void AppControllerEnable(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    if (RETCODE_OK == retcode)
    {
        if (pdPASS != xTaskCreate(AppControllerFire, (const char * const ) "AppController", TASK_STACK_SIZE_APP_CONTROLLER, NULL, TASK_PRIO_APP_CONTROLLER, &AppControllerHandle))
        {
            retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_OUT_OF_RESOURCES);
        }
    }
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerEnable : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0); /* To provide LED indication for the user */
    }
    Utils_PrintResetCause();
}

static void AppControllerSetup(void * param1, uint32_t param2) {
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    initLightSensor();

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

void AppController_Init(void * cmdProcessorHandle, uint32_t param2)
{
    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    if (cmdProcessorHandle == NULL)
    {
        printf("AppController_Init : Command processor handle is NULL \r\n");
        retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
    }
    else
    {
        AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
        retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
    }

    if (RETCODE_OK != retcode)
    {
        Retcode_RaiseError(retcode);
        assert(0); /* To provide LED indication for the user */
    }
}

/** ************************************************************************* */