Skip to content

Commit

Permalink
convert DDI pins to gpioMappings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bsstephan committed Sep 19, 2023
1 parent fe5fa56 commit 140bae1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 56 deletions.
8 changes: 4 additions & 4 deletions headers/addons/dualdirectional.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions proto/enums.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 20 additions & 30 deletions src/addons/dualdirectional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/config_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DpadMode>(legacyAddonOptions.dualDirDpadMode));
Expand Down
35 changes: 31 additions & 4 deletions src/config_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DpadMode>(DUAL_DIRECTIONAL_STICK_MODE));
INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, combineMode, DUAL_DIRECTIONAL_COMBINE_MODE);
INIT_UNSET_PROPERTY(config.addonOptions.dualDirectionalOptions, fourWayMode, false);
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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]);
Expand Down
34 changes: 24 additions & 10 deletions src/configs/webconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down

0 comments on commit 140bae1

Please sign in to comment.