From cab4cd0df2c82728d20d8659f32812d178bb4e26 Mon Sep 17 00:00:00 2001 From: David Holdeman Date: Tue, 8 Sep 2020 10:29:38 -0500 Subject: [PATCH] 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 --- firmware/controllers/buttonshift.cpp | 4 +- firmware/hw_layer/debounce.cpp | 56 +++++++++++++++++++++++++--- firmware/hw_layer/debounce.h | 18 +++++++-- firmware/hw_layer/hardware.cpp | 5 ++- firmware/hw_layer/hardware.h | 5 ++- 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/firmware/controllers/buttonshift.cpp b/firmware/controllers/buttonshift.cpp index e74c1a5bae..f44684dc0c 100644 --- a/firmware/controllers/buttonshift.cpp +++ b/firmware/controllers/buttonshift.cpp @@ -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() { diff --git a/firmware/hw_layer/debounce.cpp b/firmware/hw_layer/debounce.cpp index 185162aaec..d4b12a3b68 100644 --- a/firmware/hw_layer/debounce.cpp +++ b/firmware/hw_layer/debounce.cpp @@ -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 diff --git a/firmware/hw_layer/debounce.h b/firmware/hw_layer/debounce.h index 88594e2ff1..bfda7877e2 100644 --- a/firmware/hw_layer/debounce.h +++ b/firmware/hw_layer/debounce.h @@ -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 */ diff --git a/firmware/hw_layer/hardware.cpp b/firmware/hw_layer/hardware.cpp index 4f7e6cd279..c619724a31 100644 --- a/firmware/hw_layer/hardware.cpp +++ b/firmware/hw_layer/hardware.cpp @@ -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 diff --git a/firmware/hw_layer/hardware.h b/firmware/hw_layer/hardware.h index 5ee79a8083..16bff05c9c 100644 --- a/firmware/hw_layer/hardware.h +++ b/firmware/hw_layer/hardware.h @@ -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 */