Skip to content

Commit

Permalink
Event System Implementation (#1247)
Browse files Browse the repository at this point in the history
* Event System Implementation
Re-implemented event system from previous PR #971 to be up to date with current codebase.

* Moved profile change event to setProfile

* Fixed stray semicolon in adding restart event handler.

* Added semicolon to event macro to keep event handlers clean

* Removed semicolons from remaining event handler registrations
  • Loading branch information
mikepparks authored Jan 3, 2025
1 parent 00cd79a commit da0f83b
Show file tree
Hide file tree
Showing 26 changed files with 582 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ src/display/ui/screens/ButtonLayoutScreen.cpp
src/display/ui/screens/ConfigScreen.cpp
src/display/ui/screens/MainMenuScreen.cpp
src/display/ui/screens/PinViewerScreen.cpp
src/display/ui/screens/RestartScreen.cpp
src/display/ui/screens/StatsScreen.cpp
src/display/ui/screens/SplashScreen.cpp
src/display/GPGFX.cpp
src/display/GPGFX_UI.cpp
src/drivermanager.cpp
src/eventmanager.cpp
src/layoutmanager.cpp
src/peripheralmanager.cpp
src/storagemanager.cpp
Expand Down Expand Up @@ -271,6 +273,7 @@ headers
headers/addons
headers/configs
headers/drivers
headers/events
headers/interfaces
headers/interfaces/i2c
headers/interfaces/i2c/ads1219
Expand Down
3 changes: 3 additions & 0 deletions headers/addons/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class DisplayAddon : public GPAddon
virtual void preprocess() {}
virtual void process();
virtual std::string name() { return DisplayName; }

void handleSystemRestart(GPEvent* e);
private:
bool updateDisplayScreen();
void drawStatusBar(Gamepad*);
Expand All @@ -209,6 +211,7 @@ class DisplayAddon : public GPAddon
DisplayMode currDisplayMode;
DisplayMode prevDisplayMode;
bool turnOffWhenSuspended;
uint32_t bootMode;

GPGFX_DisplayTypeOptions gpOptions;
};
Expand Down
4 changes: 4 additions & 0 deletions headers/addons/turbo.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "gpaddon.h"
#include "storagemanager.h"
#include "eventmanager.h"
#include "enums.pb.h"

