7static const char *
const TAG =
"ultrasonic.sensor";
9static constexpr uint32_t START_TIMEOUT_US = 40000;
58 if (elapsed >= START_TIMEOUT_US) {
59 ESP_LOGW(TAG,
"'%s' - Measurement start timed out", this->
name_.
c_str());
72 ESP_LOGD(TAG,
"'%s' - Measurement pulse timed out after %" PRIu32
"us", this->
name_.
c_str(), elapsed);
83 ESP_LOGV(TAG,
"pulse start took %" PRIu32
"us, echo took %" PRIu32
"us",
86 ESP_LOGD(TAG,
"'%s' - Got distance: %.3f m", this->
name_.
c_str(), result);
88 ESP_LOGW(TAG,
"'%s' - pulse end before pulse start, does the echo pin need to be inverted?", this->
name_.
c_str());
98 LOG_SENSOR(
"",
"Ultrasonic Sensor",
this);
102 " Pulse time: %" PRIu32
" µs\n"
103 " Timeout: %" PRIu32
" µs",
105 LOG_UPDATE_INTERVAL(
this);
109 const float speed_sound_m_per_s = 343.0f;
110 const float time_s = us / 1e6f;
111 const float total_dist = time_s * speed_sound_m_per_s;
112 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
ISRInternalGPIOPin echo_pin_isr
volatile uint32_t echo_start_us
static void gpio_intr(UltrasonicSensorStore *arg)