tsl2591 - Driver for light-to-digital converter TSL2591

group tsl2591

ESP-IDF driver for TSL2591 light-to-digital.

Copyright (C) 2020 Julian Doerner https://github.com/juliandoerner

MIT Licensed as described in the file LICENSE

Defines

TSL2591_I2C_ADDR

Enums

enum tsl2591_power_status_t

Power status.

The sensor measures only if ALS an Power is on.

Values:

enumerator TSL2591_POWER_OFF
enumerator TSL2591_POWER_ON

Default.

enum tsl2591_als_status_t

ALS status.

The sensor measures only if ALS and Power is on.

Values:

enumerator TSL2591_ALS_OFF
enumerator TSL2591_ALS_ON

Default.

enum tsl2591_interrupt_t

Interrupts.

TSL2591 has two interrupt sources. Check the datasheet for details.

Values:

enumerator TSL2591_INTR_OFF

Default.

enumerator TSL2591_ALS_INTR_ON
enumerator TSL2591_ALS_INTR_NP_ON
enumerator TSL2591_ALS_INTR_BOTH_ON
enum tsl2591_sleep_after_intr_t

Interrupt sleep setting.

Values:

enumerator TSL2591_SLEEP_AFTER_OFF

Default.

enumerator TSL2591_SLEEP_AFTER_ON
enum tsl2591_integration_time_t

Integration time.

Values:

enumerator TSL2591_INTEGRATION_100MS

Default.

enumerator TSL2591_INTEGRATION_200MS
enumerator TSL2591_INTEGRATION_300MS
enumerator TSL2591_INTEGRATION_400MS
enumerator TSL2591_INTEGRATION_500MS
enumerator TSL2591_INTEGRATION_600MS
enum tsl2591_gain_t

Gain.

Values:

enumerator TSL2591_GAIN_LOW

Default.

enumerator TSL2591_GAIN_MEDIUM
enumerator TSL2591_GAIN_HIGH
enumerator TSL2591_GAIN_MAX
enum tsl2591_persistence_filter_t

Persistence filter.

Values:

enumerator TSL2591_EVERY_CYCLE

Default.

enumerator TSL2591_NO_PERSIST
enumerator TSL2591_2_CYCLES
enumerator TSL2591_3_CYCLES
enumerator TSL2591_5_CYCLES
enumerator TSL2591_10_CYCLES
enumerator TSL2591_15_CYCLES
enumerator TSL2591_20_CYCLES
enumerator TSL2591_25_CYCLES
enumerator TSL2591_30_CYCLES
enumerator TSL2591_35_CYCLES
enumerator TSL2591_40_CYCLES
enumerator TSL2591_45_CYCLES
enumerator TSL2591_50_CYCLES
enumerator TSL2591_55_CYCLES
enumerator TSL2591_60_CYCLES

Functions

