LCOV - code coverage report
Current view: top level - src - us_sensor.cpp (source / functions) Coverage Total Hit
Test: Ultrasonic Sensor Unified Coverage Lines: 100.0 % 27 27
Test Date: 2026-04-24 03:29:20 Functions: 100.0 % 5 5
Branches: 75.0 % 20 15

             Branch data     Line data    Source code
       1                 :             : #include "us_sensor.hpp"
       2                 :             : #include "us_driver.hpp"
       3                 :             : #include "us_gpio_hal.hpp"
       4                 :             : #include "us_processor.hpp"
       5                 :             : #include "us_timer_hal.hpp"
       6                 :             : #include <memory>
       7                 :             : 
       8                 :             : #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
       9                 :             : #include "esp_log.h"
      10                 :             : 
      11                 :             : namespace ultrasonic {
      12                 :             : 
      13                 :             : static const char *TAG = "UsSensor";
      14                 :             : 
      15                 :             : // Production constructor: creates concrete HALs and driver internally
      16                 :           1 : UsSensor::UsSensor(gpio_num_t trig_pin, gpio_num_t echo_pin, const UsConfig &cfg)
      17                 :           1 :     : cfg_(cfg)
      18   [ -  +  -  + ]:           2 :     , driver_(std::make_shared<UsDriver>(std::make_shared<GpioHAL>(), std::make_shared<TimerHAL>(), trig_pin, echo_pin))
      19                 :           2 :     , processor_(std::make_shared<UsProcessor>())
      20                 :             : {
      21                 :           1 : }
      22                 :             : 
      23                 :             : // Test constructor: receives dependencies via injection
      24                 :          12 : UsSensor::UsSensor(const UsConfig &cfg, std::shared_ptr<IUsDriver> driver, std::shared_ptr<IUsProcessor> processor)
      25                 :          12 :     : cfg_(cfg)
      26         [ +  - ]:          12 :     , driver_(driver)
      27   [ +  -  +  - ]:          24 :     , processor_(processor)
      28                 :             : {
      29                 :          12 : }
      30                 :             : 
      31                 :           1 : esp_err_t UsSensor::init()
      32                 :             : {
      33                 :             :     // Pass warmup_time_ms so the driver waits for sensor stabilization after GPIO setup
      34                 :           1 :     return driver_->init(cfg_.warmup_time_ms);
      35                 :             : }
      36                 :             : 
      37                 :           1 : esp_err_t UsSensor::deinit()
      38                 :             : {
      39                 :           1 :     return driver_->deinit();
      40                 :             : }
      41                 :             : 
      42                 :          11 : Reading UsSensor::read_distance(uint8_t ping_count)
      43                 :             : {
      44                 :             :     // Clamp ping_count to valid range
      45         [ +  + ]:          11 :     if (ping_count == 0 || ping_count > MAX_PINGS) {
      46                 :           2 :         ESP_LOGW(TAG, "ping_count %d out of range [1, %d], clamping", ping_count, MAX_PINGS);
      47         [ +  + ]:           2 :         ping_count = (ping_count == 0) ? 1 : MAX_PINGS;
      48                 :             :     }
      49                 :             : 
      50                 :          11 :     Reading pings[MAX_PINGS];
      51                 :             : 
      52         [ +  + ]:          51 :     for (uint8_t i = 0; i < ping_count; i++) {
      53                 :          42 :         pings[i] = driver_->ping_once(cfg_);
      54                 :             : 
      55                 :             :         // Hardware failures abort the loop immediately — application must act
      56         [ +  + ]:          42 :         if (pings[i].result == UsResult::ECHO_STUCK || pings[i].result == UsResult::HW_FAULT) {
      57                 :           2 :             ESP_LOGE(TAG, "Hardware failure on ping %d: %d — aborting", i, static_cast<int>(pings[i].result));
      58                 :           2 :             return pings[i];
      59                 :             :         }
      60                 :             : 
      61                 :             :         // Logical failures (TIMEOUT, OUT_OF_RANGE) are collected and passed to processor
      62         [ +  + ]:          40 :         if (!is_success(pings[i].result)) {
      63                 :          12 :             ESP_LOGD(TAG, "Ping %d failed: result=%d", i, static_cast<int>(pings[i].result));
      64                 :             :         }
      65                 :             : 
      66                 :             :         // Note: inter-ping delay (cfg_.ping_interval_ms) is applied inside UsDriver::ping_once()
      67                 :             :     }
      68                 :             : 
      69                 :             :     // Delegate processing (including logical error refinement) to the processor
      70                 :           9 :     return processor_->process(pings, ping_count, cfg_);
      71                 :             : }
      72                 :             : 
      73                 :             : } // namespace ultrasonic
        

Generated by: LCOV version 2.0-1