Skip to content

Commit 296da4c

Browse files
authored
Merge pull request #9 from dmadison/autosend
Autosend
2 parents 4606f10 + d2b0d5a commit 296da4c

File tree

8 files changed

+31
-5
lines changed

8 files changed

+31
-5
lines changed

examples/Blink/Blink.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ void setup() {
3636

3737
void loop() {
3838
XInput.press(BUTTON_A);
39-
XInput.send();
4039
delay(1000);
4140

4241
XInput.release(BUTTON_A);
43-
XInput.send();
4442
delay(1000);
4543
}

examples/GamepadPins/GamepadPins.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ void setup() {
116116

117117
// Set joystick range to the ADC max
118118
XInput.setJoystickRange(0, ADC_Max);
119+
120+
XInput.setAutoSend(false); // Wait for all controls before sending
119121
}
120122

121123
void loop() {

examples/ReceiveCallback/ReceiveCallback.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ void loop() {
5555
else {
5656
XInput.release(TRIGGER_RIGHT);
5757
}
58-
59-
XInput.send();
6058
}
6159

6260
void rumbleCallback(uint8_t packetType) {

examples/SimulateAll/SimulateAll.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ double angle = 0.0;
7474

7575
void setup() {
7676
pinMode(SafetyPin, INPUT_PULLUP);
77+
XInput.setAutoSend(false); // Wait for all controls before sending
7778
}
7879

7980
void loop() {

examples/WiiClassicController/WiiClassicController.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void setup() {
4141
XInput.setRange(JOY_LEFT, 8, 56);
4242
XInput.setRange(JOY_RIGHT, 4, 28);
4343

44+
XInput.setAutoSend(false); // Wait for all controls before sending
45+
4446
while (!classic.connect()) {
4547
delay(1000); // Controller not connected
4648
}

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)