LCOV - code coverage report
Current view: top level - src/AH/Hardware - Button.cpp (source / functions) Hit Total Coverage
Test: 00f463b534fbea22f0b596e091a60715679e3064 Lines: 34 34 100.0 %
Date: 2024-11-03 16:33:35 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "Button.hpp"
       2             : 
       3             : BEGIN_AH_NAMESPACE
       4             : 
       5          15 : Button::Button(pin_t pin) : pin(pin) {}
       6             : 
       7          15 : void Button::begin() { ExtIO::pinMode(pin, INPUT_PULLUP); }
       8             : 
       9           1 : void Button::invert() { state.invert = true; }
      10             : 
      11          85 : Button::State Button::update() {
      12             :     // Read pin state and current time
      13          85 :     bool input = ExtIO::digitalRead(pin) ^ state.invert;
      14          85 :     unsigned long now = millis();
      15             :     // Check if enough time has elapsed after last bounce
      16          85 :     if (state.bouncing)
      17          51 :         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          85 :     bool prevState = state.debounced & 0b01;
      21          85 :     bool newState = state.bouncing ? prevState : input;
      22          85 :     state.debounced = (prevState << 1) | newState;
      23             :     // Check if the input changed state (button pressed, released or bouncing)
      24          85 :     if (input != state.prevInput) {
      25          33 :         state.bouncing = true;
      26          33 :         state.prevInput = input;
      27          33 :         state.prevBounceTime = now;
      28             :     }
      29          85 :     return getState();
      30             : }
      31             : 
      32          93 : Button::State Button::getState() const {
      33          93 :     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 1.15