LCOV - code coverage report
Current view: top level - src/AH/Hardware - Button.cpp (source / functions) Hit Total Coverage
Test: e224b347cd670555e44f06608ac41bd1ace9d9d8 Lines: 38 38 100.0 %
Date: 2020-09-08 17:44:46 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             : using namespace ExtIO;
       8             : 
       9          87 : Button::Button(pin_t pin) : pin(pin) {}
      10             : 
      11          44 : void Button::begin() { ExtIO::pinMode(pin, INPUT_PULLUP); }
      12             : 
      13           1 : void Button::invert() { invertState = true; }
      14             : 
      15             : #ifndef AH_INDIVIDUAL_BUTTON_INVERT
      16             : bool Button::invertState = false;
      17             : #endif
      18             : 
      19         225 : Button::State Button::update() {
      20             :     // read the button state and invert it if "invertState" is true
      21         225 :     bool input = ExtIO::digitalRead(pin) ^ invertState;
      22         225 :     bool prevState = debouncedState & 0b01;
      23         225 :     unsigned long now = millis();
      24         225 :     if (now - prevBounceTime > debounceTime) { // wait for state to stabilize
      25         204 :         debouncedState = static_cast<State>((prevState << 1) | input);
      26         204 :     } else {
      27          21 :         debouncedState = static_cast<State>((prevState << 1) | prevState);
      28             :     }
      29         225 :     if (input != prevInput) { // Button is pressed, released or bounces
      30          96 :         prevBounceTime = now;
      31          96 :         prevInput = input;
      32          96 :     }
      33         450 :     return debouncedState;
      34         225 : }
      35             : 
      36           8 : Button::State Button::getState() const { return debouncedState; }
      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           5 : }
      47             : 
      48           8 : unsigned long Button::previousBounceTime() const { return prevBounceTime; }
      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
      65             : 
      66             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.14-6-g40580cd