LCOV - code coverage report
Current view: top level - src/AH/Hardware - Button.cpp (source / functions) Coverage Total Hit
Test: 73449d9b107c772cf65493691543348214e5d5eb Lines: 100.0 % 34 34
Test Date: 2026-06-06 17:44:35 Functions: 100.0 % 11 11
Legend: Lines:     hit not hit

            Line data    Source code
       1              : #include "Button.hpp"
       2              : 
       3              : BEGIN_AH_NAMESPACE
       4              : 
       5           99 : Button::Button(pin_t pin) : pin(pin) {}
       6              : 
       7           48 : void Button::begin() { ExtIO::pinMode(pin, INPUT_PULLUP); }
       8              : 
       9            1 : void Button::invert() { state.invert = true; }
      10              : 
      11          229 : Button::State Button::update() {
      12              :     // Read pin state and current time
      13          229 :     bool input = ExtIO::digitalRead(pin) ^ state.invert;
      14          229 :     unsigned long now = millis();
      15              :     // Check if enough time has elapsed after last bounce
      16          229 :     if (state.bouncing)
      17          127 :         state.bouncing = now - state.prevBounceTime <= debounceTime;
      18              :     // Shift the debounced state one bit to the left, either appending the
      19              :     // new input state if not bouncing, or repeat the old state if bouncing
      20          229 :     bool prevState = state.debounced & 0b01;
      21          229 :     bool newState = state.bouncing ? prevState : input;
      22          229 :     state.debounced = (prevState << 1) | newState;
      23              :     // Check if the input changed state (button pressed, released or bouncing)
      24          229 :     if (input != state.prevInput) {
      25           97 :         state.bouncing = true;
      26           97 :         state.prevInput = input;
      27           97 :         state.prevBounceTime = now;
      28              :     }
      29          229 :     return getState();
      30              : }
      31              : 
      32          238 : Button::State Button::getState() const {
      33          238 :     return static_cast<State>(state.debounced);
      34              : }
      35              : 
      36            5 : FlashString_t Button::getName(Button::State state) {
      37            5 :     switch (state) {
      38            1 :         case Button::Pressed: return F("Pressed");
      39            1 :         case Button::Released: return F("Released");
      40            1 :         case Button::Falling: return F("Falling");
      41            1 :         case Button::Rising: return F("Rising");
      42            1 :         default: return F("<invalid>"); // Keeps the compiler happy
      43              :     }
      44              : }
      45              : 
      46            8 : unsigned long Button::previousBounceTime() const {
      47            8 :     return state.prevBounceTime;
      48              : }
      49              : 
      50            8 : unsigned long Button::stableTime(unsigned long now) const {
      51            8 :     return now - previousBounceTime();
      52              : }
      53              : 
      54            5 : unsigned long Button::stableTime() const { return stableTime(millis()); }
      55              : 
      56            1 : void Button::setDebounceTime(unsigned long debounceTime) {
      57            1 :     Button::debounceTime = debounceTime;
      58            1 : }
      59              : 
      60            1 : unsigned long Button::getDebounceTime() { return Button::debounceTime; }
      61              : 
      62              : unsigned long Button::debounceTime = BUTTON_DEBOUNCE_TIME;
      63              : 
      64              : END_AH_NAMESPACE
        

Generated by: LCOV version 2.4-beta