Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #129 from arntsonl/addonrework
Browse files Browse the repository at this point in the history
Addon Rework - Per-Core Addons with Manager
  • Loading branch information
arntsonl authored Oct 23, 2022
2 parents 6242a9e + d5a395f commit 5fcda07
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 68 deletions.
31 changes: 31 additions & 0 deletions include/addonmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _ADDONMANAGER_H_
#define _ADDONMANAGER_H_

#include "gpaddon.h"

#include <vector>
#include <pico/mutex.h>

typedef enum ADDON_PROCESS {
CORE0_INPUT,
CORE1_LOOP
} _ADDON_PROCESS;

typedef struct AddonBlock {
GPAddon * ptr;
ADDON_PROCESS process;
bool enabled;
};

class AddonManager {
public:
AddonManager() {}
~AddonManager() {}
void LoadAddon(GPAddon*, ADDON_PROCESS, bool enabled=true);
void ProcessAddons(ADDON_PROCESS);
GPAddon * GetAddon(std::string); // hack for NeoPicoLED
private:
std::vector<AddonBlock*> addons; // addons currently loaded
};

#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions include/gp2040.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// GP2040 Classes
#include "gamepad.h"
#include "gpaddon.h"
#include "addonmanager.h"

class GP2040 {
public:
Expand All @@ -17,9 +17,9 @@ class GP2040 {
void setup(); // setup core0
void run(); // loop core0
private:
void setupInput(GPAddon*);
uint64_t nextRuntime;
Gamepad snapshot;
AddonManager addons;
};

#endif
8 changes: 4 additions & 4 deletions include/gp2040aux.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

#include <vector>

#include "addons/i2cdisplay.h"
#include "addons/neopicoleds.h"
#include "addons/playerleds.h"
#include "gpaddon.h"
#include "addonmanager.h"

class GP2040Aux {
public:
Expand All @@ -19,7 +18,8 @@ class GP2040Aux {
void setup(); // setup core1
void run(); // loop core1
private:
void setupAddon(GPAddon*);
uint64_t nextRuntime;
AddonManager addons;
};

#endif
4 changes: 0 additions & 4 deletions include/storagemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "enums.h"
#include "helper.h"
#include "gamepad.h"
#include "gpaddon.h"

#define GAMEPAD_STORAGE_INDEX 0 // 1024 bytes for gamepad options
#define BOARD_STORAGE_INDEX 1024 // 512 bytes for hardware options
Expand Down Expand Up @@ -139,9 +138,6 @@ class Storage {
uint8_t * GetFeatureData();

void ResetSettings(); // EEPROM Reset Feature

std::vector<GPAddon*> Addons; // Modular Features
std::vector<GPAddon*> Inputs;

private:
Storage() : gamepad(0) {
Expand Down
30 changes: 30 additions & 0 deletions src/addonmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "addonmanager.h"

void AddonManager::LoadAddon(GPAddon* addon, ADDON_PROCESS processAt, bool enabled) {
if (addon->available()) {
AddonBlock * block = new AddonBlock;
addon->setup();
block->ptr = addon;
block->process = processAt;
block->enabled = enabled;
addons.push_back(block);
} else {
delete addon; // Don't use the memory if we don't have to
}
}

void AddonManager::ProcessAddons(ADDON_PROCESS processType) {
// Loop through all addons and process any that match our type
for (std::vector<AddonBlock*>::iterator it = addons.begin(); it != addons.end(); it++) {
if ( (*it)->process == processType && (*it)->enabled == true)
(*it)->ptr->process();
}
}

// HACK : change this for NeoPicoLED
GPAddon * AddonManager::GetAddon(std::string name) { // hack for NeoPicoLED
for (std::vector<AddonBlock*>::iterator it = addons.begin(); it != addons.end(); it++) {
if ( (*it)->ptr->name() == name )
return (*it)->ptr;
}
}
2 changes: 1 addition & 1 deletion src/inputs/analog.cpp → src/addons/analog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "inputs/analog.h"
#include "addons/analog.h"
#include "storagemanager.h"

#include "hardware/adc.h"
Expand Down
4 changes: 2 additions & 2 deletions src/inputs/i2canalog1219.cpp → src/addons/i2canalog1219.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "inputs/i2canalog1219.h"
#include "addons/i2canalog1219.h"
#include "storagemanager.h"

#define ADS_MAX (float)((1 << 23) - 1)
#define VREF_VOLTAGE 2.048f

bool I2CAnalog1219Input::available() {
BoardOptions boardOptions = Storage::getInstance().getBoardOptions();
return (boardOptions.i2cAnalog1219SDAPin != -1 ||
return (boardOptions.i2cAnalog1219SDAPin != -1 &&
boardOptions.i2cAnalog1219SCLPin != -1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/inputs/jslider.cpp → src/addons/jslider.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "inputs/jslider.h"
#include "addons/jslider.h"

#include "storagemanager.h"

Expand Down
2 changes: 1 addition & 1 deletion src/inputs/reverse.cpp → src/addons/reverse.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "inputs/reverse.h"
#include "addons/reverse.h"
#include "storagemanager.h"
#include "GamepadEnums.h"

Expand Down
2 changes: 1 addition & 1 deletion src/inputs/turbo.cpp → src/addons/turbo.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "inputs/turbo.h"
#include "addons/turbo.h"

#include "storagemanager.h"

Expand Down
11 changes: 2 additions & 9 deletions src/configmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "configmanager.h"

#include "addonmanager.h"
#include "configs/webconfig.h"
#include "addons/neopicoleds.h"

Expand Down Expand Up @@ -26,15 +28,6 @@ void ConfigManager::setGamepadOptions(Gamepad* gamepad) {

void ConfigManager::setLedOptions(LEDOptions ledOptions) {
Storage::getInstance().setLEDOptions(ledOptions);

// Configure LEDs in the neopico
for (std::vector<GPAddon*>::iterator it = Storage::getInstance().Addons.begin(); it != Storage::getInstance().Addons.end(); it++) {
if ((*it)->name() == NeoPicoLEDName) {
NeoPicoLEDAddon * neopicoled = (NeoPicoLEDAddon*)(*it);
neopicoled->configureLEDs();
break; // trigger LED configuration
}
}
}

void ConfigManager::setBoardOptions(BoardOptions boardOptions) {
Expand Down
43 changes: 17 additions & 26 deletions src/gp2040.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// GP2040 includes
#include "gp2040.h"
#include "helper.h"
#include "configmanager.h" // Managers

#include "configmanager.h" // Global Managers
#include "storagemanager.h"
#include "addonmanager.h"

#include "inputs/analog.h" // Inputs
#include "inputs/i2canalog1219.h"
#include "inputs/jslider.h"
#include "inputs/reverse.h"
#include "inputs/turbo.h"
#include "addons/analog.h" // Inputs for Core0
#include "addons/i2canalog1219.h"
#include "addons/jslider.h"
#include "addons/reverse.h"
#include "addons/turbo.h"

// Pico includes
#include "pico/bootrom.h"
Expand All @@ -27,7 +29,7 @@ GP2040::GP2040() : nextRuntime(0) {
GP2040::~GP2040() {
}

void GP2040::setup() {
void GP2040::setup() {
// Setup Gamepad and Gamepad Storage
Gamepad * gamepad = Storage::getInstance().GetGamepad();
gamepad->setup();
Expand Down Expand Up @@ -57,13 +59,12 @@ void GP2040::setup() {
initialize_driver(inputMode);
}

// Setup Add-on Inputs
setupInput(new AnalogInput());
setupInput(new I2CAnalog1219Input());
setupInput(new JSliderInput());
setupInput(new ReverseInput());
setupInput(new TurboInput());

// Setup Add-ons
addons.LoadAddon(new AnalogInput(), CORE0_INPUT);
addons.LoadAddon(new I2CAnalog1219Input(), CORE0_INPUT);
addons.LoadAddon(new JSliderInput(), CORE0_INPUT);
addons.LoadAddon(new ReverseInput(), CORE0_INPUT);
addons.LoadAddon(new TurboInput(), CORE0_INPUT);
}

void GP2040::run() {
Expand All @@ -90,12 +91,9 @@ void GP2040::run() {
gamepad->hotkey(); // check for MPGS hotkeys
gamepad->process(); // process through MPGS

// Loop through all input modifiers/features (Analog Sticks, Turbo Buttons, Macro Inputs, Touch Screens, etc.)
for (std::vector<GPAddon*>::iterator it = Storage::getInstance().Inputs.begin(); it != Storage::getInstance().Inputs.end(); it++) {
(*it)->process();
}
addons.ProcessAddons(ADDON_PROCESS::CORE0_INPUT);

// Copy Processed Gamepad
// Copy Processed Gamepad for Core1 (race condition otherwise)
memcpy(&processedGamepad->state, &gamepad->state, sizeof(GamepadState));

// USB FEATURES : Send/Get USB Features (including Player LEDs on X-Input)
Expand All @@ -107,10 +105,3 @@ void GP2040::run() {
nextRuntime = getMicro() + GAMEPAD_POLL_MICRO;
}
}

void GP2040::setupInput(GPAddon* input) {
if (input->available()) {
input->setup();
Storage::getInstance().Inputs.push_back(input);
}
}
27 changes: 13 additions & 14 deletions src/gp2040aux.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
// GP2040 includes
#include "gp2040aux.h"
#include "gamepad.h"
#include "storagemanager.h" // Managers

#include "storagemanager.h" // Global Managers
#include "addonmanager.h"

#include "addons/i2cdisplay.h" // Add-Ons
#include "addons/neopicoleds.h"
#include "addons/playerleds.h"

#include <iterator>

GP2040Aux::GP2040Aux() {
GP2040Aux::GP2040Aux() : nextRuntime(0) {
}

GP2040Aux::~GP2040Aux() {
}

void GP2040Aux::setup() {
setupAddon(new I2CDisplayAddon());
setupAddon(new NeoPicoLEDAddon());
setupAddon(new PlayerLEDAddon());
addons.LoadAddon(new I2CDisplayAddon(), CORE1_LOOP);
addons.LoadAddon(new NeoPicoLEDAddon(), CORE1_LOOP);
addons.LoadAddon(new PlayerLEDAddon(), CORE1_LOOP);
}

void GP2040Aux::run() {
while (1) {
for (std::vector<GPAddon*>::iterator it = Storage::getInstance().Addons.begin(); it != Storage::getInstance().Addons.end(); it++) {
(*it)->process();
if (nextRuntime > getMicro()) { // fix for unsigned
sleep_us(50); // Give some time back to our CPU (lower power consumption)
continue;
}
}
}

void GP2040Aux::setupAddon(GPAddon* addon) {
if (addon->available()) {
addon->setup();
Storage::getInstance().Addons.push_back(addon);
addons.ProcessAddons(CORE1_LOOP);
nextRuntime = getMicro() + GAMEPAD_POLL_MICRO;
}
}
5 changes: 2 additions & 3 deletions src/storagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
#include "addons/i2cdisplay.h"
#include "addons/neopicoleds.h"
#include "addons/playerleds.h"

#include "inputs/i2canalog1219.h"
#include "inputs/turbo.h"
#include "addons/i2canalog1219.h"
#include "addons/turbo.h"

#include "helper.h"

Expand Down

0 comments on commit 5fcda07

Please sign in to comment.