7 static const char *
const TAG =
"tsl2561";
9 static const uint8_t TSL2561_COMMAND_BIT = 0x80;
10 static const uint8_t TSL2561_WORD_BIT = 0x20;
11 static const uint8_t TSL2561_REGISTER_CONTROL = 0x00;
12 static const uint8_t TSL2561_REGISTER_TIMING = 0x01;
13 static const uint8_t TSL2561_REGISTER_ID = 0x0A;
14 static const uint8_t TSL2561_REGISTER_DATA_0 = 0x0C;
15 static const uint8_t TSL2561_REGISTER_DATA_1 = 0x0E;
18 ESP_LOGCONFIG(TAG,
"Setting up TSL2561...");
31 timing &= ~0b00010000;
34 timing &= ~0b00000011;
40 LOG_SENSOR(
"",
"TSL2561",
this);
44 ESP_LOGE(TAG,
"Communication with TSL2561 failed!");
48 ESP_LOGCONFIG(TAG,
" Gain: %dx", gain);
51 LOG_UPDATE_INTERVAL(
this);
67 if ((ch0 == 0xFFFF) || (ch1 == 0xFFFF)) {
68 ESP_LOGW(TAG,
"TSL2561 sensor is saturated.");
72 float d0 = ch0, d1 = ch1;
73 float ratio = d1 / d0;
86 return 0.0315f * d0 - 0.0593f * d0 * powf(ratio, 1.4f);
87 }
else if (ratio < 0.65f) {
88 return 0.0229f * d0 - 0.0291f * d1;
89 }
else if (ratio < 0.80f) {
90 return 0.0157f * d0 - 0.0153f * d1;
91 }
else if (ratio < 1.30f) {
92 return 0.00338f * d0 - 0.00260f * d1;
98 return 0.0304f * d0 - 0.062f * d0 * powf(ratio, 1.4f);
99 }
else if (ratio < 0.61f) {
100 return 0.0224f * d0 - 0.031f * d1;
101 }
else if (ratio < 0.80f) {
102 return 0.0128f * d0 - 0.0153f * d1;
103 }
else if (ratio < 1.30f) {
104 return 0.00146f * d0 - 0.00112f * d1;
110 uint16_t data1, data0;
111 if (!this->
tsl2561_read_uint(TSL2561_WORD_BIT | TSL2561_REGISTER_DATA_1, &data1)) {
115 if (!this->
tsl2561_read_uint(TSL2561_WORD_BIT | TSL2561_REGISTER_DATA_0, &data0)) {
126 ESP_LOGD(TAG,
"Got illuminance=%.1flx", lx);
148 return this->
write_byte(a_register | TSL2561_COMMAND_BIT, value);
152 if (!this->
read_bytes(a_register | TSL2561_COMMAND_BIT, data, 2))
154 const uint16_t hi = data[1];
155 const uint16_t lo = data[0];
156 *value = (hi << 8) | lo;
160 return this->
read_byte(a_register | TSL2561_COMMAND_BIT, value);
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
const float DATA
For components that import data from directly connected sensors like DHT.
void set_integration_time(TSL2561IntegrationTime integration_time)
Set the time that sensor values should be accumulated for.
void status_set_warning(const char *message="unspecified")
float calculate_lx_(uint16_t ch0, uint16_t ch1)
float get_integration_time_ms_()
void set_gain(TSL2561Gain gain)
Set the internal gain of the sensor.
bool tsl2561_read_byte(uint8_t a_register, uint8_t *value)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
void set_is_cs_package(bool package_cs)
The "CS" package of this sensor has a slightly different formula for converting the raw values...
TSL2561IntegrationTime integration_time_
bool tsl2561_write_byte(uint8_t a_register, uint8_t value)
TSL2561IntegrationTime
Enum listing all conversion/integration time settings for the TSL2561.
TSL2561Gain
Enum listing all gain settings for the TSL2561.
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
IntegrationTime501 integration_time
float get_setup_priority() const override
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
void dump_config() override
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
bool tsl2561_read_uint(uint8_t a_register, uint16_t *value)