Skip to content

Commit aabcd95

Browse files
committed
Add auto-send functionality
When enabled, will automatically call the 'send' function when you successfully change any control surface. Allows you to either update all controls in a packet (disabled) or just update each control as they're changed (enabled).
1 parent 4606f10 commit aabcd95

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ setJoystick KEYWORD2
3232

3333
releaseAll KEYWORD2
3434

35+
setAutoSend KEYWORD2
36+
3537
# Read Control Data
3638
getButton KEYWORD2
3739
getDpad KEYWORD2

src/XInput.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ void XInputGamepad::setButton(XInputControl button, boolean state) {
219219
if (state) { tx[buttonData->index] |= buttonData->mask; } // Press
220220
else { tx[buttonData->index] &= ~(buttonData->mask); } // Release
221221
newData = true;
222+
autosend();
222223
}
223224
else {
224225
Range * triggerRange = getRangeFromEnum(button);
@@ -236,10 +237,16 @@ void XInputGamepad::setDpad(boolean up, boolean down, boolean left, boolean righ
236237
if (up && down) { down = false; } // Up + Down = Up
237238
if (left && right) { left = false; right = false; } // Left + Right = Neutral
238239

240+
const boolean autoSendTemp = autoSendOption; // Save autosend state
241+
autoSendOption = false; // Disable temporarily
242+
239243
setDpad(DPAD_UP, up);
240244
setDpad(DPAD_DOWN, down);
241245
setDpad(DPAD_LEFT, left);
242246
setDpad(DPAD_RIGHT, right);
247+
248+
autoSendOption = autoSendTemp; // Re-enable from option
249+
autosend();
243250
}
244251

245252
void XInputGamepad::setTrigger(XInputControl trigger, int32_t val) {
@@ -251,6 +258,7 @@ void XInputGamepad::setTrigger(XInputControl trigger, int32_t val) {
251258

252259
tx[triggerData->index] = val;
253260
newData = true;
261+
autosend();
254262
}
255263

256264
void XInputGamepad::setJoystick(XInputControl joy, int32_t x, int32_t y) {
@@ -269,12 +277,18 @@ void XInputGamepad::setJoystick(XInputControl joy, int32_t x, int32_t y) {
269277
tx[joyData->y_high] = highByte(y);
270278

271279
newData = true;
280+
autosend();
272281
}
273282

274283
void XInputGamepad::releaseAll() {
275284
const uint8_t offset = 2; // Skip message type and packet size
276285
memset(tx + offset, 0x00, sizeof(tx) - offset); // Clear TX array
277286
newData = true; // Data changed and is unsent
287+
autosend();
288+
}
289+
290+
void XInputGamepad::setAutoSend(boolean a) {
291+
autoSendOption = a;
278292
}
279293

280294
boolean XInputGamepad::getButton(XInputControl button) const {
@@ -467,8 +481,9 @@ void XInputGamepad::reset() {
467481
setTriggerRange(XInputMap_Trigger::range.min, XInputMap_Trigger::range.max);
468482
setJoystickRange(XInputMap_Joystick::range.min, XInputMap_Joystick::range.max);
469483

470-
// Clear user-set receive callback
484+
// Clear user-set options
471485
recvCallback = nullptr;
486+
autoSendOption = true;
472487
}
473488

474489
void XInputGamepad::printDebug(Print &output) const {

src/XInput.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class XInputGamepad {
9292

9393
void releaseAll();
9494

95+
// Auto-Send Data
96+
void setAutoSend(boolean a);
97+
9598
// Read Control Surfaces
9699
boolean getButton(XInputControl button) const;
97100
boolean getDpad(XInputControl dpad) const;
@@ -134,6 +137,11 @@ class XInputGamepad {
134137
// Sent Data
135138
uint8_t tx[20]; // USB transmit data
136139
boolean newData; // Flag for tx data changed
140+
boolean autoSendOption; // Flag for automatically sending data
141+
142+
void inline autosend() {
143+
if (autoSendOption) { send(); }
144+
}
137145

138146
// Received Data
139147
volatile uint8_t player; // Gamepad player #, buffered

0 commit comments

Comments
 (0)