esp32-tsl2561
ESP32-compatible C library for TSL2561 Light to Digital Converter.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator
Macros | Functions
/home/travis/build/DavidAntliff/esp32-tsl2561/tsl2561.c File Reference
#include <stddef.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "tsl2561.h"

Macros

#define REG_CONTROL   0x00
 
#define REG_TIMING   0x01
 
#define REG_THRESHLOWLOW   0x02
 
#define REG_THRESHLOWHIGH   0x03
 
#define REG_THRESHHIGHLOW   0x04
 
#define REG_THRESHHIGHHIGH   0x05
 
#define REG_INTERRUPT   0x06
 
#define REG_ID   0x0A
 
#define REG_DATA0LOW   0x0C
 
#define REG_DATA0HIGH   0x0D
 
#define REG_DATA1LOW   0x0E
 
#define REG_DATA1HIGH   0x0F
 
#define SMB_BLOCK   0x10
 
#define SMB_WORD   0x20
 
#define SMB_CLEAR   0x40
 
#define SMB_COMMAND   0x80
 
#define TSL2561_CONTROL_POWER_UP   0x03
 
#define TSL2561_CONTROL_POWER_DOWN   0x00
 
#define DEFAULT_INTEGRATION_TIME   TSL2561_INTEGRATION_TIME_402MS
 
#define DEFAULT_GAIN   TSL2561_GAIN_1X
 
#define CH_SCALE   10
 
#define CH_SCALE_TINT0   0x7517
 
#define CH_SCALE_TINT1   0x0FE7
 
#define RATIO_SCALE   9
 
#define LUX_SCALE   14
 
#define TSL2561_K1T   0x0040
 
#define TSL2561_B1T   0x01F2
 
#define TSL2561_M1T   0x01BE
 
#define TSL2561_K2T   0x0080
 
#define TSL2561_B2T   0x0214
 
#define TSL2561_M2T   0x02D1
 
#define TSL2561_K3T   0x00C0
 
#define TSL2561_B3T   0x023F
 
#define TSL2561_M3T   0x037B
 
#define TSL2561_K4T   0x0100
 
#define TSL2561_B4T   0x0270
 
#define TSL2561_M4T   0x03FE
 
#define TSL2561_K5T   0x0138
 
#define TSL2561_B5T   0x016F
 
#define TSL2561_M5T   0x01fC
 
#define TSL2561_K6T   0x019A
 
#define TSL2561_B6T   0x00D2
 
#define TSL2561_M6T   0x00FB
 
#define TSL2561_K7T   0x029A
 
#define TSL2561_B7T   0x0018
 
#define TSL2561_M7T   0x0012
 
#define TSL2561_K8T   0x029A
 
#define TSL2561_B8T   0x0000
 
#define TSL2561_M8T   0x0000
 
#define TSL2561_K1C   0x0043
 
#define TSL2561_B1C   0x0204
 
#define TSL2561_M1C   0x01AD
 
#define TSL2561_K2C   0x0085
 
#define TSL2561_B2C   0x0228
 
#define TSL2561_M2C   0x02C1
 
#define TSL2561_K3C   0x00C8
 
#define TSL2561_B3C   0x0253
 
#define TSL2561_M3C   0x0363
 
#define TSL2561_K4C   0x010A
 
#define TSL2561_B4C   0x0282
 
#define TSL2561_M4C   0x03DF
 
#define TSL2561_K5C   0x014D
 
#define TSL2561_B5C   0x0177
 
#define TSL2561_M5C   0x01DD
 
#define TSL2561_K6C   0x019A
 
#define TSL2561_B6C   0x0101
 
#define TSL2561_M6C   0x0127
 
#define TSL2561_K7C   0x029A
 
#define TSL2561_B7C   0x0037
 
#define TSL2561_M7C   0x002B
 
#define TSL2561_K8C   0x029A
 
#define TSL2561_B8C   0x0000
 
#define TSL2561_M8C   0x0000
 

Functions

tsl2561_info_ttsl2561_malloc (void)
 Construct a new TSL2561 info instance. New instance should be initialised before calling other functions. More...
 
void tsl2561_free (tsl2561_info_t **tsl2561_info)
 Delete an existing TSL2561 info instance. More...
 