#ifndef TURBO_ENABLED
Expand Down Expand Up @@ -106,6 +107,8 @@ class TurboInput : public GPAddon {
virtual void preprocess() {}
virtual void process(); // TURBO Setting of buttons (Enable/Disable)
virtual std::string name() { return TurboName; }

void handleEncoder(GPEvent* e);
private:
void updateInterval(uint8_t shotCount);
void updateTurboShotCount(uint8_t turboShotCount);
Expand All @@ -132,5 +135,6 @@ class TurboInput : public GPAddon {
uint16_t shmupBtnMask[4]; // Turbo SHMUP Non-Turbo Button Masks
uint16_t lastButtons; // Last buttons (for Turbo Reset on Release)
bool hasLedPin; // Flag for LED pin presence
uint8_t encoderValue; // Rotary encoder value
};
#endif // TURBO_H_
1 change: 1 addition & 0 deletions headers/display/GPGFX_UI.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "config.pb.h"
#include "GamepadState.h"
#include "storagemanager.h"
#include "eventmanager.h"

class GPGFX_UI {
public:
Expand Down
4 changes: 3 additions & 1 deletion headers/display/GPGFX_UI_screens.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ enum DisplayMode {
SPLASH,
PIN_VIEWER,
STATS,
MAIN_MENU
MAIN_MENU,
RESTART
};

#include "ui/screens/ButtonLayoutScreen.h"
#include "ui/screens/ConfigScreen.h"
#include "ui/screens/MainMenuScreen.h"
#include "ui/screens/PinViewerScreen.h"
#include "ui/screens/RestartScreen.h"
#include "ui/screens/SplashScreen.h"
#include "ui/screens/StatsScreen.h"

Expand Down
11 changes: 8 additions & 3 deletions headers/display/ui/screens/ButtonLayoutScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class ButtonLayoutScreen : public GPScreen {
virtual int8_t update();
virtual void init();
virtual void shutdown();

void handleProfileChange(GPEvent* e);
void handleUSB(GPEvent* e);
protected:
virtual void drawScreen();
private:
Expand Down Expand Up @@ -144,12 +147,14 @@ class ButtonLayoutScreen : public GPScreen {
std::deque<std::string> inputHistory;
std::array<bool, INPUT_HISTORY_MAX_INPUTS> lastInput;

bool profileModeDisplay;
uint8_t profileDelay = 2;
int profileDelayStart = 0;
bool bannerDisplay;
uint8_t bannerDelay = 2;
int bannerDelayStart = 0;
std::string bannerMessage;
uint16_t prevButtonState = 0;
uint8_t prevLayoutLeft = 0;
uint8_t prevLayoutRight = 0;
uint8_t profileNumber = 0;
uint8_t prevProfileNumber = 0;
ButtonLayoutParamsLeft prevLeftOptions;
ButtonLayoutParamsRight prevRightOptions;
Expand Down
21 changes: 21 additions & 0 deletions headers/display/ui/screens/RestartScreen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _RESTARTSCREEN_H_
#define _RESTARTSCREEN_H_

#include "GPGFX_UI_widgets.h"
#include "bitmaps.h"

class RestartScreen : public GPScreen {
public:
RestartScreen() {}
RestartScreen(GPGFX* renderer) { setRenderer(renderer); }
virtual int8_t update();
virtual void init();
virtual void shutdown();

void setBootMode(uint32_t mode);
protected:
virtual void drawScreen();
uint32_t bootMode;
};

#endif
44 changes: 44 additions & 0 deletions headers/eventmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef _EVENTMANAGER_H_
#define _EVENTMANAGER_H_

#include <map>
#include <vector>
#include <string>
#include <deque>
#include <array>
#include <functional>
#include <cctype>
#include "config.pb.h"
#include "enums.pb.h"

#include "GPEvent.h"
#include "GPGamepadEvent.h"
#include "GPEncoderEvent.h"
#include "GPProfileEvent.h"
#include "GPRestartEvent.h"
#include "GPUSBHostEvent.h"

#define EVENTMGR EventManager::getInstance()

class EventManager {
public:
typedef std::function<void(GPEvent* event)> EventFunction;
typedef std::pair<GPEventType, std::vector<EventFunction>> EventEntry;

EventManager(EventManager const&) = delete;
void operator=(EventManager const&) = delete;
static EventManager& getInstance() // Thread-safe storage ensures cross-thread talk
{
static EventManager instance;
return instance;
}

void registerEventHandler(GPEventType eventType, EventFunction handler);
void triggerEvent(GPEvent* event);
private:
EventManager(){}

std::vector<std::pair<GPEventType, std::vector<EventFunction>>> eventList;
};

#endif
21 changes: 21 additions & 0 deletions headers/events/GPEncoderEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _GPENCODEREVENT_H_
#define _GPENCODEREVENT_H_

class GPEncoderChangeEvent : public GPEvent {
public:
GPEncoderChangeEvent() {}
GPEncoderChangeEvent(uint8_t id, int8_t dir) {
this->encoder = id;
this->direction = dir;
}
~GPEncoderChangeEvent() {}

uint8_t encoder = 0;
int8_t direction = 0;

GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_ENCODER_CHANGE;
};

#endif
16 changes: 16 additions & 0 deletions headers/events/GPEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _GPEVENT_H_
#define _GPEVENT_H_

#define GPEVENT_CALLBACK(x) ([this](GPEvent* event){x;})

class GPEvent {
public:
GPEvent() {}
~GPEvent() {}

virtual GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_BASE;
};

#endif
130 changes: 130 additions & 0 deletions headers/events/GPGamepadEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#ifndef _GPGAMEPADEVENT_H_
#define _GPGAMEPADEVENT_H_

#include "gamepad.h"

class GPGamepadEvent : public GPEvent {
public:
GPGamepadEvent() {}
GPGamepadEvent(GamepadState currState) { this->state = currState; }
GPGamepadEvent(uint8_t dpad, uint16_t buttons, uint16_t aux) {
this->state.dpad = dpad;
this->state.buttons = buttons;
this->state.aux = aux;
}
GPGamepadEvent(uint16_t lx, uint16_t ly, uint16_t rx, uint16_t ry, uint8_t lt, uint8_t rt) {
this->state.lx = lx;
this->state.ly = ly;
this->state.rx = rx;
this->state.ry = ry;
this->state.lt = lt;
this->state.rt = rt;
}
GPGamepadEvent(uint8_t dpad, uint16_t buttons, uint16_t aux, uint16_t lx, uint16_t ly, uint16_t rx, uint16_t ry, uint8_t lt, uint8_t rt) {
this->state.dpad = dpad;
this->state.buttons = buttons;
this->state.aux = aux;
this->state.lx = lx;
this->state.ly = ly;
this->state.rx = rx;
this->state.ry = ry;
this->state.lt = lt;
this->state.rt = rt;
}
~GPGamepadEvent() {}

GamepadState state;
private:
};

class GPButtonEvent : public GPGamepadEvent {
public:
GPButtonEvent() {}
GPButtonEvent(GamepadState currState) : GPGamepadEvent(currState) {}
GPButtonEvent(uint8_t dpad, uint16_t buttons, uint16_t aux) : GPGamepadEvent(dpad, buttons, aux) {}
~GPButtonEvent() {}
private:
};

class GPButtonUpEvent : public GPButtonEvent {
public:
GPButtonUpEvent() {}
GPButtonUpEvent(GamepadState currState) : GPButtonEvent(currState) {}
GPButtonUpEvent(uint8_t dpad, uint16_t buttons, uint16_t aux) : GPButtonEvent(dpad, buttons, aux) {}
~GPButtonUpEvent() {}

GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_BUTTON_UP;
};

class GPButtonDownEvent : public GPButtonEvent {
public:
GPButtonDownEvent() {}
GPButtonDownEvent(GamepadState currState) : GPButtonEvent(currState) {}
GPButtonDownEvent(uint8_t dpad, uint16_t buttons, uint16_t aux) : GPButtonEvent(dpad, buttons, aux) {}
~GPButtonDownEvent() {}

GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_BUTTON_DOWN;
};

class GPButtonProcessedUpEvent : public GPButtonEvent {
public:
GPButtonProcessedUpEvent() {}
GPButtonProcessedUpEvent(GamepadState currState) : GPButtonEvent(currState) {}
GPButtonProcessedUpEvent(uint8_t dpad, uint16_t buttons, uint16_t aux) : GPButtonEvent(dpad, buttons, aux) {}
~GPButtonProcessedUpEvent() {}

GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_BUTTON_PROCESSED_UP;
};

class GPButtonProcessedDownEvent : public GPButtonEvent {
public:
GPButtonProcessedDownEvent() {}
GPButtonProcessedDownEvent(GamepadState currState) : GPButtonEvent(currState) {}
GPButtonProcessedDownEvent(uint8_t dpad, uint16_t buttons, uint16_t aux) : GPButtonEvent(dpad, buttons, aux) {}
~GPButtonProcessedDownEvent() {}

GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_BUTTON_PROCESSED_DOWN;
};

class GPAnalogEvent : public GPGamepadEvent {
public:
GPAnalogEvent() {}
GPAnalogEvent(GamepadState currState) : GPGamepadEvent(currState) {}
GPAnalogEvent(uint16_t lx, uint16_t ly, uint16_t rx, uint16_t ry, uint8_t lt, uint8_t rt) : GPGamepadEvent(lx, ly, rx, ry, lt, rt) {}
~GPAnalogEvent() {}
private:
};

class GPAnalogMoveEvent : public GPAnalogEvent {
public:
GPAnalogMoveEvent() {}
GPAnalogMoveEvent(GamepadState currState) : GPAnalogEvent(currState) {}
GPAnalogMoveEvent(uint16_t lx, uint16_t ly, uint16_t rx, uint16_t ry, uint8_t lt, uint8_t rt) : GPAnalogEvent(lx, ly, rx, ry, lt, rt) {}
~GPAnalogMoveEvent() {}

GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_ANALOG_MOVE;
};

class GPAnalogProcessedMoveEvent : public GPAnalogEvent {
public:
GPAnalogProcessedMoveEvent() {}
GPAnalogProcessedMoveEvent(GamepadState currState) : GPAnalogEvent(currState) {}
GPAnalogProcessedMoveEvent(uint16_t lx, uint16_t ly, uint16_t rx, uint16_t ry, uint8_t lt, uint8_t rt) : GPAnalogEvent(lx, ly, rx, ry, lt, rt) {}
~GPAnalogProcessedMoveEvent() {}

GPEventType eventType() { return this->_eventType; }
private:
GPEventType _eventType = GP_EVENT_ANALOG_PROCESSED_MOVE;
};

#endif
21 changes: 21 additions & 0 deletions headers/events/GPProfileEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _GPPROFILEEVENT_H_
#define _GPPROFILEEVENT_H_

class GPProfileChangeEvent : public GPEvent {
public:
GPProfileChangeEvent() {}
GPProfileChangeEvent(uint8_t prev, uint8_t curr) {
this->previousValue = prev;
this->currentValue = curr;
}
~GPProfileChangeEvent() {}

GPEventType eventType() { return this->_eventType; }

uint8_t previousValue;
uint8_t currentValue;
private:
GPEventType _eventType = GP_EVENT_PROFILE_CHANGE;
};

#endif
Loading

0 comments on commit da0f83b

Please sign in to comment.