esp_err_t tsl2591_init_desc(tsl2591_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptor.

Parameters
  • dev – Device descriptor

  • port – I2C port

  • sda_gpio – SDA GPIO pin

  • scl_gpio – SCL GPIO pin

Returns

ESP_OK on success

esp_err_t tsl2591_free_desc(tsl2591_t *dev)

Free device descriptor.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t tsl2591_init(tsl2591_t *dev)

Initialize device.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t tsl2591_get_channel_data(tsl2591_t *dev, uint16_t *channel0, uint16_t *channel1)

Read channel data.

Parameters
  • dev – Device descriptor

  • [out] channel0 – Channel 0 data

  • [out] channel1 – Channel 1 data

esp_err_t tsl2591_calculate_lux(tsl2591_t *dev, uint16_t channel0, uint16_t channel1, float *lux)

Calculate light intensity from channels.

Parameters
  • dev – Device descriptor

  • channel0 – Channel0 data

  • channel1 – Channel1 data

  • [out] lux – Light intensity

Returns

ESP_OK on success

esp_err_t tsl2591_get_lux(tsl2591_t *dev, float *lux)

Get and calculate light intensity.

Parameters
  • dev – Device descriptor

  • [out] lux – Light intensity

Returns

ESP_OK

esp_err_t tsl2591_set_power_status(tsl2591_t *dev, tsl2591_power_status_t power_status)

Set device power status.

Parameters
  • dev – Device descriptor

  • power_status – Power status

Returns

ESP_OK on success

esp_err_t tsl2591_get_power_status(tsl2591_t *dev, tsl2591_power_status_t *power_status)

Get device power status.

Parameters
  • dev – Device descriptor

  • [out] power_status – Power status

Returns

ESP_OK on success

esp_err_t tsl2591_set_als_status(tsl2591_t *dev, tsl2591_als_status_t als_status)

Set device ALS status.

Parameters
  • dev – Device descriptor

  • als_status – Als status

Returns

ESP_OK on success

esp_err_t tsl2591_get_als_status(tsl2591_t *dev, tsl2591_als_status_t *als_status)

Get device ALS status.

Parameters
  • dev – Device descriptor

  • [out] als_status – Als status

Returns

ESP_OK

esp_err_t tsl2591_set_interrupt(tsl2591_t *dev, tsl2591_interrupt_t interrupt)

Set device interrupt mode.

Parameters
  • dev – Device descriptor

  • interrupt – interrupt mode

Returns

ESP_OK on success

esp_err_t tsl2591_get_interrupt(tsl2591_t *dev, tsl2591_interrupt_t *interrupt)

Get device interrupt mode.

Parameters
  • dev – Device descriptor

  • [out] interrupt – interrupt mode

Returns

ESP_OK

esp_err_t tsl2591_set_sleep_after_intr(tsl2591_t *dev, tsl2591_sleep_after_intr_t sleep_after_intr)

Set sleeping after interrupt.

Parameters
  • dev – Device descriptor

  • sleep_after_intr – Sleeping after interrupt

Returns

ESP_OK on success

esp_err_t tsl2591_get_sleep_after_intr(tsl2591_t *dev, tsl2591_sleep_after_intr_t *sleep_after_intr)

Get sleeping after interrupt setting.

Parameters
  • dev – Device descriptor

  • [out] sleep_after_intr – Sleeping after interrupt

Returns

ESP_OK on success

esp_err_t tsl2591_set_integration_time(tsl2591_t *dev, tsl2591_integration_time_t integration_time)

Set device integration time.

Parameters
  • dev – Device descriptor

  • integration_time – Integration time

Returns

ESP_OK on success

esp_err_t tsl2591_get_integration_time(tsl2591_t *dev, tsl2591_integration_time_t *integration_time)

Get device integration time.

Parameters
  • dev – Device descriptor

  • [out] integration_time – Integration time

Returns

ESP_OK on success

esp_err_t tsl2591_set_gain(tsl2591_t *dev, tsl2591_gain_t gain)

Set device gain.

Parameters
  • dev – Device descriptor

  • gain – Gain

Returns

ESP_OK on success

esp_err_t tsl2591_get_gain(tsl2591_t *dev, tsl2591_gain_t *gain)

Get device gain.

Parameters
  • dev – Device descriptor

  • [out] gain – Gain

Returns

ESP_OK on success

esp_err_t tsl2591_set_persistence_filter(tsl2591_t *dev, tsl2591_persistence_filter_t filter)

Set device persistence filter.

Parameters
  • dev – Device descriptor

  • filter – Persistence filter

Returns

ESP_OK on success

esp_err_t tsl2591_get_persistence_filter(tsl2591_t *dev, tsl2591_persistence_filter_t *filter)

Get device persistence filter.

Parameters
  • dev – Device descriptor

  • [out] filter – Persistence filter

Returns

ESP_OK on success

esp_err_t tsl2591_als_set_low_threshold(tsl2591_t *dev, uint16_t low_threshold)

Set ALS interrupt low threshold.

Parameters
  • dev – Device descriptor

  • low_threshold – Low threshold

Returns

ESP_OK on success

esp_err_t tsl2591_als_set_high_threshold(tsl2591_t *dev, uint16_t high_threshold)

Set ALS interrupt high threshold.

Parameters
  • dev – Device descriptor

  • high_threshold – High threshold

Returns

ESP_OK on success

esp_err_t tsl2591_no_persist_set_low_threshold(tsl2591_t *dev, uint16_t low_threshold)

Set no persist ALS interrupt low threshold.

Parameters
  • dev – Device descriptor

  • low_threshold – Low threshold

Returns

ESP_OK on success

esp_err_t tsl2591_no_persist_set_high_threshold(tsl2591_t *dev, uint16_t high_threshold)

Set no persist ALS interrupt high threshold.

Parameters
  • dev – Device descriptor

  • high_threshold – High threshold

Returns

ESP_OK on success

esp_err_t tsl2591_set_test_intr(tsl2591_t *dev)

Set interrupt.

At least on interrupt must be enabled.

Parameters

dev – Device descriptor

Returns

ESP_PK on success

esp_err_t tsl2591_clear_als_intr(tsl2591_t *dev)

Clear ALS interrupt.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t tsl2591_clear_als_np_intr(tsl2591_t *dev)

Clear ALS no persist interrupt.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t tsl2591_clear_both_intr(tsl2591_t *dev)

Clear both interrupts.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t tsl2591_get_np_intr_flag(tsl2591_t *dev, bool *flag)

Get ALS no persist interrupt flag.

Parameters
  • dev – Device descriptor

  • [out] flag – Interrupt flag

Returns

ESP_OK on success

esp_err_t tsl2591_get_als_intr_flag(tsl2591_t *dev, bool *flag)

Get ALS interrupt flag.

Parameters
  • dev – Device descriptor

  • [out] flag – Interrupt flag

Returns

ESP_OK on success

esp_err_t tsl2591_get_als_valid_flag(tsl2591_t *dev, bool *flag)

Get ALS validity flag.

This flag is set when integration cycle is completed after enabling ALS.

Parameters
  • dev – Device descriptor

  • [out] flag – Validity flag

Returns

ESP_OK on success

struct tsl2591_settings_t
#include <tsl2591.h>

Device settings.

struct tsl2591_t
#include <tsl2591.h>

Device descriptor.