LCOV - code coverage report
Current view: top level - src/AH/Hardware - Button.cpp (source / functions) Hit Total Coverage
Test: ffed98f648fe78e7aa7bdd228474317d40dadbec Lines: 34 34 100.0 %
Date: 2022-05-28 15:22:59 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.15