esp_err_t tsl2561_init (tsl2561_info_t *tsl2561_info, smbus_info_t *smbus_info)
 Initialise a TSL2561 info instance with the specified SMBus information. More...
 
esp_err_t tsl2561_device_id (const tsl2561_info_t *tsl2561_info, tsl2561_device_type_t *device, tsl2561_revision_t *revision)
 Retrieve the Device Type ID and Revision number from the device. More...
 
esp_err_t tsl2561_read (tsl2561_info_t *tsl2561_info, tsl2561_visible_t *visible, tsl2561_infrared_t *infrared)
 Retrieve a visible and infrared light measurement from the device. This function will sleep until the integration time has passed. More...
 
esp_err_t tsl2561_set_integration_time_and_gain (tsl2561_info_t *tsl2561_info, tsl2561_integration_time_t integration_time, tsl2561_gain_t gain)
 Set the integration time and gain. These values are set together as they are programmed via the same register. More...
 
uint32_t tsl2561_compute_lux (const tsl2561_info_t *tsl2561_info, tsl2561_visible_t visible, tsl2561_infrared_t infrared)
 Compute the Lux approximation from a visible and infrared light measurement. The calculation is performed according to the procedure given in the datasheet. More...
 

Detailed Description

Acknowledgements to Kevin Townsend for the Adafruit TSL2561 driver: https://github.com/adafruit/Adafruit_TSL2561 Acknowledgements to https://github.com/lexruee/tsl2561 for a working reference.

Function Documentation

tsl2561_info_t* tsl2561_malloc ( void  )

Construct a new TSL2561 info instance. New instance should be initialised before calling other functions.

Returns
Pointer to new device info instance, or NULL if it cannot be created.
void tsl2561_free ( tsl2561_info_t **  tsl2561_info)

Delete an existing TSL2561 info instance.

Parameters
[in,out]tsl2561_infoPointer to TSL2561 info instance that will be freed and set to NULL.
esp_err_t tsl2561_init ( tsl2561_info_t tsl2561_info,
smbus_info_t *  smbus_info 
)

Initialise a TSL2561 info instance with the specified SMBus information.

Parameters
[in]tsl2561_infoPointer to TSL2561 info instance.
[in]smbus_infoPointer to SMBus info instance.
esp_err_t tsl2561_device_id ( const tsl2561_info_t tsl2561_info,
tsl2561_device_type_t device,
tsl2561_revision_t revision 
)

Retrieve the Device Type ID and Revision number from the device.

Parameters
[in]tsl2561_infoPointer to initialised TSL2561 info instance.
[out]deviceThe retrieved Device Type ID.
[out]revisionThe retrieved Device Revision number.
Returns
ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
esp_err_t tsl2561_read ( tsl2561_info_t tsl2561_info,
tsl2561_visible_t visible,
tsl2561_infrared_t infrared 
)

Retrieve a visible and infrared light measurement from the device. This function will sleep until the integration time has passed.

Parameters
[in]tsl2561_infoPointer to initialised TSL2561 info instance.
[out]visibleThe resultant visible light measurement.
[out]infraredThe resultant infrared light measurement.
Returns
ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
esp_err_t tsl2561_set_integration_time_and_gain ( tsl2561_info_t tsl2561_info,
tsl2561_integration_time_t  integration_time,
tsl2561_gain_t  gain 
)

Set the integration time and gain. These values are set together as they are programmed via the same register.

Parameters
[in]tsl2561_infoPointer to initialised TSL2561 info instance.
[out]integration_timeThe integration time to use for the next measurement.
[out]infraredThe gain setting to use for the next measurement.
Returns
ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
uint32_t tsl2561_compute_lux ( const tsl2561_info_t tsl2561_info,
tsl2561_visible_t  visible,
tsl2561_infrared_t  infrared 
)

Compute the Lux approximation from a visible and infrared light measurement. The calculation is performed according to the procedure given in the datasheet.

Parameters
[in]tsl2561_infoPointer to initialised TSL2561 info instance.
[in]visibleThe visible light measurement.
[in]infraredThe infrared light measurement.
Returns
The resulting approximation of the light measurement in Lux.