@@ -326,43 +326,46 @@ boolean XInputGamepad::connected() {
326326}
327327
328328// Send an update packet to the PC
329- size_t XInputGamepad::send () {
329+ int XInputGamepad::send () {
330330 if (!newData) return 0 ; // TX data hasn't changed
331331#ifdef USB_XINPUT
332- XInputUSB::send (tx, USB_Timeout);
333332 newData = false ;
333+ return XInputUSB::send (tx, sizeof (tx));
334334#else
335335 #warning "Using debug output for XInput send()"
336336 printDebug ();
337- #endif
338337 return sizeof (tx);
338+ #endif
339339}
340340
341- size_t XInputGamepad::receive () {
341+ int XInputGamepad::receive () {
342342#ifdef USB_XINPUT
343343 if (XInputUSB::available () == 0 ) {
344344 return 0 ; // No packet available
345345 }
346346
347347 // Grab packet and store it in rx array
348348 uint8_t rx[8 ];
349- size_t bytesRecv = XInputUSB::recv (rx, USB_Timeout);
350-
351- const uint8_t PacketType = rx[0 ];
352-
353- // Rumble Packet
354- if (PacketType == (uint8_t ) XInputReceiveType::Rumble) {
355- rumble[RumbleLeft.bufferIndex ] = rx[RumbleLeft.rxIndex ]; // Big weight (Left grip)
356- rumble[RumbleRight.bufferIndex ] = rx[RumbleRight.rxIndex ]; // Small weight (Right grip)
357- }
358- // LED Packet
359- else if (PacketType == (uint8_t ) XInputReceiveType::LEDs) {
360- parseLED (rx[2 ]);
361- }
362-
363- // User-defined receive callback
364- if (recvCallback != nullptr ) {
365- recvCallback (PacketType);
349+ const int bytesRecv = XInputUSB::recv (rx, sizeof (rx));
350+
351+ // Only process if received 3 or more bytes (min valid packet size)
352+ if (bytesRecv >= 3 ) {
353+ const uint8_t PacketType = rx[0 ];
354+
355+ // Rumble Packet
356+ if (PacketType == (uint8_t )XInputReceiveType::Rumble) {
357+ rumble[RumbleLeft.bufferIndex ] = rx[RumbleLeft.rxIndex ]; // Big weight (Left grip)
358+ rumble[RumbleRight.bufferIndex ] = rx[RumbleRight.rxIndex ]; // Small weight (Right grip)
359+ }
360+ // LED Packet
361+ else if (PacketType == (uint8_t )XInputReceiveType::LEDs) {
362+ parseLED (rx[2 ]);
363+ }
364+
365+ // User-defined receive callback
366+ if (recvCallback != nullptr ) {
367+ recvCallback (PacketType);
368+ }
366369 }
367370
368371 return bytesRecv;
0 commit comments