esp32-tsl2561
ESP32-compatible C library for TSL2561 Light to Digital Converter.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator
Data Structures | Typedefs | Enumerations | Functions
tsl2561.h File Reference

Interface definitions for the ESP32-compatible TSL2561 Light to Digital Converter component. More...

#include <stdbool.h>
#include "smbus.h"

Go to the source code of this file.

Data Structures

struct  tsl2561_info_t
 Structure containing information related to the SMBus protocol. More...
 

Typedefs

typedef uint8_t tsl2561_revision_t
 The type of the IC's revision value.
 
typedef uint16_t tsl2561_visible_t
 The type of a visible light measurement value.
 
typedef uint16_t tsl2561_infrared_t
 The type of an infrared light measurement value.
 

Enumerations

enum  tsl2561_device_type_t {
  TSL2561_DEVICE_TYPE_INVALID = 0b1111, TSL2561_DEVICE_TYPE_TSL2560CS = 0b0000, TSL2561_DEVICE_TYPE_TSL2561CS = 0b0001, TSL2561_DEVICE_TYPE_TSL2560T_FN_CL = 0b0100,
  TSL2561_DEVICE_TYPE_TSL2561T_FN_CL = 0b0101
}
 Enum for recognised TSL256x devices. More...
 
enum  tsl2561_integration_time_t { TSL2561_INTEGRATION_TIME_13MS = 0x00, TSL2561_INTEGRATION_TIME_101MS = 0x01, TSL2561_INTEGRATION_TIME_402MS = 0x02 }
 Enum for supported integration durations. These durations assume the default internal oscillator frequency of 735 kHz. More...
 
enum  tsl2561_gain_t { TSL2561_GAIN_1X = 0x00, TSL2561_GAIN_16X = 0x10 }
 Enum for supported gain values.
 

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_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...
 
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...
 
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

Interface definitions for the ESP32-compatible TSL2561 Light to Digital Converter component.

This component provides structures and functions that are useful for communicating with the device.

Technically, the TSL2561 device is an I2C not SMBus device, however the datasheet makes it clear that most SMBus operations are compatible with this device, so it makes sense to use an SMBus interface to manage communication.

Enumeration Type Documentation

Enum for recognised TSL256x devices.

Enumerator
TSL2561_DEVICE_TYPE_INVALID 

Invalid device.

TSL2561_DEVICE_TYPE_TSL2560CS 

TSL2560CS (Chipscale)

TSL2561_DEVICE_TYPE_TSL2561CS 

TSL2561CS (Chipscale)

TSL2561_DEVICE_TYPE_TSL2560T_FN_CL 

TSL2560T/FN/CL (TMB-6 or Dual Flat No-Lead-6 or ChipLED-6)

TSL2561_DEVICE_TYPE_TSL2561T_FN_CL 

TSL2561T/FN/CL (TMB-6 or Dual Flat No-Lead-6 or ChipLED-6)

Enum for supported integration durations. These durations assume the default internal oscillator frequency of 735 kHz.

Enumerator
TSL2561_INTEGRATION_TIME_13MS 

Integrate over 13.7 milliseconds.

TSL2561_INTEGRATION_TIME_101MS 

Integrate over 101 milliseconds.

TSL2561_INTEGRATION_TIME_402MS 

Integrate over 402 milliseconds.

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_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.
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.
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.