Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implementation of input history #510

Closed
wants to merge 1 commit into from
Closed
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
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