LCOV - code coverage report
Current view: top level - src/AH/Hardware - Button.cpp (source / functions) Hit Total Coverage
Test: 90a1b9beff85a60dc6ebcea034a947a845e56960 Lines: 33 33 100.0 %
Date: 2019-11-30 15:53:32 Functions: 9 9 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          88 : Button::Button(pin_t pin) : pin(pin) {}
      10             : 
      11          45 : 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         228 : Button::State Button::update() {
      20             :     // read the button state and invert it if "invertState" is true
      21         228 :     bool input = ExtIO::digitalRead(pin) ^ invertState;
      22         228 :     bool prevState = debouncedState & 0b01;
      23         228 :     unsigned long now = millis();
      24         228 :     if (now - prevBounceTime > debounceTime) { // wait for state to stabilize
      25         207 :         debouncedState = static_cast<State>((prevState << 1) | input);
      26         207 :     } else {
      27          21 :         debouncedState = static_cast<State>((prevState << 1) | prevState);
      28             :     }
      29         228 :     if (input != prevInput) { // Button is pressed, released or bounces
      30          99 :         prevBounceTime = now;
      31          99 :         prevInput = input;
      32          99 :     }
      33         228 :     return debouncedState;
      34             : }
      35             : 
      36           8 : Button::State Button::getState() const { return debouncedState; }
      37             : 
      38           5 : const __FlashStringHelper *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           7 : unsigned long Button::previousBounceTime() const { return prevBounceTime; }
      49             : 
      50           7 : unsigned long Button::stableTime(unsigned long now) const {
      51           7 :     return now - previousBounceTime();
      52             : }
      53             : 
      54           5 : unsigned long Button::stableTime() const { return stableTime(millis()); }
      55             : 
      56             : END_AH_NAMESPACE
      57             : 
      58             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.14-5-g4ff2ed6