Skip to content

Commit

Permalink
define a custom button combo to a GPIO pin
Browse files Browse the repository at this point in the history
this expands GpioMappingInfo to include dpad and button masks and uses
those masks when setting up the button maps in Gamepad. this allows for
a GPIO pin to trigger any arbitrary set of buttons
  • Loading branch information
bsstephan committed Feb 13, 2024
1 parent 180e5c3 commit 8610486
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ message PinMappings
message GpioMappingInfo
{
optional GpioAction action = 1;
optional uint32 customDpadMask = 2;
optional uint32 customButtonMask = 3;
}

message GpioMappings
Expand Down
1 change: 1 addition & 0 deletions proto/enums.proto
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ enum GpioAction
SUSTAIN_SOCD_MODE_FIRST_WIN = 30;
SUSTAIN_SOCD_MODE_BYPASS = 31;
BUTTON_PRESS_TURBO = 32;
CUSTOM_BUTTON_COMBO = 33;
}

enum GamepadHotkey
Expand Down
22 changes: 22 additions & 0 deletions src/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ void Gamepad::setup()
mapButtonA2 = new GamepadButtonMapping(GAMEPAD_MASK_A2);
mapButtonFn = new GamepadButtonMapping(AUX_MASK_FUNCTION);

const auto assignCustomMappingToMaps = [&](GpioMappingInfo mapInfo, Pin_t pin) -> void {
if (mapDpadUp->buttonMask & mapInfo.customDpadMask) mapDpadUp->pinMask |= 1 << pin;
if (mapDpadDown->buttonMask & mapInfo.customDpadMask) mapDpadDown->pinMask |= 1 << pin;
if (mapDpadLeft->buttonMask & mapInfo.customDpadMask) mapDpadLeft->pinMask |= 1 << pin;
if (mapDpadRight->buttonMask & mapInfo.customDpadMask) mapDpadRight->pinMask |= 1 << pin;
if (mapButtonB1->buttonMask & mapInfo.customButtonMask) mapButtonB1->pinMask |= 1 << pin;
if (mapButtonB2->buttonMask & mapInfo.customButtonMask) mapButtonB2->pinMask |= 1 << pin;
if (mapButtonB3->buttonMask & mapInfo.customButtonMask) mapButtonB3->pinMask |= 1 << pin;
if (mapButtonB4->buttonMask & mapInfo.customButtonMask) mapButtonB4->pinMask |= 1 << pin;
if (mapButtonL1->buttonMask & mapInfo.customButtonMask) mapButtonL1->pinMask |= 1 << pin;
if (mapButtonR1->buttonMask & mapInfo.customButtonMask) mapButtonR1->pinMask |= 1 << pin;
if (mapButtonL2->buttonMask & mapInfo.customButtonMask) mapButtonL2->pinMask |= 1 << pin;
if (mapButtonR2->buttonMask & mapInfo.customButtonMask) mapButtonR2->pinMask |= 1 << pin;
if (mapButtonS1->buttonMask & mapInfo.customButtonMask) mapButtonS1->pinMask |= 1 << pin;
if (mapButtonS2->buttonMask & mapInfo.customButtonMask) mapButtonS2->pinMask |= 1 << pin;
if (mapButtonL3->buttonMask & mapInfo.customButtonMask) mapButtonL3->pinMask |= 1 << pin;
if (mapButtonR3->buttonMask & mapInfo.customButtonMask) mapButtonR3->pinMask |= 1 << pin;
if (mapButtonA1->buttonMask & mapInfo.customButtonMask) mapButtonA1->pinMask |= 1 << pin;
if (mapButtonA2->buttonMask & mapInfo.customButtonMask) mapButtonA2->pinMask |= 1 << pin;
};

for (Pin_t pin = 0; pin < (Pin_t)NUM_BANK0_GPIOS; pin++)
{
switch (pinMappings[pin].action) {
Expand All @@ -78,6 +99,7 @@ void Gamepad::setup()
case GpioAction::BUTTON_PRESS_A1: mapButtonA1->pinMask |= 1 << pin; break;
case GpioAction::BUTTON_PRESS_A2: mapButtonA2->pinMask |= 1 << pin; break;
case GpioAction::BUTTON_PRESS_FN: mapButtonFn->pinMask |= 1 << pin; break;
case GpioAction::CUSTOM_BUTTON_COMBO: assignCustomMappingToMaps(pinMappings[pin], pin); break;
default: break;
}
}
Expand Down

0 comments on commit 8610486

Please sign in to comment.