Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions headers/addons/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,13 @@ class DisplayAddon : public GPAddon
virtual void reinit() {}
virtual std::string name() { return DisplayName; }

void handleProfileChange(GPEvent* e);
void handleSystemRestart(GPEvent* e);
void handleMenuNavigation(GPEvent* e);
void handleSystemError(GPEvent* e);
private:
bool updateDisplayScreen();
void drawStatusBar(Gamepad*);
void initMenu(char**);
bool pressedUp();
bool pressedDown();
bool pressedLeft();
bool pressedRight();
void setMenuMappings();
const DisplayOptions& getDisplayOptions();
bool isDisplayPowerOff();
void setDisplayPower(uint8_t status);
Expand Down
5 changes: 1 addition & 4 deletions headers/gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Gamepad {
// see GP2040::debounceGpioGetAll for details
Mask_t debouncedGpio;

bool userRequestedReinit = false;
uint32_t lastReinitProfileNumber = 0;

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

private:

uint8_t getModifier(uint8_t code);
uint8_t getMultimedia(uint8_t code);
void processHotkeyAction(GamepadHotkey action);

GamepadOptions & options;
Expand Down
35 changes: 24 additions & 11 deletions src/addons/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ void DisplayAddon::setup() {
turnOffWhenSuspended = options.turnOffWhenSuspended;
displaySaverMode = options.displaySaverMode;

mapMenuToggle = new GamepadButtonMapping(0);
mapMenuSelect = new GamepadButtonMapping(0);
GpioMappingInfo* pinMappings = Storage::getInstance().getProfilePinMappings();
for (Pin_t pin = 0; pin < (Pin_t)NUM_BANK0_GPIOS; pin++) {
switch (pinMappings[pin].action) {
case GpioAction::MENU_NAVIGATION_TOGGLE: mapMenuToggle->pinMask |= 1 << pin; break;
case GpioAction::MENU_NAVIGATION_SELECT: mapMenuSelect->pinMask |= 1 << pin; break;
default: break;
}
}

prevValues = Storage::getInstance().GetGamepad()->debouncedGpio;

// set current display mode
Expand All @@ -77,7 +66,9 @@ void DisplayAddon::setup() {
}
gpScreen = nullptr;
updateDisplayScreen();
setMenuMappings();

EventManager::getInstance().registerEventHandler(GP_EVENT_PROFILE_CHANGE, GPEVENT_CALLBACK(this->handleProfileChange(event)));
EventManager::getInstance().registerEventHandler(GP_EVENT_RESTART, GPEVENT_CALLBACK(this->handleSystemRestart(event)));
EventManager::getInstance().registerEventHandler(GP_EVENT_MENU_NAVIGATE, GPEVENT_CALLBACK(this->handleMenuNavigation(event)));
EventManager::getInstance().registerEventHandler(GP_EVENT_SYSTEM_ERROR, GPEVENT_CALLBACK(this->handleSystemError(event)));
Expand Down Expand Up @@ -175,6 +166,20 @@ void DisplayAddon::setDisplayPower(uint8_t status)
}
}

void DisplayAddon::setMenuMappings()
{
mapMenuToggle = new GamepadButtonMapping(0);
mapMenuSelect = new GamepadButtonMapping(0);
GpioMappingInfo* pinMappings = Storage::getInstance().getProfilePinMappings();
for (Pin_t pin = 0; pin < (Pin_t)NUM_BANK0_GPIOS; pin++) {
switch (pinMappings[pin].action) {
case GpioAction::MENU_NAVIGATION_TOGGLE: mapMenuToggle->pinMask |= 1 << pin; break;
case GpioAction::MENU_NAVIGATION_SELECT: mapMenuSelect->pinMask |= 1 << pin; break;
default: break;
}
}
}

