Line data Source code
1 : /** 2 : * @file debounce.h 3 : * @brief Generic button debounce class 4 : * https://en.wikipedia.org/wiki/Switch#Contact_bounce 5 : * If we don't 'debounce' our button inputs, we may mistakenly 6 : * read a single button press as multiple events. 7 : * 8 : * @date Aug 31, 2020 9 : * @author David Holdeman, (c) 2020 10 : */ 11 : #pragma once 12 : 13 : #include "io_pins.h" 14 : 15 : class ButtonDebounce { 16 : public: 17 : explicit ButtonDebounce(const char* name); 18 : void init(efitimems_t threshold, brain_pin_e &pin, pin_input_mode_e &mode, bool inverted = false); 19 : void stopConfiguration(); 20 : void startConfiguration(); 21 : bool readPinEvent(); 22 : bool readPinState(); 23 : bool readPinState2(bool valueWithinThreshold); 24 : static void stopConfigurationList(); 25 : static void startConfigurationList(); 26 : static void debug(); 27 : bool getPhysicalState(); 28 : #if EFI_UNIT_TEST 29 583 : static void resetForUnitTests() { 30 583 : s_firstDebounce = nullptr; 31 583 : } 32 : #endif 33 : private: 34 : const char* const m_name; 35 : efidur_t m_threshold; 36 : Timer timeLast; 37 : brain_pin_e *m_pin; 38 : brain_pin_e active_pin = Gpio::Unassigned; 39 : pin_input_mode_e *m_mode; 40 : pin_input_mode_e active_mode = PI_DEFAULT; 41 : bool storedValue = false; 42 : bool m_inverted = false; 43 : bool isInstanceRegisteredInGlobalList = false; 44 : bool needsPinInitialization = true; 45 : ButtonDebounce *nextDebounce = nullptr; 46 : static ButtonDebounce* s_firstDebounce; 47 : }; 48 : 49 : void initButtonDebounce();