2020-09-02 12:39:50 -07:00
|
|
|
/**
|
|
|
|
* @file debounce.h
|
|
|
|
* @brief Generic button debounce class
|
2020-09-20 11:01:00 -07:00
|
|
|
* https://en.wikipedia.org/wiki/Switch#Contact_bounce
|
|
|
|
* If we don't 'debounce' our button inputs, we may mistakenly
|
|
|
|
* read a single button press as multiple events.
|
2020-09-02 12:39:50 -07:00
|
|
|
*
|
|
|
|
* @date Aug 31, 2020
|
|
|
|
* @author David Holdeman, (c) 2020
|
|
|
|
*/
|
2020-09-20 11:01:00 -07:00
|
|
|
#pragma once
|
2020-09-08 08:29:38 -07:00
|
|
|
|
2020-09-02 12:39:50 -07:00
|
|
|
#include "io_pins.h"
|
|
|
|
|
|
|
|
class ButtonDebounce {
|
|
|
|
public:
|
2020-12-24 04:30:56 -08:00
|
|
|
explicit ButtonDebounce(const char* name);
|
2024-04-11 21:28:13 -07:00
|
|
|
void init(efitimems_t threshold, brain_pin_e &pin, pin_input_mode_e &mode, bool inverted = false);
|
2020-09-20 11:01:00 -07:00
|
|
|
void stopConfiguration();
|
|
|
|
void startConfiguration();
|
2020-09-02 12:39:50 -07:00
|
|
|
bool readPinEvent();
|
2020-09-09 14:18:54 -07:00
|
|
|
bool readPinState();
|
2023-11-08 21:33:16 -08:00
|
|
|
bool readPinState2(bool valueWithinThreshold);
|
2020-09-20 11:01:00 -07:00
|
|
|
static void stopConfigurationList();
|
|
|
|
static void startConfigurationList();
|
2020-11-06 20:48:35 -08:00
|
|
|
static void debug();
|
2023-09-25 10:10:54 -07:00
|
|
|
bool getPhysicalState();
|
2024-05-08 19:49:49 -07:00
|
|
|
#if EFI_UNIT_TEST
|
|
|
|
static void resetForUnitTests() {
|
|
|
|
s_firstDebounce = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2020-09-02 12:39:50 -07:00
|
|
|
private:
|
2020-12-24 04:30:56 -08:00
|
|
|
const char* const m_name;
|
2024-04-25 12:57:10 -07:00
|
|
|
efidur_t m_threshold;
|
2023-06-15 14:36:07 -07:00
|
|
|
Timer timeLast;
|
2020-09-20 11:01:00 -07:00
|
|
|
brain_pin_e *m_pin;
|
2022-04-28 14:32:39 -07:00
|
|
|
brain_pin_e active_pin = Gpio::Unassigned;
|
2020-09-20 11:01:00 -07:00
|
|
|
pin_input_mode_e *m_mode;
|
|
|
|
pin_input_mode_e active_mode = PI_DEFAULT;
|
|
|
|
bool storedValue = false;
|
2024-04-11 21:28:13 -07:00
|
|
|
bool m_inverted = false;
|
2020-12-16 19:24:19 -08:00
|
|
|
bool isInstanceRegisteredInGlobalList = false;
|
|
|
|
bool needsPinInitialization = true;
|
2020-09-08 08:29:38 -07:00
|
|
|
ButtonDebounce *nextDebounce = nullptr;
|
|
|
|
static ButtonDebounce* s_firstDebounce;
|
2020-09-02 12:39:50 -07:00
|
|
|
};
|
2020-11-06 20:48:35 -08:00
|
|
|
|
2021-04-21 09:53:13 -07:00
|
|
|
void initButtonDebounce();
|