10static constexpr uint32_t ADC_WAIT_INTERVAL_ID = 0;
13#define TSL2591_COMMAND_BIT (0xA0)
14#define TSL2591_ENABLE_POWERON (0x01)
15#define TSL2591_ENABLE_POWEROFF (0x00)
16#define TSL2591_ENABLE_AEN (0x02)
19#define TSL2591_REGISTER_ENABLE (0x00)
20#define TSL2591_REGISTER_CONTROL (0x01)
21#define TSL2591_REGISTER_DEVICE_ID (0x12)
22#define TSL2591_REGISTER_STATUS (0x13)
23#define TSL2591_REGISTER_CHAN0_LOW (0x14)
24#define TSL2591_REGISTER_CHAN0_HIGH (0x15)
25#define TSL2591_REGISTER_CHAN1_LOW (0x16)
26#define TSL2591_REGISTER_CHAN1_HIGH (0x17)
30 if (!this->
write_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN)) {
31 ESP_LOGE(TAG,
"I2C write failed");
36 if (!this->
write_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF)) {
37 ESP_LOGE(TAG,
"I2C write failed");
66 if (!this->
check_chip_id(
this, TSL2591_COMMAND_BIT | TSL2591_REGISTER_DEVICE_ID, 0x50))
74 ESP_LOGCONFIG(TAG,
"TSL2591:");
80 ESP_LOGCONFIG(TAG,
" Name: %s", this->
name_);
83 std::string gain_word =
"unknown";
99 gain_word =
"maximum";
107 int timing_ms = (1 + raw_timing) * 100;
110 " Integration Time: %d ms\n"
111 " Power save mode enabled: %s\n"
112 " Device factor: %f\n"
113 " Glass attenuation factor: %f",
116 LOG_SENSOR(
" ",
"Full spectrum:", this->full_spectrum_sensor_);
117 LOG_SENSOR(
" ",
"Infrared:", this->infrared_sensor_);
118 LOG_SENSOR(
" ",
"Visible:", this->visible_sensor_);
119 LOG_SENSOR(
" ",
"Calculated lux:", this->calculated_lux_sensor_);
120 LOG_SENSOR(
" ",
"Actual gain:", this->actual_gain_sensor_);
122 LOG_UPDATE_INTERVAL(
this);
132 ESP_LOGD(TAG,
"Got illuminance: combined 0x%" PRIX32
", full %d, IR %d, vis %d. Calc lux: %f. Actual gain: %d.",
133 combined, full, infrared, visible, lux, actual_gain);
134 PUBLISH_STATE(this->full_spectrum_sensor_, full);
135 PUBLISH_STATE(this->infrared_sensor_, infrared);
136 PUBLISH_STATE(this->visible_sensor_, visible);
137 PUBLISH_STATE(this->calculated_lux_sensor_, lux);
143 PUBLISH_STATE(this->actual_gain_sensor_, actual_gain);
150 ESP_LOGD(TAG,
"Elapsed %3llu ms; still waiting for valid ADC", (now - this->
interval_start_));
152 ESP_LOGW(TAG,
"Interval timeout for '%s' expired before ADCs became valid", this->
name_);
191 if (!this->
write_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL,
193 ESP_LOGE(TAG,
"I2C write failed");
206 if (!this->
read_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_STATUS, &
status)) {
207 ESP_LOGE(TAG,
"I2C read failed");
220 const uint8_t mini_delay = 100;
221 for (uint16_t d = 0; d < 620; d += mini_delay) {
227 ESP_LOGD(TAG,
" after %3d ms: ADC valid? %s", d, avalid ? LOG_STR_LITERAL(
"true") : LOG_STR_LITERAL(
"false"));
233 ESP_LOGE(TAG,
"Device '%s' returned invalid readings", this->
name_);
243 uint8_t ch0low, ch0high, ch1low, ch1high;
246 if (!this->
read_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW, &ch0low)) {
247 ESP_LOGE(TAG,
"I2C read failed");
250 if (!this->
read_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_HIGH, &ch0high)) {
251 ESP_LOGE(TAG,
"I2C read failed");
255 if (!this->
read_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_LOW, &ch1low)) {
256 ESP_LOGE(TAG,
"I2C read failed");
259 if (!this->
read_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_HIGH, &ch1high)) {
260 ESP_LOGE(TAG,
"I2C read failed");
264 x32 = (ch1_16 << 16) | ch0_16;
278 return (combined_illuminance & 0xFFFF);
281 return (combined_illuminance >> 16);
284 uint16_t full = combined_illuminance & 0xFFFF;
285 uint16_t ir = combined_illuminance >> 16;
286 return (ir > full) ? 0 : (full - ir);
289 ESP_LOGE(TAG,
"get_illuminance() caller requested an unknown channel: %d", channel);
310 if ((full_spectrum == max_count) || (infrared == max_count)) {
312 ESP_LOGW(TAG,
"Apparent saturation on '%s'; try reducing the gain or integration time", this->
name_);
316 if ((full_spectrum == 0) && (infrared == 0)) {
318 ESP_LOGW(TAG,
"Zero reading on both '%s' sensors", this->
name_);
325 switch (this->
gain_) {
354 float lux = (((float) full_spectrum - (float) infrared)) * (1.0F - ((
float) infrared / (
float) full_spectrum)) / cpl;
355 return std::max(lux, 0.0F);
375 switch (this->
gain_) {
377 if (full_spectrum < 54) {
379 }
else if (full_spectrum < 875) {
384 if (full_spectrum < 57) {
386 }
else if (full_spectrum < 1365) {
388 }
else if (full_spectrum > 62000 / fs_divider) {
393 if (full_spectrum < 920) {
395 }
else if (full_spectrum > 62000 / fs_divider) {
400 if (full_spectrum > 62000 / fs_divider)
405 if (this->
gain_ != new_gain) {
406 this->
gain_ = new_gain;
409 ESP_LOGD(TAG,
"Gain setting: %d", this->
gain_);
417 switch (this->
gain_) {
bool cancel_interval(const char *name)
Cancel an interval function.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
void status_clear_warning()
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
uint64_t interval_timeout_
TSL2591IntegrationTime integration_time_
void dump_config() override
Used by ESPHome framework.
TSL2591ComponentGain component_gain_
void automatic_gain_update(uint16_t full_spectrum)
Updates the gain setting based on the most recent full spectrum reading.
bool is_adc_valid()
Are the device ADC values valid?
float get_calculated_lux(uint16_t full_spectrum, uint16_t infrared)
Calculates and returns a lux value based on the ADC readings.
void set_device_and_glass_attenuation_factors(float device_factor, float glass_attenuation_factor)
Sets the device and glass attenuation factors.
uint32_t get_combined_illuminance()
Get the combined illuminance value.
void interval_function_for_update_()
void disable_if_power_saving_()
float glass_attenuation_factor_
void enable()
Powers on the TSL2591 device and enables its sensors.
void update() override
Used by ESPHome framework.
void set_integration_time_and_gain(TSL2591IntegrationTime integration_time, TSL2591Gain gain)
Set device integration time and gain.
void disable()
Powers off the TSL2591 device.
float get_actual_gain()
Reads the actual gain used.
uint16_t get_illuminance(TSL2591SensorChannel channel)
Get an individual sensor channel reading.
bool power_save_mode_enabled_
void setup() override
Used by ESPHome framework.
IntegrationTime501 integration_time
TSL2591Gain
Enum listing all gain settings for the TSL2591.
TSL2591ComponentGain
Enum listing all gain settings for the TSL2591 component.
TSL2591IntegrationTime
Enum listing all conversion/integration time settings for the TSL2591.
@ TSL2591_INTEGRATION_TIME_100MS
TSL2591SensorChannel
Enum listing sensor channels.
@ TSL2591_SENSOR_CHANNEL_INFRARED
@ TSL2591_SENSOR_CHANNEL_VISIBLE
@ TSL2591_SENSOR_CHANNEL_FULL_SPECTRUM
ESPHOME_LOG_TAG(TAG, "tsl2591.sensor")
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()