mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 09:44:12 +01:00
Add support for VEML3235 lux sensor (#5959)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
596943b683
commit
0cd232cdf5
6 changed files with 434 additions and 0 deletions
|
@ -362,6 +362,7 @@ esphome/components/ufire_ec/* @pvizeli
|
|||
esphome/components/ufire_ise/* @pvizeli
|
||||
esphome/components/ultrasonic/* @OttoWinter
|
||||
esphome/components/vbus/* @ssieb
|
||||
esphome/components/veml3235/* @kbx81
|
||||
esphome/components/version/* @esphome/core
|
||||
esphome/components/voice_assistant/* @jesserockz
|
||||
esphome/components/wake_on_lan/* @willwill2will54
|
||||
|
|
0
esphome/components/veml3235/__init__.py
Normal file
0
esphome/components/veml3235/__init__.py
Normal file
84
esphome/components/veml3235/sensor.py
Normal file
84
esphome/components/veml3235/sensor.py
Normal file
|
@ -0,0 +1,84 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import (
|
||||
CONF_GAIN,
|
||||
CONF_INTEGRATION_TIME,
|
||||
DEVICE_CLASS_ILLUMINANCE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_LUX,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@kbx81"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
CONF_AUTO_GAIN = "auto_gain"
|
||||
CONF_AUTO_GAIN_THRESHOLD_HIGH = "auto_gain_threshold_high"
|
||||
CONF_AUTO_GAIN_THRESHOLD_LOW = "auto_gain_threshold_low"
|
||||
CONF_DIGITAL_GAIN = "digital_gain"
|
||||
|
||||
veml3235_ns = cg.esphome_ns.namespace("veml3235")
|
||||
|
||||
VEML3235Sensor = veml3235_ns.class_(
|
||||
"VEML3235Sensor", sensor.Sensor, cg.PollingComponent, i2c.I2CDevice
|
||||
)
|
||||
VEML3235IntegrationTime = veml3235_ns.enum("VEML3235IntegrationTime")
|
||||
VEML3235_INTEGRATION_TIMES = {
|
||||
"50ms": VEML3235IntegrationTime.VEML3235_INTEGRATION_TIME_50MS,
|
||||
"100ms": VEML3235IntegrationTime.VEML3235_INTEGRATION_TIME_100MS,
|
||||
"200ms": VEML3235IntegrationTime.VEML3235_INTEGRATION_TIME_200MS,
|
||||
"400ms": VEML3235IntegrationTime.VEML3235_INTEGRATION_TIME_400MS,
|
||||
"800ms": VEML3235IntegrationTime.VEML3235_INTEGRATION_TIME_800MS,
|
||||
}
|
||||
VEML3235ComponentDigitalGain = veml3235_ns.enum("VEML3235ComponentDigitalGain")
|
||||
DIGITAL_GAINS = {
|
||||
"1X": VEML3235ComponentDigitalGain.VEML3235_DIGITAL_GAIN_1X,
|
||||
"2X": VEML3235ComponentDigitalGain.VEML3235_DIGITAL_GAIN_2X,
|
||||
}
|
||||
VEML3235ComponentGain = veml3235_ns.enum("VEML3235ComponentGain")
|
||||
GAINS = {
|
||||
"1X": VEML3235ComponentGain.VEML3235_GAIN_1X,
|
||||
"2X": VEML3235ComponentGain.VEML3235_GAIN_2X,
|
||||
"4X": VEML3235ComponentGain.VEML3235_GAIN_4X,
|
||||
"AUTO": VEML3235ComponentGain.VEML3235_GAIN_AUTO,
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(
|
||||
VEML3235Sensor,
|
||||
unit_of_measurement=UNIT_LUX,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_ILLUMINANCE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.Optional(CONF_DIGITAL_GAIN, default="1X"): cv.enum(
|
||||
DIGITAL_GAINS, upper=True
|
||||
),
|
||||
cv.Optional(CONF_AUTO_GAIN, default=True): cv.boolean,
|
||||
cv.Optional(CONF_AUTO_GAIN_THRESHOLD_HIGH, default="90%"): cv.percentage,
|
||||
cv.Optional(CONF_AUTO_GAIN_THRESHOLD_LOW, default="20%"): cv.percentage,
|
||||
cv.Optional(CONF_GAIN, default="1X"): cv.enum(GAINS, upper=True),
|
||||
cv.Optional(CONF_INTEGRATION_TIME, default="50ms"): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.enum(VEML3235_INTEGRATION_TIMES, lower=True),
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x10))
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await sensor.new_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
cg.add(var.set_auto_gain(config[CONF_AUTO_GAIN]))
|
||||
cg.add(var.set_auto_gain_threshold_high(config[CONF_AUTO_GAIN_THRESHOLD_HIGH]))
|
||||
cg.add(var.set_auto_gain_threshold_low(config[CONF_AUTO_GAIN_THRESHOLD_LOW]))
|
||||
cg.add(var.set_digital_gain(DIGITAL_GAINS[config[CONF_DIGITAL_GAIN]]))
|
||||
cg.add(var.set_gain(GAINS[config[CONF_GAIN]]))
|
||||
cg.add(var.set_integration_time(config[CONF_INTEGRATION_TIME]))
|
230
esphome/components/veml3235/veml3235.cpp
Normal file
230
esphome/components/veml3235/veml3235.cpp
Normal file
|
@ -0,0 +1,230 @@
|
|||
#include "veml3235.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace veml3235 {
|
||||
|
||||
static const char *const TAG = "veml3235.sensor";
|
||||
|
||||
void VEML3235Sensor::setup() {
|
||||
uint8_t device_id[] = {0, 0};
|
||||
|
||||
ESP_LOGCONFIG(TAG, "Setting up VEML3235 '%s'...", this->name_.c_str());
|
||||
|
||||
if (!this->refresh_config_reg()) {
|
||||
ESP_LOGE(TAG, "Unable to write configuration");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
if ((this->write(&ID_REG, 1, false) != i2c::ERROR_OK) || !this->read_bytes_raw(device_id, 2)) {
|
||||
ESP_LOGE(TAG, "Unable to read ID");
|
||||
this->mark_failed();
|
||||
return;
|
||||
} else if (device_id[0] != DEVICE_ID) {
|
||||
ESP_LOGE(TAG, "Incorrect device ID - expected 0x%.2x, read 0x%.2x", DEVICE_ID, device_id[0]);
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool VEML3235Sensor::refresh_config_reg(bool force_on) {
|
||||
uint16_t data = this->power_on_ || force_on ? 0 : SHUTDOWN_BITS;
|
||||
|
||||
data |= (uint16_t(this->integration_time_ << CONFIG_REG_IT_BIT));
|
||||
data |= (uint16_t(this->digital_gain_ << CONFIG_REG_DG_BIT));
|
||||
data |= (uint16_t(this->gain_ << CONFIG_REG_G_BIT));
|
||||
data |= 0x1; // mandatory 1 here per RM
|
||||
|
||||
ESP_LOGVV(TAG, "Writing 0x%.4x to register 0x%.2x", data, CONFIG_REG);
|
||||
return this->write_byte_16(CONFIG_REG, data);
|
||||
}
|
||||
|
||||
float VEML3235Sensor::read_lx_() {
|
||||
if (!this->power_on_) { // if off, turn on
|
||||
if (!this->refresh_config_reg(true)) {
|
||||
ESP_LOGW(TAG, "Turning on failed");
|
||||
this->status_set_warning();
|
||||
return NAN;
|
||||
}
|
||||
delay(4); // from RM: a wait time of 4 ms should be observed before the first measurement is picked up, to allow
|
||||
// for a correct start of the signal processor and oscillator
|
||||
}
|
||||
|
||||
uint8_t als_regs[] = {0, 0};
|
||||
if ((this->write(&ALS_REG, 1, false) != i2c::ERROR_OK) || !this->read_bytes_raw(als_regs, 2)) {
|
||||
this->status_set_warning();
|
||||
return NAN;
|
||||
}
|
||||
|
||||
this->status_clear_warning();
|
||||
|
||||
float als_raw_value_multiplier = LUX_MULTIPLIER_BASE;
|
||||
uint16_t als_raw_value = encode_uint16(als_regs[1], als_regs[0]);
|
||||
// determine multiplier value based on gains and integration time
|
||||
if (this->digital_gain_ == VEML3235_DIGITAL_GAIN_1X) {
|
||||
als_raw_value_multiplier *= 2;
|
||||
}
|
||||
switch (this->gain_) {
|
||||
case VEML3235_GAIN_1X:
|
||||
als_raw_value_multiplier *= 4;
|
||||
break;
|
||||
case VEML3235_GAIN_2X:
|
||||
als_raw_value_multiplier *= 2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (this->integration_time_) {
|
||||
case VEML3235_INTEGRATION_TIME_50MS:
|
||||
als_raw_value_multiplier *= 16;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_100MS:
|
||||
als_raw_value_multiplier *= 8;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_200MS:
|
||||
als_raw_value_multiplier *= 4;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_400MS:
|
||||
als_raw_value_multiplier *= 2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// finally, determine and return the actual lux value
|
||||
float lx = float(als_raw_value) * als_raw_value_multiplier;
|
||||
ESP_LOGVV(TAG, "'%s': ALS raw = %u, multiplier = %.5f", this->get_name().c_str(), als_raw_value,
|
||||
als_raw_value_multiplier);
|
||||
ESP_LOGD(TAG, "'%s': Illuminance = %.4flx", this->get_name().c_str(), lx);
|
||||
|
||||
if (!this->power_on_) { // turn off if required
|
||||
if (!this->refresh_config_reg()) {
|
||||
ESP_LOGW(TAG, "Turning off failed");
|
||||
this->status_set_warning();
|
||||
}
|
||||
}
|
||||
|
||||
if (this->auto_gain_) {
|
||||
this->adjust_gain_(als_raw_value);
|
||||
}
|
||||
|
||||
return lx;
|
||||
}
|
||||
|
||||
void VEML3235Sensor::adjust_gain_(const uint16_t als_raw_value) {
|
||||
if ((als_raw_value > UINT16_MAX * this->auto_gain_threshold_low_) &&
|
||||
(als_raw_value < UINT16_MAX * this->auto_gain_threshold_high_)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (als_raw_value >= UINT16_MAX * 0.9) { // over-saturated, reset all gains and start over
|
||||
this->digital_gain_ = VEML3235_DIGITAL_GAIN_1X;
|
||||
this->gain_ = VEML3235_GAIN_1X;
|
||||
this->integration_time_ = VEML3235_INTEGRATION_TIME_50MS;
|
||||
this->refresh_config_reg();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->gain_ != VEML3235_GAIN_4X) { // increase gain if possible
|
||||
switch (this->gain_) {
|
||||
case VEML3235_GAIN_1X:
|
||||
this->gain_ = VEML3235_GAIN_2X;
|
||||
break;
|
||||
case VEML3235_GAIN_2X:
|
||||
this->gain_ = VEML3235_GAIN_4X;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
this->refresh_config_reg();
|
||||
return;
|
||||
}
|
||||
// gain is maxed out; reset it and try to increase digital gain
|
||||
if (this->digital_gain_ != VEML3235_DIGITAL_GAIN_2X) { // increase digital gain if possible
|
||||
this->digital_gain_ = VEML3235_DIGITAL_GAIN_2X;
|
||||
this->gain_ = VEML3235_GAIN_1X;
|
||||
this->refresh_config_reg();
|
||||
return;
|
||||
}
|
||||
// digital gain is maxed out; reset it and try to increase integration time
|
||||
if (this->integration_time_ != VEML3235_INTEGRATION_TIME_800MS) { // increase integration time if possible
|
||||
switch (this->integration_time_) {
|
||||
case VEML3235_INTEGRATION_TIME_50MS:
|
||||
this->integration_time_ = VEML3235_INTEGRATION_TIME_100MS;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_100MS:
|
||||
this->integration_time_ = VEML3235_INTEGRATION_TIME_200MS;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_200MS:
|
||||
this->integration_time_ = VEML3235_INTEGRATION_TIME_400MS;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_400MS:
|
||||
this->integration_time_ = VEML3235_INTEGRATION_TIME_800MS;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
this->digital_gain_ = VEML3235_DIGITAL_GAIN_1X;
|
||||
this->gain_ = VEML3235_GAIN_1X;
|
||||
this->refresh_config_reg();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void VEML3235Sensor::dump_config() {
|
||||
uint8_t digital_gain = 1;
|
||||
uint8_t gain = 1;
|
||||
uint16_t integration_time = 0;
|
||||
|
||||
if (this->digital_gain_ == VEML3235_DIGITAL_GAIN_2X) {
|
||||
digital_gain = 2;
|
||||
}
|
||||
switch (this->gain_) {
|
||||
case VEML3235_GAIN_2X:
|
||||
gain = 2;
|
||||
break;
|
||||
case VEML3235_GAIN_4X:
|
||||
gain = 4;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (this->integration_time_) {
|
||||
case VEML3235_INTEGRATION_TIME_50MS:
|
||||
integration_time = 50;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_100MS:
|
||||
integration_time = 100;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_200MS:
|
||||
integration_time = 200;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_400MS:
|
||||
integration_time = 400;
|
||||
break;
|
||||
case VEML3235_INTEGRATION_TIME_800MS:
|
||||
integration_time = 800;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_SENSOR("", "VEML3235", this);
|
||||
LOG_I2C_DEVICE(this);
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, "Communication failed");
|
||||
}
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
ESP_LOGCONFIG(TAG, " Auto-gain enabled: %s", YESNO(this->auto_gain_));
|
||||
if (this->auto_gain_) {
|
||||
ESP_LOGCONFIG(TAG, " Auto-gain upper threshold: %f%%", this->auto_gain_threshold_high_ * 100.0);
|
||||
ESP_LOGCONFIG(TAG, " Auto-gain lower threshold: %f%%", this->auto_gain_threshold_low_ * 100.0);
|
||||
ESP_LOGCONFIG(TAG, " Values below will be used as initial values only");
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, " Digital gain: %uX", digital_gain);
|
||||
ESP_LOGCONFIG(TAG, " Gain: %uX", gain);
|
||||
ESP_LOGCONFIG(TAG, " Integration time: %ums", integration_time);
|
||||
}
|
||||
|
||||
} // namespace veml3235
|
||||
} // namespace esphome
|
109
esphome/components/veml3235/veml3235.h
Normal file
109
esphome/components/veml3235/veml3235.h
Normal file
|
@ -0,0 +1,109 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace veml3235 {
|
||||
|
||||
// Register IDs/locations
|
||||
//
|
||||
static const uint8_t CONFIG_REG = 0x00;
|
||||
static const uint8_t W_REG = 0x04;
|
||||
static const uint8_t ALS_REG = 0x05;
|
||||
static const uint8_t ID_REG = 0x09;
|
||||
|
||||
// Bit offsets within CONFIG_REG
|
||||
//
|
||||
static const uint8_t CONFIG_REG_IT_BIT = 12;
|
||||
static const uint8_t CONFIG_REG_DG_BIT = 5;
|
||||
static const uint8_t CONFIG_REG_G_BIT = 3;
|
||||
|
||||
// Other important constants
|
||||
//
|
||||
static const uint8_t DEVICE_ID = 0x35;
|
||||
static const uint16_t SHUTDOWN_BITS = 0x0018;
|
||||
|
||||
// Base multiplier value for lux computation
|
||||
//
|
||||
static const float LUX_MULTIPLIER_BASE = 0.00213;
|
||||
|
||||
// Enum for conversion/integration time settings for the VEML3235.
|
||||
//
|
||||
// Specific values of the enum constants are register values taken from the VEML3235 datasheet.
|
||||
// Longer times mean more accurate results, but will take more energy/more time.
|
||||
//
|
||||
enum VEML3235ComponentIntegrationTime {
|
||||
VEML3235_INTEGRATION_TIME_50MS = 0b000,
|
||||
VEML3235_INTEGRATION_TIME_100MS = 0b001,
|
||||
VEML3235_INTEGRATION_TIME_200MS = 0b010,
|
||||
VEML3235_INTEGRATION_TIME_400MS = 0b011,
|
||||
VEML3235_INTEGRATION_TIME_800MS = 0b100,
|
||||
};
|
||||
|
||||
// Enum for digital gain settings for the VEML3235.
|
||||
// Higher values are better for low light situations, but can increase noise.
|
||||
//
|
||||
enum VEML3235ComponentDigitalGain {
|
||||
VEML3235_DIGITAL_GAIN_1X = 0b0,
|
||||
VEML3235_DIGITAL_GAIN_2X = 0b1,
|
||||
};
|
||||
|
||||
// Enum for gain settings for the VEML3235.
|
||||
// Higher values are better for low light situations, but can increase noise.
|
||||
//
|
||||
enum VEML3235ComponentGain {
|
||||
VEML3235_GAIN_1X = 0b00,
|
||||
VEML3235_GAIN_2X = 0b01,
|
||||
VEML3235_GAIN_4X = 0b11,
|
||||
};
|
||||
|
||||
class VEML3235Sensor : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
void update() override { this->publish_state(this->read_lx_()); }
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
||||
// Used by ESPHome framework. Does NOT actually set the value on the device.
|
||||
void set_auto_gain(bool auto_gain) { this->auto_gain_ = auto_gain; }
|
||||
void set_auto_gain_threshold_high(float auto_gain_threshold_high) {
|
||||
this->auto_gain_threshold_high_ = auto_gain_threshold_high;
|
||||
}
|
||||
void set_auto_gain_threshold_low(float auto_gain_threshold_low) {
|
||||
this->auto_gain_threshold_low_ = auto_gain_threshold_low;
|
||||
}
|
||||
void set_power_on(bool power_on) { this->power_on_ = power_on; }
|
||||
void set_digital_gain(VEML3235ComponentDigitalGain digital_gain) { this->digital_gain_ = digital_gain; }
|
||||
void set_gain(VEML3235ComponentGain gain) { this->gain_ = gain; }
|
||||
void set_integration_time(VEML3235ComponentIntegrationTime integration_time) {
|
||||
this->integration_time_ = integration_time;
|
||||
}
|
||||
|
||||
bool auto_gain() { return this->auto_gain_; }
|
||||
float auto_gain_threshold_high() { return this->auto_gain_threshold_high_; }
|
||||
float auto_gain_threshold_low() { return this->auto_gain_threshold_low_; }
|
||||
VEML3235ComponentDigitalGain digital_gain() { return this->digital_gain_; }
|
||||
VEML3235ComponentGain gain() { return this->gain_; }
|
||||
VEML3235ComponentIntegrationTime integration_time() { return this->integration_time_; }
|
||||
|
||||
// Updates the configuration register on the device
|
||||
bool refresh_config_reg(bool force_on = false);
|
||||
|
||||
protected:
|
||||
float read_lx_();
|
||||
void adjust_gain_(uint16_t als_raw_value);
|
||||
|
||||
bool auto_gain_{true};
|
||||
bool power_on_{true};
|
||||
float auto_gain_threshold_high_{0.9};
|
||||
float auto_gain_threshold_low_{0.2};
|
||||
VEML3235ComponentDigitalGain digital_gain_{VEML3235_DIGITAL_GAIN_1X};
|
||||
VEML3235ComponentGain gain_{VEML3235_GAIN_1X};
|
||||
VEML3235ComponentIntegrationTime integration_time_{VEML3235_INTEGRATION_TIME_50MS};
|
||||
};
|
||||
|
||||
} // namespace veml3235
|
||||
} // namespace esphome
|
|
@ -1370,6 +1370,16 @@ sensor:
|
|||
name: tsl2591 calculated_lux
|
||||
id: tsl2591_cl
|
||||
i2c_id: i2c_bus
|
||||
- platform: veml3235
|
||||
id: veml3235_sensor
|
||||
name: VEML3235 Light Sensor
|
||||
i2c_id: i2c_bus
|
||||
auto_gain: true
|
||||
auto_gain_threshold_high: 90%
|
||||
auto_gain_threshold_low: 15%
|
||||
digital_gain: 1X
|
||||
gain: 1X
|
||||
integration_time: 50ms
|
||||
- platform: tee501
|
||||
name: Office Temperature 3
|
||||
address: 0x48
|
||||
|
|
Loading…
Reference in a new issue