ESPHome  2024.11.0
ultrasonic_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/gpio.h"
6 
7 #include <cinttypes>
8 
9 namespace esphome {
10 namespace ultrasonic {
11 
13  public:
14  void set_trigger_pin(GPIOPin *trigger_pin) { trigger_pin_ = trigger_pin; }
15  void set_echo_pin(InternalGPIOPin *echo_pin) { echo_pin_ = echo_pin; }
16 
18  void set_timeout_us(uint32_t timeout_us);
19 
20  // ========== INTERNAL METHODS ==========
21  // (In most use cases you won't need these)
23  void setup() override;
24  void dump_config() override;
25 
26  void update() override;
27 
28  float get_setup_priority() const override;
29 
31  void set_pulse_time_us(uint32_t pulse_time_us);
32 
33  protected:
35  static float us_to_m(uint32_t us);
37 
41  uint32_t timeout_us_{};
42  uint32_t pulse_time_us_{};
43 };
44 
45 } // namespace ultrasonic
46 } // namespace esphome
void set_pulse_time_us(uint32_t pulse_time_us)
Set the time in µs the trigger pin should be enabled for in µs, defaults to 10µs (for HC-SR04) ...
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:66
GPIOPin * trigger_pin_
Helper function to convert the specified distance in meters to the echo duration in µs...
void set_timeout_us(uint32_t timeout_us)
Set the timeout for waiting for the echo in µs.
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_echo_pin(InternalGPIOPin *echo_pin)
static float us_to_m(uint32_t us)
Helper function to convert the specified echo duration in µs to meters.
void setup() override
Set up pins and register interval.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57