Skip to content

Commit b78d343

Browse files
committed
Add debug output function
Prints a formatted string of the control data to a Print object (Serial by default). This function is also called if the USB XInput output is not defined.
1 parent 5b64f3a commit b78d343

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ setRange KEYWORD2
5757

5858
# Other
5959
reset KEYWORD2
60+
printDebug KEYWORD2
6061

6162
#######################################
6263
# Instances (KEYWORD2)

src/XInput.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ void XInputGamepad::send() {
311311
if (!newData) return; // TX data hasn't changed
312312
XInputUSB.send(tx, USB_Timeout);
313313
newData = false;
314+
#else
315+
#warning "Using debug output for XInput send()"
316+
printDebug();
314317
#endif
315318
}
316319

@@ -419,4 +422,60 @@ void XInputGamepad::reset() {
419422
setJoystickRange(XInputMap_Joystick::range.min, XInputMap_Joystick::range.max);
420423
}
421424

425+
void XInputGamepad::printDebug(Print &output) const {
426+
const char fillCharacter = '_';
427+
428+
char buffer[80];
429+
430+
// Buttons
431+
const char dpadLPrint = getButton(DPAD_LEFT) ? '<' : fillCharacter;
432+
const char dpadUPrint = getButton(DPAD_UP) ? '^' : fillCharacter;
433+
const char dpadDPrint = getButton(DPAD_DOWN) ? 'v' : fillCharacter;
434+
const char dpadRPrint = getButton(DPAD_RIGHT) ? '>' : fillCharacter;
435+
436+
const char aButtonPrint = getButton(BUTTON_A) ? 'A' : fillCharacter;
437+
const char bButtonPrint = getButton(BUTTON_B) ? 'B' : fillCharacter;
438+
const char xButtonPrint = getButton(BUTTON_X) ? 'X' : fillCharacter;
439+
const char yButtonPrint = getButton(BUTTON_Y) ? 'Y' : fillCharacter;
440+
441+
const char startPrint = getButton(BUTTON_START) ? '>' : fillCharacter;
442+
const char backPrint = getButton(BUTTON_BACK) ? '<' : fillCharacter;
443+
444+
const char logoPrint = getButton(BUTTON_LOGO) ? 'X' : fillCharacter;
445+
446+
// Bumpers
447+
char leftBumper[3] = "LB";
448+
char rightBumper[3] = "RB";
449+
450+
if (!getButton(BUTTON_LB)) {
451+
leftBumper[0] = fillCharacter;
452+
leftBumper[1] = fillCharacter;
453+
}
454+
if (!getButton(BUTTON_RB)) {
455+
rightBumper[0] = fillCharacter;
456+
rightBumper[1] = fillCharacter;
457+
}
458+
459+
output.print("XInput Debug: ");
460+
sprintf(buffer,
461+
"LT: %3u %s L:(%6d, %6d) %c%c%c%c | %c%c%c | %c%c%c%c R:(%6d, %6d) %s RT: %3u",
462+
463+
// Left side controls
464+
getTrigger(TRIGGER_LEFT),
465+
leftBumper,
466+
getJoystickX(JOY_LEFT), getJoystickY(JOY_LEFT),
467+
468+
// Buttons
469+
dpadLPrint, dpadUPrint, dpadDPrint, dpadRPrint,
470+
backPrint, logoPrint, startPrint,
471+
aButtonPrint, bButtonPrint, xButtonPrint, yButtonPrint,
472+
473+
// Right side controls
474+
getJoystickX(JOY_RIGHT), getJoystickY(JOY_RIGHT),
475+
rightBumper,
476+
getTrigger(TRIGGER_RIGHT)
477+
);
478+
output.println(buffer);
479+
}
480+
422481
XInputGamepad XInput;

src/XInput.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ class XInputGamepad {
117117
// Setup
118118
void reset();
119119

120+
// Debug
121+
void printDebug(Print& output = Serial) const;
122+
120123
private:
121124
static const uint32_t USB_Timeout = 12840; // Packet timeout, in milliseconds
122125
uint8_t tx[20]; // USB transmit data

0 commit comments

Comments
 (0)