From 140bae17cc8e81e60e3c1b1d725f5745dd71d35c Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Wed, 13 Sep 2023 22:32:08 -0500 Subject: [PATCH] convert DDI pins to gpioMappings the rest of the DDI settings remain untouched, and the whole addon is basically where it always was, this just allows for centralization in the new GPIO mappings --- headers/addons/dualdirectional.h | 8 ++--- proto/config.proto | 8 ++--- proto/enums.proto | 4 +++ src/addons/dualdirectional.cpp | 50 +++++++++++++------------------- src/config_legacy.cpp | 8 ++--- src/config_utils.cpp | 35 +++++++++++++++++++--- src/configs/webconfig.cpp | 34 +++++++++++++++------- 7 files changed, 91 insertions(+), 56 deletions(-) diff --git a/headers/addons/dualdirectional.h b/headers/addons/dualdirectional.h index 89e349045..0a465f28e 100644 --- a/headers/addons/dualdirectional.h +++ b/headers/addons/dualdirectional.h @@ -67,12 +67,12 @@ class DualDirectionalInput : public GPAddon { DpadDirection lastDualUD; // Dual Last Up-Down DpadDirection lastDualLR; // Gamepad Last Left-Right uint32_t dpadTime[4]; - uint8_t pinDualDirDown; - uint8_t pinDualDirUp; - uint8_t pinDualDirLeft; - uint8_t pinDualDirRight; uint8_t combineMode; DpadMode dpadMode; + GamepadButtonMapping *mapDpadUp; + GamepadButtonMapping *mapDpadDown; + GamepadButtonMapping *mapDpadLeft; + GamepadButtonMapping *mapDpadRight; }; #endif // _DualDirectional_H diff --git a/proto/config.proto b/proto/config.proto index cc8fc9c8a..2c30f18a6 100644 --- a/proto/config.proto +++ b/proto/config.proto @@ -394,10 +394,10 @@ message DualDirectionalOptions { optional bool enabled = 1; - optional int32 upPin = 2; - optional int32 downPin = 3; - optional int32 leftPin = 4; - optional int32 rightPin = 5; + optional int32 deprecatedUpPin = 2; + optional int32 deprecatedDownPin = 3; + optional int32 deprecatedLeftPin = 4; + optional int32 deprecatedRightPin = 5; optional DpadMode dpadMode = 6; optional uint32 combineMode = 7; diff --git a/proto/enums.proto b/proto/enums.proto index e03d81666..df9a4cdce 100644 --- a/proto/enums.proto +++ b/proto/enums.proto @@ -146,6 +146,10 @@ enum GpioAction BUTTON_PRESS_L3 = 17; BUTTON_PRESS_R3 = 18; BUTTON_PRESS_FN = 19; + BUTTON_PRESS_DDI_UP = 20; + BUTTON_PRESS_DDI_DOWN = 21; + BUTTON_PRESS_DDI_LEFT = 22; + BUTTON_PRESS_DDI_RIGHT = 23; } enum GamepadHotkey diff --git a/src/addons/dualdirectional.cpp b/src/addons/dualdirectional.cpp index fc07a125a..697629f8f 100644 --- a/src/addons/dualdirectional.cpp +++ b/src/addons/dualdirectional.cpp @@ -11,24 +11,22 @@ bool DualDirectionalInput::available() { void DualDirectionalInput::setup() { const DualDirectionalOptions& options = Storage::getInstance().getAddonOptions().dualDirectionalOptions; combineMode = options.combineMode; - pinDualDirDown = options.downPin; - pinDualDirUp = options.upPin; - pinDualDirLeft = options.leftPin; - pinDualDirRight = options.rightPin; - dpadMode = options.dpadMode; - // Setup TURBO Key - uint8_t pinDualDir[4] = {pinDualDirDown, - pinDualDirUp, - pinDualDirLeft, - pinDualDirRight}; - - for (int i = 0; i < 4; i++) { - if ( isValidPin(pinDualDir[i]) ) { - gpio_init(pinDualDir[i]); // Initialize pin - gpio_set_dir(pinDualDir[i], GPIO_IN); // Set as INPUT - gpio_pull_up(pinDualDir[i]); // Set as PULLUP + mapDpadUp = new GamepadButtonMapping(GAMEPAD_MASK_UP); + mapDpadDown = new GamepadButtonMapping(GAMEPAD_MASK_DOWN); + mapDpadLeft = new GamepadButtonMapping(GAMEPAD_MASK_LEFT); + mapDpadRight = new GamepadButtonMapping(GAMEPAD_MASK_RIGHT); + + GpioAction* pinMappings = Storage::getInstance().getProfilePinMappings(); + for (uint32_t pin = 0; pin < NUM_BANK0_GPIOS; pin++) + { + switch (pinMappings[pin]) { + case GpioAction::BUTTON_PRESS_DDI_UP: mapDpadUp->pinMask |= 1 << pin; break; + case GpioAction::BUTTON_PRESS_DDI_DOWN: mapDpadDown->pinMask |= 1 << pin; break; + case GpioAction::BUTTON_PRESS_DDI_LEFT: mapDpadLeft->pinMask |= 1 << pin; break; + case GpioAction::BUTTON_PRESS_DDI_RIGHT: mapDpadRight->pinMask |= 1 << pin; break; + default: break; } } @@ -66,21 +64,13 @@ void DualDirectionalInput::debounce() void DualDirectionalInput::preprocess() { Gamepad * gamepad = Storage::getInstance().GetGamepad(); + uint32_t values = ~gpio_get_all(); - // Need to invert since we're using pullups - dualState = 0; - if ( pinDualDirUp != (uint8_t)-1 ) { - dualState |= (!gpio_get(pinDualDirUp) ? gamepad->mapDpadUp->buttonMask : 0); - } - if ( pinDualDirDown != (uint8_t)-1 ) { - dualState |= (!gpio_get(pinDualDirDown) ? gamepad->mapDpadDown->buttonMask : 0); - } - if ( pinDualDirLeft != (uint8_t)-1 ) { - dualState |= (!gpio_get(pinDualDirLeft) ? gamepad->mapDpadLeft->buttonMask : 0); - } - if ( pinDualDirRight != (uint8_t)-1 ) { - dualState |= (!gpio_get(pinDualDirRight) ? gamepad->mapDpadRight->buttonMask : 0); - } + dualState = 0 + | (values & mapDpadUp->pinMask) ? mapDpadUp->buttonMask : 0 + | (values & mapDpadDown->pinMask) ? mapDpadDown->buttonMask : 0 + | (values & mapDpadLeft->pinMask) ? mapDpadLeft->buttonMask : 0 + | (values & mapDpadRight->pinMask)? mapDpadRight->buttonMask : 0; // Debounce our directional pins debounce(); diff --git a/src/config_legacy.cpp b/src/config_legacy.cpp index 23d1010ee..7b36a69c0 100644 --- a/src/config_legacy.cpp +++ b/src/config_legacy.cpp @@ -959,10 +959,10 @@ bool ConfigUtils::fromLegacyStorage(Config& config) DualDirectionalOptions& dualDirectionalOptions = config.addonOptions.dualDirectionalOptions; config.addonOptions.has_dualDirectionalOptions = true; SET_PROPERTY(dualDirectionalOptions, enabled, legacyAddonOptions.DualDirectionalInputEnabled); - SET_PROPERTY(dualDirectionalOptions, upPin, bytePinToIntPin(legacyAddonOptions.pinDualDirUp)); - SET_PROPERTY(dualDirectionalOptions, downPin, bytePinToIntPin(legacyAddonOptions.pinDualDirDown)); - SET_PROPERTY(dualDirectionalOptions, leftPin, bytePinToIntPin(legacyAddonOptions.pinDualDirLeft)); - SET_PROPERTY(dualDirectionalOptions, rightPin, bytePinToIntPin(legacyAddonOptions.pinDualDirRight)); + SET_PROPERTY(dualDirectionalOptions, deprecatedUpPin, bytePinToIntPin(legacyAddonOptions.pinDualDirUp)); + SET_PROPERTY(dualDirectionalOptions, deprecatedDownPin, bytePinToIntPin(legacyAddonOptions.pinDualDirDown)); + SET_PROPERTY(dualDirectionalOptions, deprecatedLeftPin, bytePinToIntPin(legacyAddonOptions.pinDualDirLeft)); + SET_PROPERTY(dualDirectionalOptions, deprecatedRightPin, bytePinToIntPin(legacyAddonOptions.pinDualDirRight)); if (isValidDpadMode(legacyAddonOptions.dualDirDpadMode)) { SET_PROPERTY(dualDirectionalOptions, dpadMode, static_cast(legacyAddonOptions.dualDirDpadMode)); diff --git a/src/config_utils.cpp b/src/config_utils.cpp index a5ec6d8c0..262055c6b 100644 --- a/src/config_utils.cpp +++ b/src/config_utils.cpp @@ -442,10 +442,6 @@ void ConfigUtils::initUnsetPropertiesWithDefaults(Config& config) // addonOptions.dualDirectionalOptions INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, enabled, !!DUAL_DIRECTIONAL_ENABLED); - INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, upPin, PIN_DUAL_DIRECTIONAL_UP); - INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, downPin, PIN_DUAL_DIRECTIONAL_DOWN) - INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, leftPin, PIN_DUAL_DIRECTIONAL_LEFT); - INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, rightPin, PIN_DUAL_DIRECTIONAL_RIGHT); INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, dpadMode, static_cast(DUAL_DIRECTIONAL_STICK_MODE)); INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, combineMode, DUAL_DIRECTIONAL_COMBINE_MODE); INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, fourWayMode, false); @@ -560,6 +556,7 @@ void gpioMappingsMigrationCore(Config& config) { PinMappings& deprecatedPinMappings = config.deprecatedPinMappings; ExtraButtonOptions& extraButtonOptions = config.addonOptions.deprecatedExtraButtonOptions; + DualDirectionalOptions& ddiOptions = config.addonOptions.dualDirectionalOptions; const auto gamepadMaskToGpioAction = [&](uint32_t gpMask) -> GpioAction { @@ -762,6 +759,36 @@ void gpioMappingsMigrationCore(Config& config) else if (isValidPin(EXTRA_BUTTON_PIN)) actions[EXTRA_BUTTON_PIN] = gamepadMaskToGpioAction(EXTRA_BUTTON_MASK); + // convert DDI direction pin mapping to GPIO mapping config + if (ddiOptions.enabled && isValidPin(ddiOptions.deprecatedUpPin)) { + actions[ddiOptions.deprecatedUpPin] = GpioAction::BUTTON_PRESS_DDI_UP; + ddiOptions.deprecatedUpPin = -1; + } + else if (isValidPin(PIN_DUAL_DIRECTIONAL_UP)) { + actions[PIN_DUAL_DIRECTIONAL_UP] = GpioAction::BUTTON_PRESS_DDI_UP; + } + if (ddiOptions.enabled && isValidPin(ddiOptions.deprecatedDownPin)) { + actions[ddiOptions.deprecatedDownPin] = GpioAction::BUTTON_PRESS_DDI_DOWN; + ddiOptions.deprecatedDownPin = -1; + } + else if (isValidPin(PIN_DUAL_DIRECTIONAL_DOWN)) { + actions[PIN_DUAL_DIRECTIONAL_DOWN] = GpioAction::BUTTON_PRESS_DDI_DOWN; + } + if (ddiOptions.enabled && isValidPin(ddiOptions.deprecatedLeftPin)) { + actions[ddiOptions.deprecatedLeftPin] = GpioAction::BUTTON_PRESS_DDI_LEFT; + ddiOptions.deprecatedLeftPin = -1; + } + else if (isValidPin(PIN_DUAL_DIRECTIONAL_LEFT)) { + actions[PIN_DUAL_DIRECTIONAL_LEFT] = GpioAction::BUTTON_PRESS_DDI_LEFT; + } + if (ddiOptions.enabled && isValidPin(ddiOptions.deprecatedRightPin)) { + actions[ddiOptions.deprecatedRightPin] = GpioAction::BUTTON_PRESS_DDI_RIGHT; + ddiOptions.deprecatedRightPin = -1; + } + else if (isValidPin(PIN_DUAL_DIRECTIONAL_RIGHT)) { + actions[PIN_DUAL_DIRECTIONAL_RIGHT] = GpioAction::BUTTON_PRESS_DDI_RIGHT; + } + INIT_UNSET_PROPERTY(config.gpioMappings, pin00, actions[0]); INIT_UNSET_PROPERTY(config.gpioMappings, pin01, actions[1]); INIT_UNSET_PROPERTY(config.gpioMappings, pin02, actions[2]); diff --git a/src/configs/webconfig.cpp b/src/configs/webconfig.cpp index 4a9581e4b..beb0d9b10 100644 --- a/src/configs/webconfig.cpp +++ b/src/configs/webconfig.cpp @@ -959,6 +959,13 @@ std::string setAddonOptions() { DynamicJsonDocument doc = get_post_data(); + GpioAction** gpioMappings = Storage::getInstance().getGpioMappingsArray(); + // PinMappings uses -1 to denote unassigned pins + const auto convertPin = [&] (const int32_t pin, const GpioAction action) -> void + { + if (isValidPin(pin)) *gpioMappings[pin] = action; + }; + AnalogOptions& analogOptions = Storage::getInstance().getAddonOptions().analogOptions; docToPin(analogOptions.analogAdc1PinX, doc, "analogAdc1PinX"); docToPin(analogOptions.analogAdc1PinY, doc, "analogAdc1PinY"); @@ -982,11 +989,11 @@ std::string setAddonOptions() docToValue(buzzerOptions.volume, doc, "buzzerVolume"); docToValue(buzzerOptions.enabled, doc, "BuzzerSpeakerAddonEnabled"); - DualDirectionalOptions& dualDirectionalOptions = Storage::getInstance().getAddonOptions().dualDirectionalOptions; - docToPin(dualDirectionalOptions.downPin, doc, "dualDirDownPin"); - docToPin(dualDirectionalOptions.upPin, doc, "dualDirUpPin"); - docToPin(dualDirectionalOptions.leftPin, doc, "dualDirLeftPin"); - docToPin(dualDirectionalOptions.rightPin, doc, "dualDirRightPin"); + DualDirectionalOptions& dualDirectionalOptions = Storage::getInstance().getAddonOptions().dualDirectionalOptions; + convertPin(doc["dualDirUpPin"], GpioAction::BUTTON_PRESS_DDI_UP); + convertPin(doc["dualDirDownPin"], GpioAction::BUTTON_PRESS_DDI_DOWN); + convertPin(doc["dualDirLeftPin"], GpioAction::BUTTON_PRESS_DDI_LEFT); + convertPin(doc["dualDirRightPin"], GpioAction::BUTTON_PRESS_DDI_RIGHT); docToValue(dualDirectionalOptions.dpadMode, doc, "dualDirDpadMode"); docToValue(dualDirectionalOptions.combineMode, doc, "dualDirCombineMode"); docToValue(dualDirectionalOptions.fourWayMode, doc, "dualDirFourWayMode"); @@ -1374,6 +1381,17 @@ std::string getAddonOptions() { DynamicJsonDocument doc(LWIP_HTTPD_POST_MAX_PAYLOAD_LEN); + GpioAction** gpioMappings = Storage::getInstance().getGpioMappingsArray(); + for (uint32_t pin = 0; pin < NUM_BANK0_GPIOS; pin++) { + switch (*gpioMappings[pin]) { + case GpioAction::BUTTON_PRESS_DDI_UP: writeDoc(doc, "dualDirUpPin", pin); break; + case GpioAction::BUTTON_PRESS_DDI_DOWN: writeDoc(doc, "dualDirDownPin", pin); break; + case GpioAction::BUTTON_PRESS_DDI_LEFT: writeDoc(doc, "dualDirLeftPin", pin); break; + case GpioAction::BUTTON_PRESS_DDI_RIGHT: writeDoc(doc, "dualDirRightPin", pin); break; + default: break; + } + } + const AnalogOptions& analogOptions = Storage::getInstance().getAddonOptions().analogOptions; writeDoc(doc, "analogAdc1PinX", cleanPin(analogOptions.analogAdc1PinX)); writeDoc(doc, "analogAdc1PinY", cleanPin(analogOptions.analogAdc1PinY)); @@ -1397,11 +1415,7 @@ std::string getAddonOptions() writeDoc(doc, "buzzerVolume", buzzerOptions.volume); writeDoc(doc, "BuzzerSpeakerAddonEnabled", buzzerOptions.enabled); - const DualDirectionalOptions& dualDirectionalOptions = Storage::getInstance().getAddonOptions().dualDirectionalOptions; - writeDoc(doc, "dualDirDownPin", cleanPin(dualDirectionalOptions.downPin)); - writeDoc(doc, "dualDirUpPin", cleanPin(dualDirectionalOptions.upPin)); - writeDoc(doc, "dualDirLeftPin", cleanPin(dualDirectionalOptions.leftPin)); - writeDoc(doc, "dualDirRightPin", cleanPin(dualDirectionalOptions.rightPin)); + const DualDirectionalOptions& dualDirectionalOptions = Storage::getInstance().getAddonOptions().dualDirectionalOptions; writeDoc(doc, "dualDirDpadMode", dualDirectionalOptions.dpadMode); writeDoc(doc, "dualDirCombineMode", dualDirectionalOptions.combineMode); writeDoc(doc, "dualDirFourWayMode", dualDirectionalOptions.fourWayMode);