Debounce pin management (#1760)

* add linked list

* macros, and use in buttonshift

* unit_tests macros

* add extern

* add parens

* move extern

* move extern

* move buttonDebounceListHead

* move buttonDebouncePointerHead

* merge gore

* undo

* reduce unused size

* don't store pointer if already initialized

* few changes

* remove oldPin

* fix merge conflict

* merge in changes commited to wrong branch

* fix definition

* out of class?

* brute force programming

* fix few problemos

* am confuse

* am confuse

* am confuse

* am confuse

* oldPin snuck in

* move to public?

* define again

* try constexpr

* def in cpp

* remove constexpr

* fix def

* fix?

* update active

* fix a few things
This commit is contained in:
David Holdeman 2020-09-08 10:29:38 -05:00 committed by GitHub
parent 8f8d4eccaf
commit cab4cd0df2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 13 deletions

View File

@ -14,8 +14,8 @@ ButtonShiftController buttonShiftController;
void ButtonShiftController::init (DECLARE_ENGINE_PARAMETER_SIGNATURE) {
// 500 millisecond is maybe a little long?
debounceUp.init(500, CONFIG(tcuUpshiftButtonPin), CONFIG(tcuUpshiftButtonPinMode));
debounceDown.init(500, CONFIG(tcuDownshiftButtonPin), CONFIG(tcuDownshiftButtonPinMode));
debounceUp.init(500, &CONFIG(tcuUpshiftButtonPin), &CONFIG(tcuUpshiftButtonPinMode));
debounceDown.init(500, &CONFIG(tcuDownshiftButtonPin), &CONFIG(tcuDownshiftButtonPinMode));
}
void ButtonShiftController::update() {

View File

@ -6,16 +6,60 @@
* @author David Holdeman, (c) 2020
*/
#include "debounce.h"
#include "pin_repository.h"
#include "engine_configuration.h"
#include "hardware.h"
void ButtonDebounce::init (int t, brain_pin_e p, pin_input_mode_e m) {
ButtonDebounce* ButtonDebounce::s_firstDebounce = nullptr;
void ButtonDebounce::init (int t, brain_pin_e *pin, pin_input_mode_e *mode) {
if (!initialized) {
ButtonDebounce *listItem = s_firstDebounce;
if (listItem == nullptr) {
s_firstDebounce = this;
} else {
while (listItem->nextDebounce != nullptr) {
listItem = listItem->nextDebounce;
}
listItem->nextDebounce = this;
}
}
threshold = MS2NT(t);
timeLast = 0;
pin = p;
this->pin = pin;
active_pin = *pin;
this->mode = mode;
active_mode = *mode;
#ifdef PAL_MODE_INPUT_PULLDOWN
// getInputMode converts from pin_input_mode_e to iomode_t
mode = getInputMode(m);
efiSetPadMode("Button", p, mode);
efiSetPadMode("Button", active_pin, getInputMode(active_mode));
#endif
initialized = true;
}
void ButtonDebounce::updateConfigurationList () {
ButtonDebounce *listItem = s_firstDebounce;
while (listItem != nullptr) {
listItem->updateConfiguration();
if (listItem->nextDebounce != nullptr) {
listItem = listItem->nextDebounce;
} else {
break;
}
}
}
void ButtonDebounce::updateConfiguration () {
#ifndef EFI_ACTIVE_CONFIGURATION_IN_FLASH
if (*pin != active_pin || *mode != active_mode) {
#else
if (*pin != active_pin || *mode != active_mode || (isActiveConfigurationVoid && (*pin != 0 || *mode != 0))) {
#endif /* EFI_ACTIVE_CONFIGURATION_IN_FLASH */
brain_pin_markUnused(active_pin);
efiSetPadMode("Button", *pin, getInputMode(*mode));
}
active_pin = *pin;
active_mode = *mode;
}
/**
@ -36,9 +80,9 @@ bool ButtonDebounce::readPinEvent() {
// for example to implement long button presses, it will be needed.
readValue = false;
#ifdef PAL_MODE_INPUT_PULLDOWN
readValue = efiReadPin(pin);
readValue = efiReadPin(active_pin);
// Invert
if (mode != PAL_MODE_INPUT_PULLDOWN) {
if (getInputMode(active_mode) == PAL_MODE_INPUT_PULLUP) {
readValue = !readValue;
}
#endif

View File

@ -5,17 +5,29 @@
* @date Aug 31, 2020
* @author David Holdeman, (c) 2020
*/
#ifndef DEBOUNCE_INC
#define DEBOUNCE_INC
#include "globalaccess.h"
#include "io_pins.h"
class ButtonDebounce {
public:
void init(int t, brain_pin_e p, pin_input_mode_e m);
void init(int t, brain_pin_e *p, pin_input_mode_e *m);
void updateConfiguration();
bool readPinEvent();
static void updateConfigurationList();
private:
int threshold;
efitick_t timeLast;
brain_pin_e pin;
iomode_t mode;
brain_pin_e *pin;
brain_pin_e active_pin;
pin_input_mode_e *mode;
pin_input_mode_e active_mode;
bool readValue;
bool initialized = false;
ButtonDebounce *nextDebounce = nullptr;
static ButtonDebounce* s_firstDebounce;
};
#endif /* DEBOUNCE_INC */

View File

@ -264,9 +264,12 @@ void stopSpi(spi_device_e device) {
* this method is NOT currently invoked on ECU start
* todo: maybe start invoking this method on ECU start so that peripheral start-up initialization and restart are unified?
*/
void applyNewHardwareSettings(void) {
// all 'stop' methods need to go before we begin starting pins
ButtonDebounce::updateConfigurationList();
#if EFI_SHAFT_POSITION_INPUT
stopTriggerInputPins();
#endif /* EFI_SHAFT_POSITION_INPUT */
@ -275,7 +278,7 @@ void applyNewHardwareSettings(void) {
#if (HAL_USE_PAL && EFI_JOYSTICK)
stopJoystickPins();
#endif /* HAL_USE_PAL && EFI_JOYSTICK */
enginePins.stopInjectionPins();
enginePins.stopIgnitionPins();
#if EFI_CAN_SUPPORT

View File

@ -46,6 +46,8 @@ brain_pin_e getSckPin(spi_device_e device);
#if EFI_PROD_CODE
#include "engine.h"
#include "debounce.h"
void applyNewHardwareSettings(void);
void initHardware(Logging *logging);
#endif /* EFI_PROD_CODE */
@ -53,5 +55,6 @@ void initHardware(Logging *logging);
void showBor(void);
void setBor(int borValue);
#endif /* __cplusplus */
class ButtonDebounce;
#endif /* __cplusplus */