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