diff --git a/PS4Parser.cpp b/PS4Parser.cpp index 1a449adb..d8157f43 100644 --- a/PS4Parser.cpp +++ b/PS4Parser.cpp @@ -36,89 +36,89 @@ bool PS4Parser::checkDpad(ButtonEnum b) { } bool PS4Parser::getButtonPress(ButtonEnum b) { - if (b <= LEFT) // Dpad - return checkDpad(b); - else { - uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]); - uint8_t index = button < 8 ? 0 : button < 16 ? 1 : 2; - uint8_t mask = 1 << (button - 8 * index); - return ps4Data.btn.val[index] & mask; - } + if (b <= LEFT) // Dpad + return checkDpad(b); + else { + uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]); + uint8_t index = button < 8 ? 0 : button < 16 ? 1 : 2; + uint8_t mask = 1 << (button - 8 * index); + return ps4Data.btn.val[index] & mask; + } } bool PS4Parser::getButtonClick(ButtonEnum b) { - uint8_t mask, index = 0; - if (b <= LEFT) // Dpad - mask = 1 << b; - else { - uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]); - index = button < 8 ? 0 : button < 16 ? 1 : 2; - mask = 1 << (button - 8 * index); - } + uint8_t mask, index = 0; + if (b <= LEFT) // Dpad + mask = 1 << b; + else { + uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]); + index = button < 8 ? 0 : button < 16 ? 1 : 2; + mask = 1 << (button - 8 * index); + } - bool click = buttonClickState.val[index] & mask; - buttonClickState.val[index] &= ~mask; // Clear "click" event - return click; + bool click = buttonClickState.val[index] & mask; + buttonClickState.val[index] &= ~mask; // Clear "click" event + return click; } uint8_t PS4Parser::getAnalogButton(ButtonEnum b) { - if (b == L2) // These are the only analog buttons on the controller - return ps4Data.trigger[0]; - else if (b == R2) - return ps4Data.trigger[1]; - return 0; + if (b == L2) // These are the only analog buttons on the controller + return ps4Data.trigger[0]; + else if (b == R2) + return ps4Data.trigger[1]; + return 0; } uint8_t PS4Parser::getAnalogHat(AnalogHatEnum a) { - return ps4Data.hatValue[(uint8_t)a]; + return ps4Data.hatValue[(uint8_t)a]; } void PS4Parser::Parse(uint8_t len, uint8_t *buf) { - if (len > 0 && buf) { + if (len > 0 && buf) { #ifdef PRINTREPORT - Notify(PSTR("\r\n"), 0x80); - for (uint8_t i = 0; i < len; i++) { - D_PrintHex (buf[i], 0x80); - Notify(PSTR(" "), 0x80); - } + Notify(PSTR("\r\n"), 0x80); + for (uint8_t i = 0; i < len; i++) { + D_PrintHex (buf[i], 0x80); + Notify(PSTR(" "), 0x80); + } #endif - if (buf[0] == 0x01) // Check report ID - memcpy(&ps4Data, buf + 1, min(len - 1, sizeof(ps4Data))); - else if (buf[0] == 0x11) // This report is send via Bluetooth, it has an offset of 2 compared to the USB data - memcpy(&ps4Data, buf + 3, min(len - 3, sizeof(ps4Data))); - else { + if (buf[0] == 0x01) // Check report ID + memcpy(&ps4Data, buf + 1, min(len - 1, sizeof(ps4Data))); + else if (buf[0] == 0x11) // This report is send via Bluetooth, it has an offset of 2 compared to the USB data + memcpy(&ps4Data, buf + 3, min(len - 3, sizeof(ps4Data))); + else { #ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nUnknown report id: "), 0x80); - D_PrintHex (buf[0], 0x80); + Notify(PSTR("\r\nUnknown report id: "), 0x80); + D_PrintHex (buf[0], 0x80); #endif - return; - } + return; + } - for (uint8_t i = 0; i < sizeof(ps4Data.btn); i++) { - if (ps4Data.btn.val[i] != oldButtonState.val[i]) { // Check if anything has changed - buttonClickState.val[i] = ps4Data.btn.val[i] & ~oldButtonState.val[i]; // Update click state variable - oldButtonState.val[i] = ps4Data.btn.val[i]; - if (i == 0) { // The DPAD buttons does not set the different bits, but set a value corresponding to the buttons pressed, we will simply set the bits ourself - uint8_t newDpad = 0; - if (checkDpad(UP)) - newDpad |= 1 << UP; - if (checkDpad(RIGHT)) - newDpad |= 1 << RIGHT; - if (checkDpad(DOWN)) - newDpad |= 1 << DOWN; - if (checkDpad(LEFT)) - newDpad |= 1 << LEFT; - if (newDpad != oldDpad) { - buttonClickState.dpad = newDpad & ~oldDpad; // Override values - oldDpad = newDpad; - } - } - } - } - } + for (uint8_t i = 0; i < sizeof(ps4Data.btn); i++) { + if (ps4Data.btn.val[i] != oldButtonState.val[i]) { // Check if anything has changed + buttonClickState.val[i] = ps4Data.btn.val[i] & ~oldButtonState.val[i]; // Update click state variable + oldButtonState.val[i] = ps4Data.btn.val[i]; + if (i == 0) { // The DPAD buttons does not set the different bits, but set a value corresponding to the buttons pressed, we will simply set the bits ourself + uint8_t newDpad = 0; + if (checkDpad(UP)) + newDpad |= 1 << UP; + if (checkDpad(RIGHT)) + newDpad |= 1 << RIGHT; + if (checkDpad(DOWN)) + newDpad |= 1 << DOWN; + if (checkDpad(LEFT)) + newDpad |= 1 << LEFT; + if (newDpad != oldDpad) { + buttonClickState.dpad = newDpad & ~oldDpad; // Override values + oldDpad = newDpad; + } + } + } + } + } - if (ps4Output.reportChanged) - sendOutputReport(&ps4Output); // Send output report + if (ps4Output.reportChanged) + sendOutputReport(&ps4Output); // Send output report } \ No newline at end of file