Skip to content

Commit d59fc17

Browse files
committed
Add reset function
For resetting the class to its initial values. Releases all set control values and clears any buffered data such as player and rumble.
1 parent 36c114a commit d59fc17

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ getLEDPatternID KEYWORD2
4444
send KEYWORD2
4545
receive KEYWORD2
4646

47+
# Other
48+
reset KEYWORD2
49+
4750
#######################################
4851
# Instances (KEYWORD2)
4952
#######################################

src/XInput.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ static const XInputMap_Rumble RumbleRight(4, 1); // Small motor
151151
XInputGamepad::XInputGamepad() :
152152
tx(), rumble() // Zero initialize arrays
153153
{
154-
tx[0] = 0x00; // Message type
155-
tx[1] = 0x14; // Packet size
154+
reset();
156155
}
157156

158157
void XInputGamepad::press(XInputControl button) {
@@ -284,4 +283,15 @@ void XInputGamepad::parseLED(uint8_t leds) {
284283
}
285284
}
286285

286+
// Resets class back to initial values
287+
void XInputGamepad::reset() {
288+
releaseAll(); // Clear TX buffer
289+
tx[0] = 0x00; // Set tx message type
290+
tx[1] = 0x14; // Set tx packet size (20)
291+
292+
player = 0; // Not connected, no player
293+
memset(rumble, 0x00, sizeof(rumble)); // Clear rumble values
294+
ledPattern = XInputLEDPattern::Off; // No LEDs on
295+
}
296+
287297
XInputGamepad XInput;

src/XInput.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ class XInputGamepad {
9999
// USB IO
100100
void send();
101101
void receive();
102-
102+
103+
// Other
104+
void reset();
105+
103106
private:
104107
static const uint32_t USB_Timeout = 12840; // Packet timeout, in milliseconds
105108
uint8_t tx[20]; // USB transmit data

0 commit comments

Comments
 (0)