mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
Improved the way PS4 buttons are read
This commit is contained in:
parent
a21d1e7258
commit
fcd6eb5084
2 changed files with 27 additions and 44 deletions
|
@ -38,26 +38,14 @@ 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;
|
||||
}
|
||||
else
|
||||
return ps4Data.btn.val & (1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
bool click = buttonClickState.val[index] & mask;
|
||||
buttonClickState.val[index] &= ~mask; // Clear "click" event
|
||||
uint32_t mask = 1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
|
||||
bool click = buttonClickState.val & mask;
|
||||
buttonClickState.val &= ~mask; // Clear "click" event
|
||||
return click;
|
||||
}
|
||||
|
||||
|
@ -83,7 +71,6 @@ void PS4Parser::Parse(uint8_t len, uint8_t *buf) {
|
|||
}
|
||||
#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
|
||||
|
@ -96,25 +83,23 @@ void PS4Parser::Parse(uint8_t len, uint8_t *buf) {
|
|||
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;
|
||||
}
|
||||
}
|
||||
if (ps4Data.btn.val != oldButtonState.val) { // Check if anything has changed
|
||||
buttonClickState.val = ps4Data.btn.val & ~oldButtonState.val; // Update click state variable
|
||||
oldButtonState.val = ps4Data.btn.val;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
PS4Parser.h
12
PS4Parser.h
|
@ -68,8 +68,8 @@ union PS4Buttons {
|
|||
uint8_t touchpad : 1;
|
||||
uint8_t reportCounter : 6;
|
||||
} __attribute__((packed));
|
||||
uint8_t val[3];
|
||||
};
|
||||
uint32_t val : 24;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct touchpadXY {
|
||||
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() {
|
||||
uint8_t i;
|
||||
for (i = 0; i < sizeof(ps4Data.hatValue); i++)
|
||||
ps4Data.hatValue[i] = 127;
|
||||
for (i = 0; i < sizeof(PS4Buttons); i++) {
|
||||
ps4Data.btn.val[i] = 0;
|
||||
oldButtonState.val[i] = 0;
|
||||
}
|
||||
ps4Data.hatValue[i] = 127; // Center value
|
||||
ps4Data.btn.val = 0;
|
||||
oldButtonState.val = 0;
|
||||
for (i = 0; i < sizeof(ps4Data.trigger); i++)
|
||||
ps4Data.trigger[i] = 0;
|
||||
for (i = 0; i < sizeof(ps4Data.xy)/sizeof(ps4Data.xy[0]); i++) {
|
||||
|
|
Loading…
Reference in a new issue