Tsl2591 gain publish (#4291)

fixes https://github.com/esphome/issues/issues/4031
This commit is contained in:
Z3LIFF
2023-01-17 16:17:31 -05:00
committed by GitHub
parent 1bb90f304c
commit ddedc1cd76
4 changed files with 55 additions and 5 deletions
+33 -3
View File
@@ -130,6 +130,7 @@ void TSL2591Component::dump_config() {
LOG_SENSOR(" ", "Infrared:", this->infrared_sensor_);
LOG_SENSOR(" ", "Visible:", this->visible_sensor_);
LOG_SENSOR(" ", "Calculated lux:", this->calculated_lux_sensor_);
LOG_SENSOR(" ", "Actual gain:", this->actual_gain_sensor_);
LOG_UPDATE_INTERVAL(this);
}
@@ -140,8 +141,9 @@ void TSL2591Component::process_update_() {
uint16_t infrared = this->get_illuminance(TSL2591_SENSOR_CHANNEL_INFRARED, combined);
uint16_t full = this->get_illuminance(TSL2591_SENSOR_CHANNEL_FULL_SPECTRUM, combined);
float lux = this->get_calculated_lux(full, infrared);
ESP_LOGD(TAG, "Got illuminance: combined 0x%X, full %d, IR %d, vis %d. Calc lux: %f", combined, full, infrared,
visible, lux);
uint16_t actual_gain = this->get_actual_gain();
ESP_LOGD(TAG, "Got illuminance: combined 0x%X, full %d, IR %d, vis %d. Calc lux: %f. Actual gain: %d.", combined,
full, infrared, visible, lux, actual_gain);
if (this->full_spectrum_sensor_ != nullptr) {
this->full_spectrum_sensor_->publish_state(full);
}
@@ -154,9 +156,14 @@ void TSL2591Component::process_update_() {
if (this->calculated_lux_sensor_ != nullptr) {
this->calculated_lux_sensor_->publish_state(lux);
}
if (this->component_gain_ == TSL2591_CGAIN_AUTO) {
this->automatic_gain_update(full);
}
if (this->actual_gain_sensor_ != nullptr) {
this->actual_gain_sensor_->publish_state(actual_gain);
}
this->status_clear_warning();
}
@@ -207,6 +214,10 @@ void TSL2591Component::set_calculated_lux_sensor(sensor::Sensor *calculated_lux_
this->calculated_lux_sensor_ = calculated_lux_sensor;
}
void TSL2591Component::set_actual_gain_sensor(sensor::Sensor *actual_gain_sensor) {
this->actual_gain_sensor_ = actual_gain_sensor;
}
void TSL2591Component::set_integration_time(TSL2591IntegrationTime integration_time) {
this->integration_time_ = integration_time;
}
@@ -377,7 +388,6 @@ float TSL2591Component::get_calculated_lux(uint16_t full_spectrum, uint16_t infr
again = 1.0F;
break;
}
// This lux equation is copied from the Adafruit TSL2591 v1.4.0 and modified slightly.
// See: https://github.com/adafruit/Adafruit_TSL2591_Library/issues/14
// and that library code.
@@ -448,5 +458,25 @@ void TSL2591Component::automatic_gain_update(uint16_t full_spectrum) {
ESP_LOGD(TAG, "Gain setting: %d", this->gain_);
}
/** Reads the actual gain used
*
* Useful for exposing the real gain used when configured in "auto" gain mode
*/
float TSL2591Component::get_actual_gain() {
switch (this->gain_) {
case TSL2591_GAIN_LOW:
return 1.0F;
case TSL2591_GAIN_MED:
return 25.0F;
case TSL2591_GAIN_HIGH:
return 400.0F;
case TSL2591_GAIN_MAX:
return 9500.0F;
default:
// Shouldn't get here, but just in case.
return NAN;
}
}
} // namespace tsl2591
} // namespace esphome