9static constexpr uint32_t MEASUREMENT_ID = 0;
11static constexpr uint8_t DEFAULT_ADDRESS = 0x29;
12static constexpr uint8_t MODEL_ID = 0xB4;
14static constexpr uint16_t REG_IDENTIFICATION_MODEL_ID = 0x000;
15static constexpr uint16_t REG_SYSTEM_INTERRUPT_CLEAR = 0x015;
16static constexpr uint16_t REG_SYSTEM_FRESH_OUT_OF_RESET = 0x016;
17static constexpr uint16_t REG_SYSRANGE_START = 0x018;
18static constexpr uint16_t REG_SYSRANGE_CROSSTALK_VALID_HEIGHT = 0x021;
19static constexpr uint16_t REG_SYSRANGE_PART_TO_PART_OFFSET = 0x024;
20static constexpr uint16_t REG_SYSRANGE_RANGE_CHECK_ENABLES = 0x02D;
21static constexpr uint16_t REG_SYSALS_START = 0x038;
22static constexpr uint16_t REG_SYSALS_ANALOGUE_GAIN = 0x03F;
23static constexpr uint16_t REG_SYSALS_INTEGRATION_PERIOD = 0x040;
24static constexpr uint16_t REG_RESULT_RANGE_STATUS = 0x04D;
25static constexpr uint16_t REG_RESULT_ALS_STATUS = 0x04E;
26static constexpr uint16_t REG_RESULT_RANGE_VAL = 0x062;
27static constexpr uint16_t REG_RANGE_SCALING = 0x096;
28static constexpr uint16_t REG_I2C_DEVICE_ADDRESS = 0x212;
31static constexpr uint8_t RANGE_SAMPLE_READY = 0x04;
32static constexpr uint8_t ALS_SAMPLE_READY = 0x04 << 3;
35static constexpr uint32_t RANGE_TIME_MS = 70;
36static constexpr uint32_t ALS_READOUT_MARGIN_MS = 400;
39static constexpr uint16_t RANGE_SCALING_VALUES[] PROGMEM = {0, 253, 127, 84};
47static constexpr RegValue MANDATORY_SETTINGS[] PROGMEM = {
78 {REG_SYSTEM_FRESH_OUT_OF_RESET, 0x00},
82static constexpr RegValue DEFAULT_SETTINGS[]
PROGMEM = {
88 {0x0014, RANGE_SAMPLE_READY | ALS_SAMPLE_READY},
89 {REG_SYSTEM_INTERRUPT_CLEAR, 0x07},
92template<
size_t N>
static bool write_settings(i2c::I2CDevice *device,
const RegValue (&table)[N]) {
93 for (
const auto &entry : table) {
96 if (!device->write_byte_reg16(setting.reg, setting.value))
102#ifdef VL6180X_ENABLE_PIN_COUNT
107#ifdef VL6180X_ENABLE_PIN_COUNT
120 uint8_t model_id = 0;
121 const bool read_ok = this->
read_byte_reg16(REG_IDENTIFICATION_MODEL_ID, &model_id);
122 const bool ok = read_ok && model_id == MODEL_ID && this->
init_() &&
127#ifdef VL6180X_ENABLE_PIN_COUNT
132 if (read_ok && model_id != MODEL_ID) {
133 ESP_LOGE(TAG,
"Wrong model ID 0x%02X", model_id);
148 int16_t part_offset =
static_cast<int8_t
>(offset);
150 if (!write_settings(
this, MANDATORY_SETTINGS))
157 for (uint8_t scaler = 2; scaler <= 3; scaler++) {
159 part_offset *= scaler;
167 return write_settings(
this, DEFAULT_SETTINGS) &&
171 this->
write_byte_reg16(REG_SYSRANGE_PART_TO_PART_OFFSET,
static_cast<uint8_t
>(part_offset / this->scaler_)) &&
172 this->
write_byte_reg16(REG_SYSRANGE_CROSSTALK_VALID_HEIGHT, 20 / this->scaler_) &&
174 this->
write_byte_reg16(REG_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | (this->scaler_ == 1 ? 0x01 : 0x00));
177void VL6180XComponent::update() {
182 if (this->distance_sensor_ !=
nullptr) {
207 const uint8_t error =
status[0] >> 4;
209 if (error >= 1 && error <= 5) {
210 ESP_LOGW(TAG,
"Range self-test fault %u", error);
211 PUBLISH_STATE(this->distance_sensor_, NAN);
218 ESP_LOGV(TAG,
"Range error code %u", error);
219 PUBLISH_STATE(this->distance_sensor_, NAN);
221 PUBLISH_STATE(this->distance_sensor_,
static_cast<float>(
raw * this->
scaler_));
224 if (this->illuminance_sensor_ !=
nullptr) {
250 ESP_LOGV(TAG,
"ALS error code %u",
result[0] >> 4);
251 PUBLISH_STATE(this->illuminance_sensor_, NAN);
257void VL6180XComponent::dump_config() {
258 ESP_LOGCONFIG(TAG,
"VL6180X:");
259 LOG_I2C_DEVICE(
this);
261 LOG_UPDATE_INTERVAL(
this);
262 LOG_SENSOR(
" ",
"Distance", this->distance_sensor_);
263 LOG_SENSOR(
" ",
"Illuminance", this->illuminance_sensor_);
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
void status_set_warning()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
void status_clear_warning()
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
virtual void digital_write(bool value)=0
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
bool write_byte_reg16(uint16_t a_register, uint8_t data) const
Write one byte to a register with a 16-bit register address.
uint8_t address_
store the address of the device on the bus
bool read_byte_reg16(uint16_t a_register, uint8_t *data)
Read one byte from a register with a 16-bit register address.
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
uint16_t als_integration_time_ms_
static SlotList< GPIOPin *, VL6180X_ENABLE_PIN_COUNT > enable_pins
const uint8_t STORE_YAML_DATA[] PROGMEM
The zstd YAML envelope generated by store_yaml, STORE_YAML_DATA_SIZE bytes in flash.
@ ERROR_OK
No error found during execution of method.
ESPHOME_LOG_TAG(TAG, "vl6180x")
void progmem_memcpy(void *dst, const void *src, size_t len)
std::conditional_t< N==0, EmptyList< T >, StaticVector< T, N > > SlotList
StaticVector<T, N>, or EmptyList<T> when N is 0.
uint16_t progmem_read_uint16(const uint16_t *addr)
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)