7static const char *
const TAG =
"ultrasonic.sensor";
9static constexpr uint32_t DEBOUNCE_US = 50;
10static constexpr uint32_t START_DELAY_US = 100;
11static constexpr uint32_t START_TIMEOUT_US = 40000;
62 if (elapsed >= START_TIMEOUT_US) {
63 ESP_LOGW(TAG,
"'%s' - Measurement start timed out", this->
name_.
c_str());
76 ESP_LOGD(TAG,
"'%s' - Measurement pulse timed out after %" PRIu32
"us", this->
name_.
c_str(), elapsed);
85 ESP_LOGV(TAG,
"Echo took %" PRIu32
"us", pulse_duration);
87 ESP_LOGD(TAG,
"'%s' - Got distance: %.3f m", this->
name_.
c_str(), result);
95 LOG_SENSOR(
"",
"Ultrasonic Sensor",
this);
99 " Pulse time: %" PRIu32
" µs\n"
100 " Timeout: %" PRIu32
" µs",
102 LOG_UPDATE_INTERVAL(
this);
106 const float speed_sound_m_per_s = 343.0f;
107 const float time_s = us / 1e6f;
108 const float total_dist = time_s * speed_sound_m_per_s;
109 return total_dist / 2.0f;
virtual void digital_write(bool value)=0
void digital_write(bool value)
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
virtual ISRInternalGPIOPin to_isr() const =0
Helper class to disable interrupts.
constexpr const char * c_str() const
void publish_state(float state)
Publish a new state to the front-end.
InternalGPIOPin * trigger_pin_
bool measurement_pending_
void send_trigger_pulse_()
void dump_config() override
InternalGPIOPin * echo_pin_
ISRInternalGPIOPin trigger_pin_isr_
static float us_to_m(uint32_t us)
Helper function to convert the specified echo duration in µs to meters.
UltrasonicSensorStore store_
uint32_t measurement_start_us_
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint32_t IRAM_ATTR HOT micros()
volatile uint32_t echo_end_us
volatile uint32_t echo_start_us
static void gpio_intr(UltrasonicSensorStore *arg)
volatile uint32_t measurement_start_us