void DisplayAddon::process() {
// If GPDisplay is not loaded or we're in standard mode with display power off enabled
if (gpDisplay->getDriver() == nullptr ||
Expand Down Expand Up @@ -217,6 +222,14 @@ const DisplayOptions& DisplayAddon::getDisplayOptions() {
return Storage::getInstance().getDisplayOptions();
}

void DisplayAddon::handleProfileChange(GPEvent* e)
{
delete mapMenuToggle;
delete mapMenuSelect;
mapMenuToggle = nullptr;
mapMenuSelect = nullptr;
setMenuMappings();
}

void DisplayAddon::handleSystemRestart(GPEvent* e) {
nextDisplayMode = DisplayMode::RESTART;
Expand Down
8 changes: 0 additions & 8 deletions src/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,62 +625,54 @@ void Gamepad::processHotkeyAction(GamepadHotkey action) {
case HOTKEY_LOAD_PROFILE_1:
if (action != lastAction) {
if (Storage::getInstance().setProfile(1)) {
userRequestedReinit = true;
reqSave = true;
}
}
break;
case HOTKEY_LOAD_PROFILE_2:
if (action != lastAction) {
if (Storage::getInstance().setProfile(2)) {
userRequestedReinit = true;
reqSave = true;
}
}
break;
case HOTKEY_LOAD_PROFILE_3:
if (action != lastAction) {
if (Storage::getInstance().setProfile(3)) {
userRequestedReinit = true;
reqSave = true;
}
}
break;
case HOTKEY_LOAD_PROFILE_4:
if (action != lastAction) {
if (Storage::getInstance().setProfile(4)) {
userRequestedReinit = true;
reqSave = true;
}
}
break;
case HOTKEY_LOAD_PROFILE_5:
if (action != lastAction) {
if (Storage::getInstance().setProfile(5)) {
userRequestedReinit = true;
reqSave = true;
}
}
break;
case HOTKEY_LOAD_PROFILE_6:
if (action != lastAction) {
if (Storage::getInstance().setProfile(6)) {
userRequestedReinit = true;
reqSave = true;
}
}
break;
case HOTKEY_NEXT_PROFILE:
if (action != lastAction) {
Storage::getInstance().nextProfile();
userRequestedReinit = true;
reqSave = true;
}
break;
case HOTKEY_PREVIOUS_PROFILE:
if (action != lastAction) {
Storage::getInstance().previousProfile();
userRequestedReinit = true;
reqSave = true;
}
break;
Expand Down
96 changes: 54 additions & 42 deletions src/gp2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,33 @@ void GP2040::setup() {
// Set pin mappings for all GPIO functions
Storage::getInstance().setFunctionalPinMappings();

// power up...
// power up...
gamepad->auxState.power.pluggedIn = true;
gamepad->auxState.power.charging = false;
gamepad->auxState.power.level = GAMEPAD_AUX_MAX_POWER;

// Setup Gamepad
gamepad->setup();


// Initialize last reinit profile to current so we don't reinit on first loop
gamepad->lastReinitProfileNumber = Storage::getInstance().getGamepadOptions().profileNumber;

// now we can load the latest configured profile, which will map the
// new set of GPIOs to use...
this->initializeStandardGpio();
this->initializeStandardGpio();

const GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();
const GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();

// check setup options and add modes to the list
// user modes
bootActions.insert({GAMEPAD_MASK_B1, gamepadOptions.inputModeB1});
bootActions.insert({GAMEPAD_MASK_B2, gamepadOptions.inputModeB2});
bootActions.insert({GAMEPAD_MASK_B3, gamepadOptions.inputModeB3});
bootActions.insert({GAMEPAD_MASK_B4, gamepadOptions.inputModeB4});
bootActions.insert({GAMEPAD_MASK_L1, gamepadOptions.inputModeL1});
bootActions.insert({GAMEPAD_MASK_L2, gamepadOptions.inputModeL2});
bootActions.insert({GAMEPAD_MASK_R1, gamepadOptions.inputModeR1});
bootActions.insert({GAMEPAD_MASK_R2, gamepadOptions.inputModeR2});
// check setup options and add modes to the list
// user modes
bootActions.insert({GAMEPAD_MASK_B1, gamepadOptions.inputModeB1});
bootActions.insert({GAMEPAD_MASK_B2, gamepadOptions.inputModeB2});
bootActions.insert({GAMEPAD_MASK_B3, gamepadOptions.inputModeB3});
bootActions.insert({GAMEPAD_MASK_B4, gamepadOptions.inputModeB4});
bootActions.insert({GAMEPAD_MASK_L1, gamepadOptions.inputModeL1});
bootActions.insert({GAMEPAD_MASK_L2, gamepadOptions.inputModeL2});
bootActions.insert({GAMEPAD_MASK_R1, gamepadOptions.inputModeR1});
bootActions.insert({GAMEPAD_MASK_R2, gamepadOptions.inputModeR2});

// Initialize our ADC (various add-ons)
adc_init();
Expand Down Expand Up @@ -277,10 +280,10 @@ void GP2040::run() {
GPDriver * inputDriver = DriverManager::getInstance().getDriver();
Gamepad * gamepad = Storage::getInstance().GetGamepad();
Gamepad * processedGamepad = Storage::getInstance().GetProcessedGamepad();
GamepadState prevState;
GamepadState prevState;

// Start the TinyUSB Device functionality
tud_init(TUD_OPT_RHPORT);
// Start the TinyUSB Device functionality
tud_init(TUD_OPT_RHPORT);

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

gamepad->hotkey(); // check for MPGS hotkeys
rebootHotkeys.process(gamepad, configMode);

gamepad->process(); // process through MPGS

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

// Process Input Driver
bool processed = inputDriver->process(gamepad);

// TinyUSB Task update
tud_task();

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

void GP2040::getReinitGamepad(Gamepad * gamepad) {
// check if we should reinitialize the gamepad
if (gamepad->userRequestedReinit) {
GamepadOptions& gamepadOptions = Storage::getInstance().getGamepadOptions();

// Check if profile has changed since last reinit
if (gamepad->lastReinitProfileNumber != gamepadOptions.profileNumber) {
uint32_t previousProfile = gamepad->lastReinitProfileNumber;
uint32_t currentProfile = gamepadOptions.profileNumber;

// deinitialize the ordinary (non-reserved, non-addon) GPIO pins, since
// we are moving off of them and onto potentially different pin assignments
// we currently don't support ASSIGNED_TO_ADDON pins being reinitialized,
Expand All @@ -361,12 +369,16 @@ void GP2040::getReinitGamepad(Gamepad * gamepad) {
// now we can tell the gamepad that the new mappings are in place
// and ready to use, and the pins are ready, so it should reinitialize itself
gamepad->reinit();

// ...and addons on this core, if they implemented reinit (just things
// with simple GPIO pin usage, at time of writing)
addons.ReinitializeAddons();

// and we're done
gamepad->userRequestedReinit = false;
// Update the last reinit profile
gamepad->lastReinitProfileNumber = currentProfile;

// Trigger the profile change event now that reinit is complete
EventManager::getInstance().triggerEvent(new GPProfileChangeEvent(previousProfile, currentProfile));
}
}

Expand All @@ -380,13 +392,13 @@ GP2040::BootAction GP2040::getBootAction() {
// Determine boot action based on gamepad state during boot
Gamepad * gamepad = Storage::getInstance().GetGamepad();
Gamepad * processedGamepad = Storage::getInstance().GetProcessedGamepad();

debounceGpioGetAll();
gamepad->read();

// Pre-Process add-ons for MPGS
addons.PreprocessAddons();

gamepad->process(); // process through MPGS

// Process for add-ons
Expand All @@ -410,37 +422,37 @@ GP2040::BootAction GP2040::getBootAction() {
if (!modeSwitchLocked) {
if (auto search = bootActions.find(gamepad->state.buttons); search != bootActions.end()) {
switch (search->second) {
case INPUT_MODE_XINPUT:
case INPUT_MODE_XINPUT:
return BootAction::SET_INPUT_MODE_XINPUT;
case INPUT_MODE_SWITCH:
case INPUT_MODE_SWITCH:
return BootAction::SET_INPUT_MODE_SWITCH;
case INPUT_MODE_KEYBOARD:
case INPUT_MODE_KEYBOARD:
return BootAction::SET_INPUT_MODE_KEYBOARD;
case INPUT_MODE_GENERIC:
return BootAction::SET_INPUT_MODE_GENERIC;
case INPUT_MODE_PS3:
return BootAction::SET_INPUT_MODE_PS3;
case INPUT_MODE_PS4:
case INPUT_MODE_PS4:
return BootAction::SET_INPUT_MODE_PS4;
case INPUT_MODE_PS5:
case INPUT_MODE_PS5:
return BootAction::SET_INPUT_MODE_PS5;
case INPUT_MODE_NEOGEO:
case INPUT_MODE_NEOGEO:
return BootAction::SET_INPUT_MODE_NEOGEO;
case INPUT_MODE_MDMINI:
case INPUT_MODE_MDMINI:
return BootAction::SET_INPUT_MODE_MDMINI;
case INPUT_MODE_PCEMINI:
case INPUT_MODE_PCEMINI:
return BootAction::SET_INPUT_MODE_PCEMINI;
case INPUT_MODE_EGRET:
case INPUT_MODE_EGRET:
return BootAction::SET_INPUT_MODE_EGRET;
case INPUT_MODE_ASTRO:
case INPUT_MODE_ASTRO:
return BootAction::SET_INPUT_MODE_ASTRO;
case INPUT_MODE_PSCLASSIC:
case INPUT_MODE_PSCLASSIC:
return BootAction::SET_INPUT_MODE_PSCLASSIC;
case INPUT_MODE_XBOXORIGINAL:
case INPUT_MODE_XBOXORIGINAL:
return BootAction::SET_INPUT_MODE_XBOXORIGINAL;
case INPUT_MODE_XBONE:
return BootAction::SET_INPUT_MODE_XBONE;
case INPUT_MODE_SWITCH_PRO:
case INPUT_MODE_SWITCH_PRO:
return BootAction::SET_INPUT_MODE_SWITCH_PRO;
default:
return BootAction::NONE;
Expand Down Expand Up @@ -568,13 +580,13 @@ void GP2040::checkSaveRebootState() {
}

void GP2040::handleStorageSave(GPEvent* e) {
saveRequested = true;
forceSave = ((GPStorageSaveEvent*)e)->forceSave;
rebootRequested = ((GPStorageSaveEvent*)e)->restartAfterSave;
saveRequested = true;
forceSave = ((GPStorageSaveEvent*)e)->forceSave;
rebootRequested = ((GPStorageSaveEvent*)e)->restartAfterSave;
rebootMode = System::BootMode::DEFAULT;
}

void GP2040::handleSystemReboot(GPEvent* e) {
rebootRequested = true;
rebootRequested = true;
rebootMode = ((GPRestartEvent*)e)->bootMode;
}
2 changes: 1 addition & 1 deletion src/storagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool Storage::setProfile(const uint32_t profileNum)
// is this profile enabled?
// profile 1 (core) is always enabled, others we must check
if (profileNum == 1 || config.profileOptions.gpioMappingsSets[profileNum-2].enabled) {
EventManager::getInstance().triggerEvent(new GPProfileChangeEvent(this->config.gamepadOptions.profileNumber, profileNum));
// Update the profile number - reinit will be triggered automatically in gp2040.cpp
this->config.gamepadOptions.profileNumber = profileNum;
return true;
}
Expand Down