# C

This code base on language C have the propose to get information about this sensor and display it.

First part that we need to write on our code is the libraries that we are going to use. In this case we are going to use

XdkSensorHandle.h

```
/************************************************************
*   LightSensor.c
************************************************************/

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

/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

#include "XdkSensorHandle.h"

/* own header files */
```

A storage variable need to be declared. The data is then read by the sensor by the LightSensor\_readLuxData() function and then stored in a passed reference of the storage variable.

```
// Function that read and print the sensor data of the MAX44009 to the console of the XDK-Workbench
static void readLightSensor(xTimerHandle xTimer)
{
    (void) xTimer;

    Retcode_T returnValue = RETCODE_FAILURE;
    /* Read and print light sensor data */

    uint32_t max44009 = UINT32_C(0);

    returnValue = LightSensor_readLuxData(xdkLightSensor_MAX44009_Handle,&max44009);

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

Then we declared the function to initialized the sensor and variable. We need to use the function of LightSensor\_init(xdkLightSensor\_MAX44009\_Handle);

```
// Function that initializes the Lightsensor with the MAX44009 handler and with additional presettings
static void initLightSensor(void)
{
    Retcode_T returnValue = RETCODE_FAILURE;
    Retcode_T returnBrightnessValue = RETCODE_FAILURE;
    Retcode_T returnIntegrationTimeValue = RETCODE_FAILURE;

    /* initialize light sensor */

    returnValue = LightSensor_init(xdkLightSensor_MAX44009_Handle);

    if ( RETCODE_OK != returnValue){
        printf("MAX44009 Light Sensor initialization failed\n\r");
    }

    returnBrightnessValue = LightSensor_setBrightness(xdkLightSensor_MAX44009_Handle,LIGHTSENSOR_NORMAL_BRIGHTNESS);
    if (RETCODE_OK != returnBrightnessValue) {
        printf("Configuring brightness failed \n\r");
    }
    returnIntegrationTimeValue = LightSensor_setIntegrationTime(xdkLightSensor_MAX44009_Handle,LIGHTSENSOR_200MS);
    if (RETCODE_OK != returnIntegrationTimeValue) {
        printf("Configuring integration time failed \n\r");
    }
}
```

Finally we declared the main part of the program where we need to call all the function that we previously defined. Also define the timers.

```
void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
    {
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);

    uint32_t timerBlockTime = UINT32_MAX;
    uint32_t oneSecondDelay = UINT32_C(1000);
    uint32_t timerAutoReloadOn = UINT32_C(1);

    xTimerHandle lightSensorHandle = NULL;

    initLightSensor();

    lightSensorHandle = xTimerCreate((const char *) "readLightSensor", oneSecondDelay,timerAutoReloadOn, NULL, readLightSensor);

    xTimerStart(lightSensorHandle,timerBlockTime);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alfredo-reyes-montero.gitbook.io/iot-bosch/devices/bosch-xdk-110/hardware/sensors/ambient-light/c.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
