Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : #include <memory>
4 : :
5 : : #include "esp_err.h"
6 : : #include "i_us_driver.hpp"
7 : : #include "i_us_gpio_hal.hpp"
8 : : #include "i_us_timer_hal.hpp"
9 : :
10 : : /**
11 : : * @brief Concrete implementation of IUsDriver for HC-SR04-compatible sensors.
12 : : * @internal
13 : : *
14 : : * Handles the low-level GPIO protocol: trigger pulse, echo detection, and
15 : : * pulse duration measurement. Maps hardware errors to UsResult.
16 : : */
17 : : class UsDriver : public IUsDriver
18 : : {
19 : : public:
20 : : /** @internal */
21 : : static constexpr float SOUND_SPEED_CM_PER_US = 0.0343f;
22 : :
23 : : /** @internal */
24 : : UsDriver(
25 : : std::shared_ptr<IGpioHAL> gpio_hal,
26 : : std::shared_ptr<ITimerHAL> timer_hal,
27 : : gpio_num_t trig_pin,
28 : : gpio_num_t echo_pin);
29 : :
30 [ + - + - ]: 56 : ~UsDriver() override = default;
31 : :
32 : : /** @copydoc IUsDriver::init() */
33 : : esp_err_t init(uint16_t warmup_time_ms = 0) override;
34 : :
35 : : /** @copydoc IUsDriver::deinit() */
36 : : esp_err_t deinit() override;
37 : :
38 : : /** @copydoc IUsDriver::ping_once() */
39 : : Reading ping_once(const UsConfig &cfg) override;
40 : :
41 : : private:
42 : : /** @internal */
43 : : bool is_echo_stuck();
44 : :
45 : : /** @internal */
46 : : esp_err_t trigger(uint16_t pulse_duration_us);
47 : :
48 : : /** @internal */
49 : : esp_err_t wait_rising_edge(uint32_t timeout_us);
50 : :
51 : : /** @internal */
52 : : esp_err_t measure_pulse(uint32_t timeout_us, uint32_t &duration_us);
53 : :
54 : : /** @internal */
55 : : std::shared_ptr<IGpioHAL> gpio_hal_;
56 : : /** @internal */
57 : : std::shared_ptr<ITimerHAL> timer_hal_;
58 : :
59 : : /** @internal */
60 : : gpio_num_t trig_pin_;
61 : : /** @internal */
62 : : gpio_num_t echo_pin_;
63 : : };
|