@@ -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
345348void 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
570582void 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
577589void GP2040::handleSystemReboot (GPEvent* e) {
578- rebootRequested = true ;
590+ rebootRequested = true ;
579591 rebootMode = ((GPRestartEvent*)e)->bootMode ;
580592}
0 commit comments