mirror of
https://github.com/esphome/esphome.git
synced 2025-01-05 12:21:43 +01:00
LTR390 - Multiple bugfixes (#6161)
This commit is contained in:
parent
d96090095a
commit
e847039ffd
3 changed files with 29 additions and 21 deletions
|
@ -8,10 +8,23 @@ namespace ltr390 {
|
||||||
|
|
||||||
static const char *const TAG = "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 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 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 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 little_endian_bytes_to_int(const uint8_t *buffer, uint8_t num_bytes) {
|
||||||
uint32_t value = 0;
|
uint32_t value = 0;
|
||||||
|
|
||||||
|
@ -58,7 +71,7 @@ void LTR390Component::read_als_() {
|
||||||
uint32_t als = *val;
|
uint32_t als = *val;
|
||||||
|
|
||||||
if (this->light_sensor_ != nullptr) {
|
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);
|
this->light_sensor_->publish_state(lux);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +87,7 @@ void LTR390Component::read_uvs_() {
|
||||||
uint32_t uv = *val;
|
uint32_t uv = *val;
|
||||||
|
|
||||||
if (this->uvi_sensor_ != nullptr) {
|
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) {
|
if (this->uv_sensor_ != nullptr) {
|
||||||
|
@ -132,12 +145,13 @@ void LTR390Component::setup() {
|
||||||
// Set gain
|
// Set gain
|
||||||
this->reg(LTR390_GAIN) = gain_;
|
this->reg(LTR390_GAIN) = gain_;
|
||||||
|
|
||||||
// Set resolution
|
// Set resolution and measurement rate
|
||||||
uint8_t res = this->reg(LTR390_MEAS_RATE).get();
|
this->reg(LTR390_MEAS_RATE) = RESOLUTION_SETTING[this->res_];
|
||||||
// resolution is in bits 5-7
|
|
||||||
res &= ~0b01110000;
|
// Set sensitivity by linearly scaling against known value in the datasheet
|
||||||
res |= res << 4;
|
float gain_scale = GAINVALUES[this->gain_] / GAIN_MAX;
|
||||||
this->reg(LTR390_MEAS_RATE) = res;
|
float intg_scale = (RESOLUTIONVALUE[this->res_] * 100) / INTG_MAX;
|
||||||
|
this->sensitivity_ = SENSITIVITY_MAX * gain_scale * intg_scale;
|
||||||
|
|
||||||
// Set sensor read state
|
// Set sensor read state
|
||||||
this->reading_ = false;
|
this->reading_ = false;
|
||||||
|
|
|
@ -17,14 +17,6 @@ enum LTR390CTRL {
|
||||||
};
|
};
|
||||||
|
|
||||||
// enums from https://github.com/adafruit/Adafruit_LTR390/
|
// 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
|
// Sensing modes
|
||||||
enum LTR390MODE {
|
enum LTR390MODE {
|
||||||
LTR390_MODE_ALS,
|
LTR390_MODE_ALS,
|
||||||
|
@ -81,6 +73,7 @@ class LTR390Component : public PollingComponent, public i2c::I2CDevice {
|
||||||
|
|
||||||
LTR390GAIN gain_;
|
LTR390GAIN gain_;
|
||||||
LTR390RESOLUTION res_;
|
LTR390RESOLUTION res_;
|
||||||
|
float sensitivity_;
|
||||||
float wfac_;
|
float wfac_;
|
||||||
|
|
||||||
sensor::Sensor *light_sensor_{nullptr};
|
sensor::Sensor *light_sensor_{nullptr};
|
||||||
|
|
|
@ -8,6 +8,7 @@ from esphome.const import (
|
||||||
CONF_RESOLUTION,
|
CONF_RESOLUTION,
|
||||||
UNIT_LUX,
|
UNIT_LUX,
|
||||||
ICON_BRIGHTNESS_5,
|
ICON_BRIGHTNESS_5,
|
||||||
|
DEVICE_CLASS_EMPTY,
|
||||||
DEVICE_CLASS_ILLUMINANCE,
|
DEVICE_CLASS_ILLUMINANCE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -61,22 +62,22 @@ CONFIG_SCHEMA = cv.All(
|
||||||
unit_of_measurement=UNIT_COUNTS,
|
unit_of_measurement=UNIT_COUNTS,
|
||||||
icon=ICON_BRIGHTNESS_5,
|
icon=ICON_BRIGHTNESS_5,
|
||||||
accuracy_decimals=1,
|
accuracy_decimals=1,
|
||||||
device_class=DEVICE_CLASS_ILLUMINANCE,
|
device_class=DEVICE_CLASS_EMPTY,
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_UV_INDEX): sensor.sensor_schema(
|
cv.Optional(CONF_UV_INDEX): sensor.sensor_schema(
|
||||||
unit_of_measurement=UNIT_UVI,
|
unit_of_measurement=UNIT_UVI,
|
||||||
icon=ICON_BRIGHTNESS_5,
|
icon=ICON_BRIGHTNESS_5,
|
||||||
accuracy_decimals=5,
|
accuracy_decimals=5,
|
||||||
device_class=DEVICE_CLASS_ILLUMINANCE,
|
device_class=DEVICE_CLASS_EMPTY,
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_UV): sensor.sensor_schema(
|
cv.Optional(CONF_UV): sensor.sensor_schema(
|
||||||
unit_of_measurement=UNIT_COUNTS,
|
unit_of_measurement=UNIT_COUNTS,
|
||||||
icon=ICON_BRIGHTNESS_5,
|
icon=ICON_BRIGHTNESS_5,
|
||||||
accuracy_decimals=1,
|
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_GAIN, default="X18"): cv.enum(GAIN_OPTIONS),
|
||||||
cv.Optional(CONF_RESOLUTION, default=18): cv.enum(RES_OPTIONS),
|
cv.Optional(CONF_RESOLUTION, default=20): cv.enum(RES_OPTIONS),
|
||||||
cv.Optional(CONF_WINDOW_CORRECTION_FACTOR, default=1.0): cv.float_range(
|
cv.Optional(CONF_WINDOW_CORRECTION_FACTOR, default=1.0): cv.float_range(
|
||||||
min=1.0
|
min=1.0
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in a new issue