16 static const char *
const TAG =
"vl53l0x";
24 LOG_SENSOR(
"",
"VL53L0X",
this);
25 LOG_UPDATE_INTERVAL(
this);
34 ESP_LOGD(TAG,
"'%s' - setup BEGIN", this->
name_.
c_str());
38 if (vl53_sensor->enable_pin_ !=
nullptr) {
40 vl53_sensor->enable_pin_->setup();
41 vl53_sensor->enable_pin_->digital_write(
false);
90 while (
reg(0x83).
get() == 0x00) {
92 ESP_LOGE(TAG,
"'%s' - setup timeout", this->
name_.
c_str());
100 uint8_t tmp =
reg(0x92).
get();
101 uint8_t spad_count = tmp & 0x7F;
102 bool spad_type_is_aperture = tmp & 0x80;
112 uint8_t ref_spad_map[6] = {};
121 uint8_t first_spad_to_enable = spad_type_is_aperture ? 12 : 0;
122 uint8_t spads_enabled = 0;
123 for (
int i = 0; i < 48; i++) {
124 uint8_t &
val = ref_spad_map[i / 8];
125 uint8_t mask = 1 << (i % 8);
127 if (i < first_spad_to_enable || spads_enabled == spad_count) {
129 }
else if (val & mask) {
239 ESP_LOGW(TAG,
"1st reference calibration failed!");
245 ESP_LOGW(TAG,
"2nd reference calibration failed!");
255 reg(0x8A) = final_address & 0x7F;
258 ESP_LOGD(TAG,
"'%s' - setup END", this->
name_.
c_str());
265 ESP_LOGW(TAG,
"%s - update called before prior reading complete - initiated:%d waiting_for_interrupt:%d",
287 if (
reg(0x00).
get() & 0x01) {
297 if (
reg(0x13).
get() & 0x07) {
298 uint16_t range_mm = 0;
303 if (range_mm >= 8190) {
304 ESP_LOGD(TAG,
"'%s' - Distance is out of range, please move the target closer", this->
name_.
c_str());
309 float range_m = range_mm / 1e3f;
310 ESP_LOGD(TAG,
"'%s' - Got distance %.3f m", this->
name_.
c_str(), range_m);
320 uint16_t start_overhead = 1910;
321 uint16_t end_overhead = 960;
322 uint16_t msrc_overhead = 660;
323 uint16_t tcc_overhead = 590;
324 uint16_t dss_overhead = 690;
325 uint16_t pre_range_overhead = 660;
326 uint16_t final_range_overhead = 550;
329 uint32_t budget_us = start_overhead + end_overhead;
335 budget_us += (timeouts.msrc_dss_tcc_us + tcc_overhead);
338 budget_us += 2 * (timeouts.msrc_dss_tcc_us + dss_overhead);
339 }
else if (enables.msrc) {
340 budget_us += (timeouts.msrc_dss_tcc_us + msrc_overhead);
343 if (enables.pre_range)
344 budget_us += (timeouts.pre_range_us + pre_range_overhead);
346 if (enables.final_range)
347 budget_us += (timeouts.final_range_us + final_range_overhead);
357 uint16_t start_overhead = 1320;
358 uint16_t end_overhead = 960;
359 uint16_t msrc_overhead = 660;
360 uint16_t tcc_overhead = 590;
361 uint16_t dss_overhead = 690;
362 uint16_t pre_range_overhead = 660;
363 uint16_t final_range_overhead = 550;
365 uint32_t min_timing_budget = 20000;
367 if (budget_us < min_timing_budget) {
371 uint32_t used_budget_us = start_overhead + end_overhead;
377 used_budget_us += (timeouts.msrc_dss_tcc_us + tcc_overhead);
381 used_budget_us += 2 * (timeouts.msrc_dss_tcc_us + dss_overhead);
382 }
else if (enables.msrc) {
383 used_budget_us += (timeouts.msrc_dss_tcc_us + msrc_overhead);
386 if (enables.pre_range) {
387 used_budget_us += (timeouts.pre_range_us + pre_range_overhead);
390 if (enables.final_range) {
391 used_budget_us += final_range_overhead;
399 if (used_budget_us > budget_us) {
404 uint32_t final_range_timeout_us = budget_us - used_budget_us;
414 uint16_t final_range_timeout_mclks =
417 if (enables.pre_range) {
418 final_range_timeout_mclks += timeouts.pre_range_mclks;
431 uint8_t sequence_config =
reg(0x01).
get();
432 enables->
tcc = (sequence_config >> 4) & 0x1;
433 enables->
dss = (sequence_config >> 3) & 0x1;
434 enables->
msrc = (sequence_config >> 2) & 0x1;
435 enables->
pre_range = (sequence_config >> 6) & 0x1;
436 enables->
final_range = (sequence_config >> 7) & 0x1;
475 return (vcsel + 1) << 1;
479 return ((2304UL * vcsel_period_pclks * 1655UL) + 500UL) / 1000UL;
484 return ((timeout_period_mclks * macro_period_ns) + (macro_period_ns / 2)) / 1000;
489 return (((timeout_period_us * 1000) + (macro_period_ns / 2)) / macro_period_ns);
494 uint8_t msb = (reg_val >> 8) & 0xFF;
495 uint8_t lsb = (reg_val >> 0) & 0xFF;
496 return (uint16_t(lsb) << msb) + 1;
501 uint32_t ls_byte = 0;
502 uint16_t ms_byte = 0;
504 if (timeout_mclks <= 0)
507 ls_byte = timeout_mclks - 1;
509 while ((ls_byte & 0xFFFFFF00) > 0) {
514 return (ms_byte << 8) | (ls_byte & 0xFF);
518 reg(0x00) = 0x01 | vhv_init_byte;
520 uint32_t start =
millis();
521 while ((
reg(0x13).
get() & 0x07) == 0) {
522 if (
millis() - start > 1000)
virtual void digital_write(bool value)=0
bool read_byte_16(uint8_t a_register, uint16_t *data)
uint32_t get_macro_period_(uint8_t vcsel_period_pclks)
uint16_t pre_range_vcsel_period_pclks
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
uint8_t get() const
returns the register value
uint32_t timeout_mclks_to_microseconds_(uint16_t timeout_period_mclks, uint8_t vcsel_period_pclks)
bool waiting_for_interrupt_
void status_momentary_warning(const std::string &name, uint32_t length=5000)
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
bool set_measurement_timing_budget_(uint32_t budget_us)
uint16_t decode_timeout_(uint16_t reg_val)
static bool enable_pin_setup_complete
void dump_config() override
uint32_t IRAM_ATTR HOT micros()
uint32_t IRAM_ATTR HOT millis()
uint32_t measurement_timing_budget_us_
uint16_t msrc_dss_tcc_mclks
void publish_state(float state)
Publish a new state to the front-end.
uint16_t timeout_start_us_
uint32_t timeout_microseconds_to_mclks_(uint32_t timeout_period_us, uint8_t vcsel_period_pclks)
void get_sequence_step_enables_(SequenceStepEnables *enables)
static std::list< VL53L0XSensor * > vl53_sensors
bool perform_single_ref_calibration_(uint8_t vhv_init_byte)
constexpr const char * c_str() const
uint8_t address_
store the address of the device on the bus
uint16_t final_range_vcsel_period_pclks
void IRAM_ATTR HOT yield()
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint8_t get_vcsel_pulse_period_(VcselPeriodType type)
uint32_t get_measurement_timing_budget_()
bool write_byte_16(uint8_t a_register, uint16_t data)
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
uint16_t encode_timeout_(uint16_t timeout_mclks)
uint16_t final_range_mclks
void get_sequence_step_timeouts_(SequenceStepEnables const *enables, SequenceStepTimeouts *timeouts)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)