1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-28 16:12:24 +01:00

LTR390 separate ALS and UV gain and resolution (#7026)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Anton Viktorov
2024-07-12 21:42:41 +00:00
committed by GitHub
parent 8a3f0e3b93
commit feae794787
6 changed files with 110 additions and 41 deletions

View File

@@ -19,6 +19,7 @@ static const uint8_t LTR390_MAIN_STATUS = 0x07;
static const float GAINVALUES[5] = {1.0, 3.0, 6.0, 9.0, 18.0};
static const float RESOLUTIONVALUE[6] = {4.0, 2.0, 1.0, 0.5, 0.25, 0.125};
static const uint8_t RESOLUTION_BITS[6] = {20, 19, 18, 17, 16, 13};
// Request fastest measurement rate - will be slowed by device if conversion rate is slower.
static const float RESOLUTION_SETTING[6] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
@@ -74,7 +75,7 @@ void LTR390Component::read_als_() {
uint32_t als = *val;
if (this->light_sensor_ != nullptr) {
float lux = ((0.6 * als) / (GAINVALUES[this->gain_] * RESOLUTIONVALUE[this->res_])) * this->wfac_;
float lux = ((0.6 * als) / (GAINVALUES[this->gain_als_] * RESOLUTIONVALUE[this->res_als_])) * this->wfac_;
this->light_sensor_->publish_state(lux);
}
@@ -90,7 +91,7 @@ void LTR390Component::read_uvs_() {
uint32_t uv = *val;
if (this->uvi_sensor_ != nullptr) {
this->uvi_sensor_->publish_state((uv / this->sensitivity_) * this->wfac_);
this->uvi_sensor_->publish_state((uv / this->sensitivity_uv_) * this->wfac_);
}
if (this->uv_sensor_ != nullptr) {
@@ -107,24 +108,38 @@ void LTR390Component::read_mode_(int mode_index) {
ctrl[LTR390_CTRL_EN] = true;
this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
// After the sensor integration time do the following
this->set_timeout(((uint32_t) RESOLUTIONVALUE[this->res_]) * 100 + LTR390_WAKEUP_TIME + LTR390_SETTLE_TIME,
[this, mode_index]() {
// Read from the sensor
std::get<1>(this->mode_funcs_[mode_index])();
uint32_t int_time{0};
// Set gain, resolution and measurement rate
switch (mode) {
case LTR390_MODE_ALS:
this->reg(LTR390_GAIN) = this->gain_als_;
this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_als_];
int_time = ((uint32_t) RESOLUTIONVALUE[this->res_als_]) * 100;
break;
case LTR390_MODE_UVS:
this->reg(LTR390_GAIN) = this->gain_uv_;
this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_uv_];
int_time = ((uint32_t) RESOLUTIONVALUE[this->res_uv_]) * 100;
break;
}
// If there are more modes to read then begin the next
// otherwise stop
if (mode_index + 1 < (int) this->mode_funcs_.size()) {
this->read_mode_(mode_index + 1);
} else {
// put sensor in standby
std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
ctrl[LTR390_CTRL_EN] = false;
this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
this->reading_ = false;
}
});
// After the sensor integration time do the following
this->set_timeout(int_time + LTR390_WAKEUP_TIME + LTR390_SETTLE_TIME, [this, mode_index]() {
// Read from the sensor
std::get<1>(this->mode_funcs_[mode_index])();
// If there are more modes to read then begin the next
// otherwise stop
if (mode_index + 1 < (int) this->mode_funcs_.size()) {
this->read_mode_(mode_index + 1);
} else {
// put sensor in standby
std::bitset<8> ctrl = this->reg(LTR390_MAIN_CTRL).get();
ctrl[LTR390_CTRL_EN] = false;
this->reg(LTR390_MAIN_CTRL) = ctrl.to_ulong();
this->reading_ = false;
}
});
}
void LTR390Component::setup() {
@@ -151,16 +166,10 @@ void LTR390Component::setup() {
return;
}
// Set gain
this->reg(LTR390_GAIN) = gain_;
// Set resolution and measurement rate
this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_];
// Set sensitivity by linearly scaling against known value in the datasheet
float gain_scale = GAINVALUES[this->gain_] / GAIN_MAX;
float intg_scale = (RESOLUTIONVALUE[this->res_] * 100) / INTG_MAX;
this->sensitivity_ = SENSITIVITY_MAX * gain_scale * intg_scale;
float gain_scale_uv = GAINVALUES[this->gain_uv_] / GAIN_MAX;
float intg_scale_uv = (RESOLUTIONVALUE[this->res_uv_] * 100) / INTG_MAX;
this->sensitivity_uv_ = SENSITIVITY_MAX * gain_scale_uv * intg_scale_uv;
// Set sensor read state
this->reading_ = false;
@@ -176,7 +185,13 @@ void LTR390Component::setup() {
}
}
void LTR390Component::dump_config() { LOG_I2C_DEVICE(this); }
void LTR390Component::dump_config() {
LOG_I2C_DEVICE(this);
ESP_LOGCONFIG(TAG, " ALS Gain: X%.0f", GAINVALUES[this->gain_als_]);
ESP_LOGCONFIG(TAG, " ALS Resolution: %u-bit", RESOLUTION_BITS[this->res_als_]);
ESP_LOGCONFIG(TAG, " UV Gain: X%.0f", GAINVALUES[this->gain_uv_]);
ESP_LOGCONFIG(TAG, " UV Resolution: %u-bit", RESOLUTION_BITS[this->res_uv_]);
}
void LTR390Component::update() {
if (!this->reading_ && !mode_funcs_.empty()) {