LTR390 - Multiple bugfixes (#6161)

This commit is contained in:
Stephen Tierney 2024-02-21 15:10:04 +11:00 committed by GitHub
parent d96090095a
commit e847039ffd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 21 deletions

View file

@ -8,10 +8,23 @@ namespace ltr390 {
static const char *const TAG = "ltr390";
static const uint8_t LTR390_MAIN_CTRL = 0x00;
static const uint8_t LTR390_MEAS_RATE = 0x04;
static const uint8_t LTR390_GAIN = 0x05;
static const uint8_t LTR390_PART_ID = 0x06;
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};
// 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};
static const uint32_t MODEADDRESSES[2] = {0x0D, 0x10};
static const float SENSITIVITY_MAX = 2300;
static const float INTG_MAX = RESOLUTIONVALUE[0] * 100;
static const int GAIN_MAX = GAINVALUES[4];
uint32_t little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes) {
uint32_t value = 0;
@ -58,7 +71,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_] * RESOLUTIONVALUE[this->res_])) * this->wfac_;
this->light_sensor_->publish_state(lux);
}
@ -74,7 +87,7 @@ void LTR390Component::read_uvs_() {
uint32_t uv = *val;
if (this->uvi_sensor_ != nullptr) {
this->uvi_sensor_->publish_state(uv / LTR390_SENSITIVITY * this->wfac_);
this->uvi_sensor_->publish_state((uv / this->sensitivity_) * this->wfac_);
}
if (this->uv_sensor_ != nullptr) {
@ -132,12 +145,13 @@ void LTR390Component::setup() {
// Set gain
this->reg(LTR390_GAIN) = gain_;
// Set resolution
uint8_t res = this->reg(LTR390_MEAS_RATE).get();
// resolution is in bits 5-7
res &= ~0b01110000;
res |= res << 4;
this->reg(LTR390_MEAS_RATE) = res;
// 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;
// Set sensor read state
this->reading_ = false;

View file

@ -17,14 +17,6 @@ enum LTR390CTRL {
};
// enums from https://github.com/adafruit/Adafruit_LTR390/
static const uint8_t LTR390_MAIN_CTRL = 0x00;
static const uint8_t LTR390_MEAS_RATE = 0x04;
static const uint8_t LTR390_GAIN = 0x05;
static const uint8_t LTR390_PART_ID = 0x06;
static const uint8_t LTR390_MAIN_STATUS = 0x07;
static const float LTR390_SENSITIVITY = 2300.0;
// Sensing modes
enum LTR390MODE {
LTR390_MODE_ALS,
@ -81,6 +73,7 @@ class LTR390Component : public PollingComponent, public i2c::I2CDevice {
LTR390GAIN gain_;
LTR390RESOLUTION res_;
float sensitivity_;
float wfac_;
sensor::Sensor *light_sensor_{nullptr};

View file

@ -8,6 +8,7 @@ from esphome.const import (
CONF_RESOLUTION,
UNIT_LUX,
ICON_BRIGHTNESS_5,
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_ILLUMINANCE,
)
@ -61,22 +62,22 @@ CONFIG_SCHEMA = cv.All(
unit_of_measurement=UNIT_COUNTS,
icon=ICON_BRIGHTNESS_5,
accuracy_decimals=1,
device_class=DEVICE_CLASS_ILLUMINANCE,
device_class=DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_UV_INDEX): sensor.sensor_schema(
unit_of_measurement=UNIT_UVI,
icon=ICON_BRIGHTNESS_5,
accuracy_decimals=5,
device_class=DEVICE_CLASS_ILLUMINANCE,
device_class=DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_UV): sensor.sensor_schema(
unit_of_measurement=UNIT_COUNTS,
icon=ICON_BRIGHTNESS_5,
accuracy_decimals=1,
device_class=DEVICE_CLASS_ILLUMINANCE,
device_class=DEVICE_CLASS_EMPTY,
),
cv.Optional(CONF_GAIN, default="X3"): cv.enum(GAIN_OPTIONS),
cv.Optional(CONF_RESOLUTION, default=18): cv.enum(RES_OPTIONS),
cv.Optional(CONF_GAIN, default="X18"): cv.enum(GAIN_OPTIONS),
cv.Optional(CONF_RESOLUTION, default=20): cv.enum(RES_OPTIONS),
cv.Optional(CONF_WINDOW_CORRECTION_FACTOR, default=1.0): cv.float_range(
min=1.0
),