Skip to content

Commit dfc4185

Browse files
authored
Refactor gamepad profile handling and update display mappings (#1525)
1 parent 3ab5d72 commit dfc4185

File tree

6 files changed

+82
-72
lines changed

6 files changed

+82
-72
lines changed

headers/addons/display.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,13 @@ class DisplayAddon : public GPAddon
197197
virtual void reinit() {}
198198
virtual std::string name() { return DisplayName; }
199199

200+
void handleProfileChange(GPEvent* e);
200201
void handleSystemRestart(GPEvent* e);
201202
void handleMenuNavigation(GPEvent* e);
202203
void handleSystemError(GPEvent* e);
203204
private:
204205
bool updateDisplayScreen();
205-
void drawStatusBar(Gamepad*);
206-
void initMenu(char**);
207-
bool pressedUp();
208-
bool pressedDown();
209-
bool pressedLeft();
210-
bool pressedRight();
206+
void setMenuMappings();
211207
const DisplayOptions& getDisplayOptions();
212208
bool isDisplayPowerOff();
213209
void setDisplayPower(uint8_t status);

headers/gamepad.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class Gamepad {
193193
// see GP2040::debounceGpioGetAll for details
194194
Mask_t debouncedGpio;
195195

196-
bool userRequestedReinit = false;
196+
uint32_t lastReinitProfileNumber = 0;
197197

198198
// These are special to SOCD
199199
inline static const SOCDMode resolveSOCDMode(const GamepadOptions& options) {
@@ -206,9 +206,6 @@ class Gamepad {
206206
};
207207

208208
private:
209-
210-
uint8_t getModifier(uint8_t code);
211-
uint8_t getMultimedia(uint8_t code);
212209
void processHotkeyAction(GamepadHotkey action);
213210

214211
GamepadOptions & options;

src/addons/display.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ void DisplayAddon::setup() {
5252
turnOffWhenSuspended = options.turnOffWhenSuspended;
5353
displaySaverMode = options.displaySaverMode;
5454

55-
mapMenuToggle = new GamepadButtonMapping(0);
56-
mapMenuSelect = new GamepadButtonMapping(0);
57-
GpioMappingInfo* pinMappings = Storage::getInstance().getProfilePinMappings();
58-
for (Pin_t pin = 0; pin < (Pin_t)NUM_BANK0_GPIOS; pin++) {
59-
switch (pinMappings[pin].action) {
60-
case GpioAction::MENU_NAVIGATION_TOGGLE: mapMenuToggle->pinMask |= 1 << pin; break;
61-
case GpioAction::MENU_NAVIGATION_SELECT: mapMenuSelect->pinMask |= 1 << pin; break;
62-
default: break;
63-
}
64-
}
65-
6655
prevValues = Storage::getInstance().GetGamepad()->debouncedGpio;
6756

6857
// set current display mode
@@ -77,7 +66,9 @@ void DisplayAddon::setup() {
7766
}
7867
gpScreen = nullptr;
7968
updateDisplayScreen();
69+
setMenuMappings();
8070

71+
EventManager::getInstance().registerEventHandler(GP_EVENT_PROFILE_CHANGE, GPEVENT_CALLBACK(this->handleProfileChange(event)));
8172
EventManager::getInstance().registerEventHandler(GP_EVENT_RESTART, GPEVENT_CALLBACK(this->handleSystemRestart(event)));
8273
EventManager::getInstance().registerEventHandler(GP_EVENT_MENU_NAVIGATE, GPEVENT_CALLBACK(this->handleMenuNavigation(event)));
8374
EventManager::getInstance().registerEventHandler(GP_EVENT_SYSTEM_ERROR, GPEVENT_CALLBACK(this->handleSystemError(event)));
@@ -175,6 +166,20 @@ void DisplayAddon::setDisplayPower(uint8_t status)
175166
}
176167
}
177168

169+
void DisplayAddon::setMenuMappings()
170+
{
171+
mapMenuToggle = new GamepadButtonMapping(0);
172+
mapMenuSelect = new GamepadButtonMapping(0);
173+
GpioMappingInfo* pinMappings = Storage::getInstance().getProfilePinMappings();
174+
for (Pin_t pin = 0; pin < (Pin_t)NUM_BANK0_GPIOS; pin++) {
175+
switch (pinMappings[pin].action) {
176+
case GpioAction::MENU_NAVIGATION_TOGGLE: mapMenuToggle->pinMask |= 1 << pin; break;
177+
case GpioAction::MENU_NAVIGATION_SELECT: mapMenuSelect->pinMask |= 1 << pin; break;
178+
default: break;
179+
}
180+
}
181+
}
182+
178183
void DisplayAddon::process() {
179184
// If GPDisplay is not loaded or we're in standard mode with display power off enabled
180185
if (gpDisplay->getDriver() == nullptr ||
@@ -217,6 +222,14 @@ const DisplayOptions& DisplayAddon::getDisplayOptions() {
217222
return Storage::getInstance().getDisplayOptions();
218223
}
219224

225+
void DisplayAddon::handleProfileChange(GPEvent* e)
226+
{
227+
delete mapMenuToggle;
228+
delete mapMenuSelect;
229+
mapMenuToggle = nullptr;
230+
mapMenuSelect = nullptr;
231+
setMenuMappings();
232+
}
220233

221234
void DisplayAddon::handleSystemRestart(GPEvent* e) {
222235
nextDisplayMode = DisplayMode::RESTART;

src/gamepad.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -625,62 +625,54 @@ void Gamepad::processHotkeyAction(GamepadHotkey action) {
625625
case HOTKEY_LOAD_PROFILE_1:
626626
if (action != lastAction) {
627627
if (Storage::getInstance().setProfile(1)) {
628-
userRequestedReinit = true;
629628
reqSave = true;
630629
}
631630
}
632631
break;
633632
case HOTKEY_LOAD_PROFILE_2:
634633
if (action != lastAction) {
635634
if (Storage::getInstance().setProfile(2)) {
636-
userRequestedReinit = true;
637635
reqSave = true;
638636
}
639637
}
640638
break;
641639
case HOTKEY_LOAD_PROFILE_3:
642640
if (action != lastAction) {
643641
if (Storage::getInstance().setProfile(3)) {
644-
userRequestedReinit = true;
645642
reqSave = true;
646643
}
647644
}
648645
break;
649646
case HOTKEY_LOAD_PROFILE_4:
650647
if (action != lastAction) {
651648
if (Storage::getInstance().setProfile(4)) {
652-
userRequestedReinit = true;
653649
reqSave = true;
654650
}
655651
}
656652
break;
657653
case HOTKEY_LOAD_PROFILE_5:
658654
if (action != lastAction) {
659655
if (Storage::getInstance().setProfile(5)) {
660-
userRequestedReinit = true;
661656
reqSave = true;
662657
}
663658
}
664659
break;
665660
case HOTKEY_LOAD_PROFILE_6:
666661
if (action != lastAction) {
667662
if (Storage::getInstance().setProfile(6)) {
668-
userRequestedReinit = true;
669663
reqSave = true;
670664
}
671665
}
672666
break;
673667
case HOTKEY_NEXT_PROFILE:
674668
if (action != lastAction) {
675669
Storage::getInstance().nextProfile();
676-
userRequestedReinit = true;
677670
reqSave = true;
678671
}
679672
break;
680673
case HOTKEY_PREVIOUS_PROFILE:
681674
if (action != lastAction) {
682675
Storage::getInstance().previousProfile();
683-
userRequestedReinit = true;
684676
reqSave = true;
685677
}
686678
break;

src/gp2040.cpp

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,33 @@ void GP2040::setup() {
7272
// Set pin mappings for all GPIO functions
7373
Storage::getInstance().setFunctionalPinMappings();
7474

75-
// power up...
75+
// power up...
7676
gamepad->auxState.power.pluggedIn = true;
7777
gamepad->auxState.power.charging = false;
7878
gamepad->auxState.power.level = GAMEPAD_AUX_MAX_POWER;
7979

8080
// Setup Gamepad
8181
gamepad->setup();
82-
82+
83+
// Initialize last reinit profile to current so we don't reinit on first loop
84+
gamepad->lastReinitProfileNumber = Storage::getInstance().getGamepadOptions().profileNumber;
85+
8386
// now we can load the latest configured profile, which will map the
8487
// new set of GPIOs to use...
85-
this->initializeStandardGpio();
88+
this->initializeStandardGpio();
8689

87-
const GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();
90+
const GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();
8891

89-
// check setup options and add modes to the list
90-
// user modes
91-
bootActions.insert({GAMEPAD_MASK_B1, gamepadOptions.inputModeB1});
92-
bootActions.insert({GAMEPAD_MASK_B2, gamepadOptions.inputModeB2});
93-
bootActions.insert({GAMEPAD_MASK_B3, gamepadOptions.inputModeB3});
94-
bootActions.insert({GAMEPAD_MASK_B4, gamepadOptions.inputModeB4});
95-
bootActions.insert({GAMEPAD_MASK_L1, gamepadOptions.inputModeL1});
96-
bootActions.insert({GAMEPAD_MASK_L2, gamepadOptions.inputModeL2});
97-
bootActions.insert({GAMEPAD_MASK_R1, gamepadOptions.inputModeR1});
98-
bootActions.insert({GAMEPAD_MASK_R2, gamepadOptions.inputModeR2});
92+
// check setup options and add modes to the list
93+
// user modes
94+
bootActions.insert({GAMEPAD_MASK_B1, gamepadOptions.inputModeB1});
95+
bootActions.insert({GAMEPAD_MASK_B2, gamepadOptions.inputModeB2});
96+
bootActions.insert({GAMEPAD_MASK_B3, gamepadOptions.inputModeB3});
97+
bootActions.insert({GAMEPAD_MASK_B4, gamepadOptions.inputModeB4});
98+
bootActions.insert({GAMEPAD_MASK_L1, gamepadOptions.inputModeL1});
99+
bootActions.insert({GAMEPAD_MASK_L2, gamepadOptions.inputModeL2});
100+
bootActions.insert({GAMEPAD_MASK_R1, gamepadOptions.inputModeR1});
101+
bootActions.insert({GAMEPAD_MASK_R2, gamepadOptions.inputModeR2});
99102

100103
// Initialize our ADC (various add-ons)
101104
adc_init();
@@ -277,10 +280,10 @@ void GP2040::run() {
277280
GPDriver * inputDriver = DriverManager::getInstance().getDriver();
278281
Gamepad * gamepad = Storage::getInstance().GetGamepad();
279282
Gamepad * processedGamepad = Storage::getInstance().GetProcessedGamepad();
280-
GamepadState prevState;
283+
GamepadState prevState;
281284

282-
// Start the TinyUSB Device functionality
283-
tud_init(TUD_OPT_RHPORT);
285+
// Start the TinyUSB Device functionality
286+
tud_init(TUD_OPT_RHPORT);
284287

285288
// Initialize our USB manager
286289
USBHostManager::getInstance().start();
@@ -317,7 +320,7 @@ void GP2040::run() {
317320

318321
gamepad->hotkey(); // check for MPGS hotkeys
319322
rebootHotkeys.process(gamepad, configMode);
320-
323+
321324
gamepad->process(); // process through MPGS
322325

323326
// (Post) Process for add-ons
@@ -330,7 +333,7 @@ void GP2040::run() {
330333

331334
// Process Input Driver
332335
bool processed = inputDriver->process(gamepad);
333-
336+
334337
// TinyUSB Task update
335338
tud_task();
336339

@@ -343,8 +346,13 @@ void GP2040::run() {
343346
}
344347

345348
void GP2040::getReinitGamepad(Gamepad * gamepad) {
346-
// check if we should reinitialize the gamepad
347-
if (gamepad->userRequestedReinit) {
349+
GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();
350+
351+
// Check if profile has changed since last reinit
352+
if (gamepad->lastReinitProfileNumber != gamepadOptions.profileNumber) {
353+
uint32_t previousProfile = gamepad->lastReinitProfileNumber;
354+
uint32_t currentProfile = gamepadOptions.profileNumber;
355+
348356
// deinitialize the ordinary (non-reserved, non-addon) GPIO pins, since
349357
// we are moving off of them and onto potentially different pin assignments
350358
// we currently don't support ASSIGNED_TO_ADDON pins being reinitialized,
@@ -361,12 +369,16 @@ void GP2040::getReinitGamepad(Gamepad * gamepad) {
361369
// now we can tell the gamepad that the new mappings are in place
362370
// and ready to use, and the pins are ready, so it should reinitialize itself
363371
gamepad->reinit();
372+
364373
// ...and addons on this core, if they implemented reinit (just things
365374
// with simple GPIO pin usage, at time of writing)
366375
addons.ReinitializeAddons();
367376

368-
// and we're done
369-
gamepad->userRequestedReinit = false;
377+
// Update the last reinit profile
378+
gamepad->lastReinitProfileNumber = currentProfile;
379+
380+
// Trigger the profile change event now that reinit is complete
381+
EventManager::getInstance().triggerEvent(new GPProfileChangeEvent(previousProfile, currentProfile));
370382
}
371383
}
372384

@@ -380,13 +392,13 @@ GP2040::BootAction GP2040::getBootAction() {
380392
// Determine boot action based on gamepad state during boot
381393
Gamepad * gamepad = Storage::getInstance().GetGamepad();
382394
Gamepad * processedGamepad = Storage::getInstance().GetProcessedGamepad();
383-
395+
384396
debounceGpioGetAll();
385397
gamepad->read();
386398

387399
// Pre-Process add-ons for MPGS
388400
addons.PreprocessAddons();
389-
401+
390402
gamepad->process(); // process through MPGS
391403

392404
// Process for add-ons
@@ -410,37 +422,37 @@ GP2040::BootAction GP2040::getBootAction() {
410422
if (!modeSwitchLocked) {
411423
if (auto search = bootActions.find(gamepad->state.buttons); search != bootActions.end()) {
412424
switch (search->second) {
413-
case INPUT_MODE_XINPUT:
425+
case INPUT_MODE_XINPUT:
414426
return BootAction::SET_INPUT_MODE_XINPUT;
415-
case INPUT_MODE_SWITCH:
427+
case INPUT_MODE_SWITCH:
416428
return BootAction::SET_INPUT_MODE_SWITCH;
417-
case INPUT_MODE_KEYBOARD:
429+
case INPUT_MODE_KEYBOARD:
418430
return BootAction::SET_INPUT_MODE_KEYBOARD;
419431
case INPUT_MODE_GENERIC:
420432
return BootAction::SET_INPUT_MODE_GENERIC;
421433
case INPUT_MODE_PS3:
422434
return BootAction::SET_INPUT_MODE_PS3;
423-
case INPUT_MODE_PS4:
435+
case INPUT_MODE_PS4:
424436
return BootAction::SET_INPUT_MODE_PS4;
425-
case INPUT_MODE_PS5:
437+
case INPUT_MODE_PS5:
426438
return BootAction::SET_INPUT_MODE_PS5;
427-
case INPUT_MODE_NEOGEO:
439+
case INPUT_MODE_NEOGEO:
428440
return BootAction::SET_INPUT_MODE_NEOGEO;
429-
case INPUT_MODE_MDMINI:
441+
case INPUT_MODE_MDMINI:
430442
return BootAction::SET_INPUT_MODE_MDMINI;
431-
case INPUT_MODE_PCEMINI:
443+
case INPUT_MODE_PCEMINI:
432444
return BootAction::SET_INPUT_MODE_PCEMINI;
433-
case INPUT_MODE_EGRET:
445+
case INPUT_MODE_EGRET:
434446
return BootAction::SET_INPUT_MODE_EGRET;
435-
case INPUT_MODE_ASTRO:
447+
case INPUT_MODE_ASTRO:
436448
return BootAction::SET_INPUT_MODE_ASTRO;
437-
case INPUT_MODE_PSCLASSIC:
449+
case INPUT_MODE_PSCLASSIC:
438450
return BootAction::SET_INPUT_MODE_PSCLASSIC;
439-
case INPUT_MODE_XBOXORIGINAL:
451+
case INPUT_MODE_XBOXORIGINAL:
440452
return BootAction::SET_INPUT_MODE_XBOXORIGINAL;
441453
case INPUT_MODE_XBONE:
442454
return BootAction::SET_INPUT_MODE_XBONE;
443-
case INPUT_MODE_SWITCH_PRO:
455+
case INPUT_MODE_SWITCH_PRO:
444456
return BootAction::SET_INPUT_MODE_SWITCH_PRO;
445457
default:
446458
return BootAction::NONE;
@@ -568,13 +580,13 @@ void GP2040::checkSaveRebootState() {
568580
}
569581

570582
void GP2040::handleStorageSave(GPEvent* e) {
571-
saveRequested = true;
572-
forceSave = ((GPStorageSaveEvent*)e)->forceSave;
573-
rebootRequested = ((GPStorageSaveEvent*)e)->restartAfterSave;
583+
saveRequested = true;
584+
forceSave = ((GPStorageSaveEvent*)e)->forceSave;
585+
rebootRequested = ((GPStorageSaveEvent*)e)->restartAfterSave;
574586
rebootMode = System::BootMode::DEFAULT;
575587
}
576588

577589
void GP2040::handleSystemReboot(GPEvent* e) {
578-
rebootRequested = true;
590+
rebootRequested = true;
579591
rebootMode = ((GPRestartEvent*)e)->bootMode;
580592
}

src/storagemanager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool Storage::setProfile(const uint32_t profileNum)
6767
// is this profile enabled?
6868
// profile 1 (core) is always enabled, others we must check
6969
if (profileNum == 1 || config.profileOptions.gpioMappingsSets[profileNum-2].enabled) {
70-
EventManager::getInstance().triggerEvent(new GPProfileChangeEvent(this->config.gamepadOptions.profileNumber, profileNum));
70+
// Update the profile number - reinit will be triggered automatically in gp2040.cpp
7171
this->config.gamepadOptions.profileNumber = profileNum;
7272
return true;
7373
}

0 commit comments

Comments
 (0)