Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : #include <memory>
4 : :
5 : : #include "i_us_driver.hpp"
6 : : #include "i_us_processor.hpp"
7 : : #include "i_us_sensor.hpp"
8 : : #include "us_types.hpp"
9 : :
10 : : /**
11 : : * @brief Concrete implementation of IUsSensor.
12 : : *
13 : : * This class orchestrates multiple pings via IUsDriver and applies statistical
14 : : * processing via IUsProcessor to produce a final Reading.
15 : : *
16 : : * It manages the high-level measurement cycle, handling hardware failures
17 : : * immediately and logical failures (like timeouts or out-of-range pings) by
18 : : * collecting them for statistical analysis.
19 : : */
20 : : class UsSensor : public IUsSensor
21 : : {
22 : : public:
23 : : /**
24 : : * @brief Construct a new UsSensor object.
25 : : *
26 : : * This constructor is intended for production use and automatically creates
27 : : * the necessary internal components (driver, processor, HALs).
28 : : *
29 : : * @param trig_pin GPIO number for the trigger pin.
30 : : * @param echo_pin GPIO number for the echo pin.
31 : : * @param cfg Configuration structure for the sensor.
32 : : */
33 : : UsSensor(gpio_num_t trig_pin, gpio_num_t echo_pin, const UsConfig &cfg);
34 : :
35 : : /**
36 : : * @brief Construct a new UsSensor object with dependency injection.
37 : : *
38 : : * This constructor is primarily intended for unit testing or advanced
39 : : * customization.
40 : : *
41 : : * @param cfg Configuration structure for the sensor.
42 : : * @param driver Shared pointer to a driver implementation.
43 : : * @param processor Shared pointer to a processor implementation.
44 : : */
45 : : UsSensor(const UsConfig &cfg, std::shared_ptr<IUsDriver> driver, std::shared_ptr<IUsProcessor> processor);
46 : :
47 [ + - + - ]: 36 : ~UsSensor() override = default;
48 : :
49 : : /** @copydoc IUsSensor::init() */
50 : : esp_err_t init() override;
51 : :
52 : : /** @copydoc IUsSensor::deinit() */
53 : : esp_err_t deinit() override;
54 : :
55 : : /** @copydoc IUsSensor::read_distance() */
56 : : Reading read_distance(uint8_t ping_count) override;
57 : :
58 : : private:
59 : : /** @internal */
60 : : UsConfig cfg_;
61 : : /** @internal */
62 : : std::shared_ptr<IUsDriver> driver_;
63 : : /** @internal */
64 : : std::shared_ptr<IUsProcessor> processor_;
65 : :
66 : : /** @internal */
67 : : static constexpr uint8_t MAX_PINGS = 15;
68 : : };
|