IoT Drivers  v3.3.0 (S2022)
Engineering in Software Technology
Quick start guide for TSL2591 Light sensor Driver

This is the quick start guide for the Driver for TSL2591 Light sensor, with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

TSL2591 Driver use cases

Initialise the driver

The following must be added to the project:

#include <tsl2591.h>

Call back function

The driver needs a call back function to that it will call to tell the result of a function call.

An simple example of a call back function can be seen here:

void tsl2591Callback(tsl2591_returnCode_t rc)
{
uint16_t _tmp;
float _lux;
switch (rc)
{
if ( TSL2591_OK == (rc = tsl2591_getFullSpectrumRaw(&_tmp)) )
{
printf("\nFull Raw:%04X\n", _tmp);
}
else if( TSL2591_OVERFLOW == rc )
{
printf("\nFull spectrum overflow - change gain and integration time\n");
}
if ( TSL2591_OK == (rc = tsl259_getVisibleRaw(&_tmp)) )
{
printf("Visible Raw:%04X\n", _tmp);
}
else if( TSL2591_OVERFLOW == rc )
{
printf("Visible overflow - change gain and integration time\n");
}
if ( TSL2591_OK == (rc = tsl2591_getInfraredRaw(&_tmp)) )
{
printf("Infrared Raw:%04X\n", _tmp);
}
else if( TSL2591_OVERFLOW == rc )
{
printf("Infrared overflow - change gain and integration time\n");
}
if ( TSL2591_OK == (rc = tsl2591_getLux(&_lux)) )
{
printf("Lux: %5.4f\n", _lux);
}
else if( TSL2591_OVERFLOW == rc )
{
printf("Lux overflow - change gain and integration time\n");
}
break;
case TSL2591_OK:
// Last command performed successful
break;
// Dev ID now fetched
break;
default:
break;
}
}

Add to application initialization:

Initialise the driver by given it a function pointer to your call back function (in this example: tsl2591Callback):

if ( TSL2591_OK == tsl2591_initialise(tsl2591Callback) )
{
// Driver initilised OK
// Always check what tsl2591_initialise() returns
}

How to perform a new measuring with the sensor

In this use case, the steps to perform a measuring is shown.

Note
The driver must be initialised (see Initialise the driver) before a measuring is possible.

The sensor must be powered up before it can be used. This is done with the following command:

{
// The power up command is now send to the sensor - it can be powered down with a call to tsl2591_disable()
}
Note
The sensor is not powered up before the driver calls the call back function with TSL2591_OK. It is only necessary to power up the sensor once - or if it has been powered down with a call to tsl2591_disable()

Start the measuring

The following must be added to the application code:

{
// Something went wrong
// Investigate the return code further
}
else
{
The light data will be ready after the driver calls the call back function with TSL2591_DATA_READY.
}
tsl2591_initialise
tsl2591_returnCode_t tsl2591_initialise(void(*callBack)(tsl2591_returnCode_t))
Initialise the driver.
tsl2591_returnCode_t
tsl2591_returnCode_t
TSL2591 Driver return codes.
Definition: tsl2591.h:110
tsl2591.h
Driver for a TSL2591 Light sensor.
tsl259_getVisibleRaw
tsl2591_returnCode_t tsl259_getVisibleRaw(uint16_t *visible)
Retrieve the latest visible light spectrum as raw data fetched from the TSL2591 sensor.
tsl2591_fetchData
tsl2591_returnCode_t tsl2591_fetchData(void)
Start fetching the TSL2591 sensor's light data.
TSL2591_OVERFLOW
@ TSL2591_OVERFLOW
Definition: tsl2591.h:114
tsl2591_getInfraredRaw
tsl2591_returnCode_t tsl2591_getInfraredRaw(uint16_t *infrared)
Retrieve the latest infrared light spectrum as raw data fetched from the TSL2591 sensor.
tsl2591_getFullSpectrumRaw
tsl2591_returnCode_t tsl2591_getFullSpectrumRaw(uint16_t *fullSpectrum)
Retrieve the latest full light spectrum as raw data fetched from the TSL2591 sensor.
tsl2591_getLux
tsl2591_returnCode_t tsl2591_getLux(float *lux)
Retrieve the latest visible light spectrum as lux fetched from the TSL2591 sensor.
TSL2591_DATA_READY
@ TSL2591_DATA_READY
Definition: tsl2591.h:112
TSL2591_DEV_ID_READY
@ TSL2591_DEV_ID_READY
Definition: tsl2591.h:113
tsl2591_enable
tsl2591_returnCode_t tsl2591_enable(void)
Enable/Power up the TSL2591 sensor.
TSL2591_OK
@ TSL2591_OK
Definition: tsl2591.h:111