From e63735b5467c4174b8fe054052bb8d2ce8051c51 Mon Sep 17 00:00:00 2001 From: TheTrain <32771064+TheTrainGoes@users.noreply.github.com> Date: Thu, 7 Sep 2023 23:23:37 -0400 Subject: [PATCH] Re-implementation of input history This is a re-implementation of the previous input history RP that was submitted by Thnikk (https://github.com/OpenStickCommunity/GP2040-CE/pull/53). Changes: - Removed all layout changes - Added PS4 mode This PR is not finished. It needs to be added into an addon and have a web-config component added to it. There is also currently no mapping for the HID Keyboard mode which casues issues. As a nice to have I am going to look into making a square and triangle for PS4 and PS3 mode. --- headers/addons/i2cdisplay.h | 5 ++ src/addons/i2cdisplay.cpp | 113 +++++++++++++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/headers/addons/i2cdisplay.h b/headers/addons/i2cdisplay.h index c7d5c6b02..601a91cac 100644 --- a/headers/addons/i2cdisplay.h +++ b/headers/addons/i2cdisplay.h @@ -7,6 +7,8 @@ #define DISPLAY_H_ #include +#include +#include #include #include "OneBitDisplay.h" #include "BoardConfig.h" @@ -150,6 +152,7 @@ class I2CDisplayAddon : public GPAddon void drawWasdBox(int startX, int startY, int buttonRadius, int buttonPadding); void drawArcadeStick(int startX, int startY, int buttonRadius, int buttonPadding); void drawStatusBar(Gamepad*); + void drawHistory(Gamepad*); void drawText(int startX, int startY, std::string text); void initMenu(char**); //Adding my stuff here, remember to sort before PR @@ -198,6 +201,8 @@ class I2CDisplayAddon : public GPAddon std::string statusBar; Gamepad* gamepad; Gamepad* pGamepad; + std::deque history; + std::array last; bool configMode; enum DisplayMode { diff --git a/src/addons/i2cdisplay.cpp b/src/addons/i2cdisplay.cpp index bdd6fc22f..58b9c40b6 100644 --- a/src/addons/i2cdisplay.cpp +++ b/src/addons/i2cdisplay.cpp @@ -221,7 +221,7 @@ void I2CDisplayAddon::process() { } break; } - + drawHistory(gamepad); obdDumpBuffer(&obd, NULL); } @@ -978,6 +978,117 @@ void I2CDisplayAddon::drawStatusBar(Gamepad * gamepad) drawText(0, 0, statusBar); } +void I2CDisplayAddon::drawHistory(Gamepad *gamepad) +{ + std::deque pressed; + + // Get key states + std::array current = { + pressedUp(), + pressedDown(), + pressedLeft(), + pressedRight(), + gamepad->pressedB1(), + gamepad->pressedB2(), + gamepad->pressedR2(), + gamepad->pressedL2(), + gamepad->pressedB3(), + gamepad->pressedB4(), + gamepad->pressedR1(), + gamepad->pressedL1(), + gamepad->pressedL3(), + gamepad->pressedS1(), + gamepad->pressedA1(), + gamepad->pressedS2(), + gamepad->pressedR3(), + }; + + // Key names shown on display + std::string displayNames[][17] = { + { // DInput + "U", "D", "L", "R", + "X", "O", "R2", "L2", + "#", "^", "R1", "L1", + "LS", "SL", "H", "ST", "RS" + }, + { // Switch + "U", "D", "L", "R", + "B", "A", "ZR", "ZL", + "Y", "X", "R", "L", + "LS", "-", "H", "+", "RS" + }, + { // XInput + "U", "D", "L", "R", + "A", "B", "RT", "LT", + "X", "Y", "RB", "LB", + "L3", "S1", "A1", "S2", "R3" + }, + { // PS4 + "U", "D", "L", "R", + "X", "O", "R2", "L2", + "#", "^", "R1", "L1", + "LS", "SL", "H", "ST", "RS" + } + }; + + uint8_t mode; + switch (gamepad->getOptions().inputMode) + { + case INPUT_MODE_HID: mode=0; break; + case INPUT_MODE_SWITCH: mode=1; break; + case INPUT_MODE_XINPUT: mode=2; break; + case INPUT_MODE_PS4: mode=3; break; + } + + // Check if any new keys have been pressed + if (last != current) { + // Iterate through array + for (uint8_t x=0; x<17; x++) { + // Add any pressed keys to deque + if (current[x]) pressed.push_back(displayNames[mode][x]); + } + // Update the last keypress array + last = current; + } + + if (pressed.size() > 0) { + std::string newInput; + for(const auto &s : pressed) { + if(!newInput.empty()) + newInput += "+"; + newInput += s; + } + + history.push_back(newInput); + } + + if (history.size() > 10) { + history.pop_front(); + } + + std::string ret; + + for (auto it = history.crbegin(); it != history.crend(); ++it) { + if (ret.size() < 22) { + std::string newRet = ret; + if (!newRet.empty()) + newRet = " " + newRet; + + newRet = *it + newRet; + + if (newRet.size() < 22) { + ret = newRet; + } + else { + break; + } + } + } + + // Draw history at bottom of display + obdWriteString(&obd, 0, 0, 7, (char *)ret.c_str(), FONT_6x8, 0, 0); +} + bool I2CDisplayAddon::pressedUp() { switch (gamepad->getOptions().dpadMode)