Skip to content

Commit

Permalink
Re-implementation of input history
Browse files Browse the repository at this point in the history
This is a re-implementation of the previous input history RP that was submitted by Thnikk (#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.
  • Loading branch information
TheTrainGoes committed Sep 8, 2023
1 parent 15bb465 commit e63735b
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
5 changes: 5 additions & 0 deletions headers/addons/i2cdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define DISPLAY_H_

#include <string>
#include <deque>
#include <array>
#include <hardware/i2c.h>
#include "OneBitDisplay.h"
#include "BoardConfig.h"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -198,6 +201,8 @@ class I2CDisplayAddon : public GPAddon
std::string statusBar;
Gamepad* gamepad;
Gamepad* pGamepad;
std::deque<std::string> history;
std::array<bool, 17> last;
bool configMode;

enum DisplayMode {
Expand Down
113 changes: 112 additions & 1 deletion src/addons/i2cdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void I2CDisplayAddon::process() {
}
break;
}

drawHistory(gamepad);
obdDumpBuffer(&obd, NULL);
}

Expand Down Expand Up @@ -978,6 +978,117 @@ void I2CDisplayAddon::drawStatusBar(Gamepad * gamepad)
drawText(0, 0, statusBar);
}

void I2CDisplayAddon::drawHistory(Gamepad *gamepad)
{
std::deque<std::string> pressed;

// Get key states
std::array<bool, 17> 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)
Expand Down

0 comments on commit e63735b

Please sign in to comment.