Improved the way PS4 buttons are read

This commit is contained in:
Kristian Lauszus 2014-02-17 01:25:29 +01:00
parent a21d1e7258
commit fcd6eb5084
2 changed files with 27 additions and 44 deletions

View file

@ -38,26 +38,14 @@ bool PS4Parser::checkDpad(ButtonEnum b) {
bool PS4Parser::getButtonPress(ButtonEnum b) { bool PS4Parser::getButtonPress(ButtonEnum b) {
if (b <= LEFT) // Dpad if (b <= LEFT) // Dpad
return checkDpad(b); return checkDpad(b);
else { else
uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]); return ps4Data.btn.val & (1UL << 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) { bool PS4Parser::getButtonClick(ButtonEnum b) {
uint8_t mask, index = 0; uint32_t mask = 1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
if (b <= LEFT) // Dpad bool click = buttonClickState.val & mask;
mask = 1 << b; buttonClickState.val &= ~mask; // Clear "click" event
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; return click;
} }
@ -83,7 +71,6 @@ void PS4Parser::Parse(uint8_t len, uint8_t *buf) {
} }
#endif #endif
if (buf[0] == 0x01) // Check report ID if (buf[0] == 0x01) // Check report ID
memcpy(&ps4Data, buf + 1, min(len - 1, sizeof(ps4Data))); 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 else if (buf[0] == 0x11) // This report is send via Bluetooth, it has an offset of 2 compared to the USB data
@ -96,25 +83,23 @@ void PS4Parser::Parse(uint8_t len, uint8_t *buf) {
return; return;
} }
for (uint8_t i = 0; i < sizeof(ps4Data.btn); i++) { if (ps4Data.btn.val != oldButtonState.val) { // Check if anything has changed
if (ps4Data.btn.val[i] != oldButtonState.val[i]) { // Check if anything has changed buttonClickState.val = ps4Data.btn.val & ~oldButtonState.val; // Update click state variable
buttonClickState.val[i] = ps4Data.btn.val[i] & ~oldButtonState.val[i]; // Update click state variable oldButtonState.val = ps4Data.btn.val;
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 // 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; uint8_t newDpad = 0;
if (checkDpad(UP)) if (checkDpad(UP))
newDpad |= 1 << UP; newDpad |= 1 << UP;
if (checkDpad(RIGHT)) if (checkDpad(RIGHT))
newDpad |= 1 << RIGHT; newDpad |= 1 << RIGHT;
if (checkDpad(DOWN)) if (checkDpad(DOWN))
newDpad |= 1 << DOWN; newDpad |= 1 << DOWN;
if (checkDpad(LEFT)) if (checkDpad(LEFT))
newDpad |= 1 << LEFT; newDpad |= 1 << LEFT;
if (newDpad != oldDpad) { if (newDpad != oldDpad) {
buttonClickState.dpad = newDpad & ~oldDpad; // Override values buttonClickState.dpad = newDpad & ~oldDpad; // Override values
oldDpad = newDpad; oldDpad = newDpad;
}
}
} }
} }
} }

View file

@ -68,8 +68,8 @@ union PS4Buttons {
uint8_t touchpad : 1; uint8_t touchpad : 1;
uint8_t reportCounter : 6; uint8_t reportCounter : 6;
} __attribute__((packed)); } __attribute__((packed));
uint8_t val[3]; uint32_t val : 24;
}; } __attribute__((packed));
struct touchpadXY { struct touchpadXY {
uint8_t dummy; // I can not figure out what this data is for, it seems to change randomly, maybe a timestamp? uint8_t dummy; // I can not figure out what this data is for, it seems to change randomly, maybe a timestamp?
@ -326,11 +326,9 @@ protected:
void Reset() { void Reset() {
uint8_t i; uint8_t i;
for (i = 0; i < sizeof(ps4Data.hatValue); i++) for (i = 0; i < sizeof(ps4Data.hatValue); i++)
ps4Data.hatValue[i] = 127; ps4Data.hatValue[i] = 127; // Center value
for (i = 0; i < sizeof(PS4Buttons); i++) { ps4Data.btn.val = 0;
ps4Data.btn.val[i] = 0; oldButtonState.val = 0;
oldButtonState.val[i] = 0;
}
for (i = 0; i < sizeof(ps4Data.trigger); i++) for (i = 0; i < sizeof(ps4Data.trigger); i++)
ps4Data.trigger[i] = 0; ps4Data.trigger[i] = 0;
for (i = 0; i < sizeof(ps4Data.xy)/sizeof(ps4Data.xy[0]); i++) { for (i = 0; i < sizeof(ps4Data.xy)/sizeof(ps4Data.xy[0]); i++) {