| 451 | } |
| 452 | |
| 453 | int XInputController::receive() { |
| 454 | #ifdef USB_XINPUT |
| 455 | if (XInputUSB::available() == 0) { |
| 456 | return 0; // No packet available |
| 457 | } |
| 458 | |
| 459 | // Grab packet and store it in rx array |
| 460 | uint8_t rx[8]; |
| 461 | const int bytesRecv = XInputUSB::recv(rx, sizeof(rx)); |
| 462 | |
| 463 | // Only process if received 3 or more bytes (min valid packet size) |
| 464 | if (bytesRecv >= 3) { |
| 465 | const uint8_t PacketType = rx[0]; |
| 466 | |
| 467 | // Rumble Packet |
| 468 | if (PacketType == (uint8_t)XInputReceiveType::Rumble) { |
| 469 | rumble[RumbleLeft.bufferIndex] = rx[RumbleLeft.rxIndex]; // Big weight (Left grip) |
| 470 | rumble[RumbleRight.bufferIndex] = rx[RumbleRight.rxIndex]; // Small weight (Right grip) |
| 471 | } |
| 472 | // LED Packet |
| 473 | else if (PacketType == (uint8_t)XInputReceiveType::LEDs) { |
| 474 | parseLED(rx[2]); |
| 475 | } |
| 476 | |
| 477 | // User-defined receive callback |
| 478 | if (recvCallback != nullptr) { |
| 479 | recvCallback(PacketType); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return bytesRecv; |
| 484 | #else |
| 485 | return 0; |
| 486 | #endif |
| 487 | } |
| 488 | |
| 489 | void XInputController::parseLED(uint8_t leds) { |
| 490 | if (leds > 0x0D) return; // Not a known pattern |
no outgoing calls
no test coverage detected