Skip to content

Commit fcd6eb5

Browse files
committed
Improved the way PS4 buttons are read
1 parent a21d1e7 commit fcd6eb5

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

PS4Parser.cpp

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,14 @@ bool PS4Parser::checkDpad(ButtonEnum b) {
3838
bool PS4Parser::getButtonPress(ButtonEnum b) {
3939
if (b <= LEFT) // Dpad
4040
return checkDpad(b);
41-
else {
42-
uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
43-
uint8_t index = button < 8 ? 0 : button < 16 ? 1 : 2;
44-
uint8_t mask = 1 << (button - 8 * index);
45-
return ps4Data.btn.val[index] & mask;
46-
}
41+
else
42+
return ps4Data.btn.val & (1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]));
4743
}
4844

4945
bool PS4Parser::getButtonClick(ButtonEnum b) {
50-
uint8_t mask, index = 0;
51-
if (b <= LEFT) // Dpad
52-
mask = 1 << b;
53-
else {
54-
uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
55-
index = button < 8 ? 0 : button < 16 ? 1 : 2;
56-
mask = 1 << (button - 8 * index);
57-
}
58-
59-
bool click = buttonClickState.val[index] & mask;
60-
buttonClickState.val[index] &= ~mask; // Clear "click" event
46+
uint32_t mask = 1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
47+
bool click = buttonClickState.val & mask;
48+
buttonClickState.val &= ~mask; // Clear "click" event
6149
return click;
6250
}
6351

@@ -83,7 +71,6 @@ void PS4Parser::Parse(uint8_t len, uint8_t *buf) {
8371
}
8472
#endif
8573

86-
8774
if (buf[0] == 0x01) // Check report ID
8875
memcpy(&ps4Data, buf + 1, min(len - 1, sizeof(ps4Data)));
8976
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) {
9683
return;
9784
}
9885

99-
for (uint8_t i = 0; i < sizeof(ps4Data.btn); i++) {
100-
if (ps4Data.btn.val[i] != oldButtonState.val[i]) { // Check if anything has changed
101-
buttonClickState.val[i] = ps4Data.btn.val[i] & ~oldButtonState.val[i]; // Update click state variable
102-
oldButtonState.val[i] = ps4Data.btn.val[i];
103-
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
104-
uint8_t newDpad = 0;
105-
if (checkDpad(UP))
106-
newDpad |= 1 << UP;
107-
if (checkDpad(RIGHT))
108-
newDpad |= 1 << RIGHT;
109-
if (checkDpad(DOWN))
110-
newDpad |= 1 << DOWN;
111-
if (checkDpad(LEFT))
112-
newDpad |= 1 << LEFT;
113-
if (newDpad != oldDpad) {
114-
buttonClickState.dpad = newDpad & ~oldDpad; // Override values
115-
oldDpad = newDpad;
116-
}
117-
}
86+
if (ps4Data.btn.val != oldButtonState.val) { // Check if anything has changed
87+
buttonClickState.val = ps4Data.btn.val & ~oldButtonState.val; // Update click state variable
88+
oldButtonState.val = ps4Data.btn.val;
89+
90+
// 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
91+
uint8_t newDpad = 0;
92+
if (checkDpad(UP))
93+
newDpad |= 1 << UP;
94+
if (checkDpad(RIGHT))
95+
newDpad |= 1 << RIGHT;
96+
if (checkDpad(DOWN))
97+
newDpad |= 1 << DOWN;
98+
if (checkDpad(LEFT))
99+
newDpad |= 1 << LEFT;
100+
if (newDpad != oldDpad) {
101+
buttonClickState.dpad = newDpad & ~oldDpad; // Override values
102+
oldDpad = newDpad;
118103
}
119104
}
120105
}

PS4Parser.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ union PS4Buttons {
6868
uint8_t touchpad : 1;
6969
uint8_t reportCounter : 6;
7070
} __attribute__((packed));
71-
uint8_t val[3];
72-
};
71+
uint32_t val : 24;
72+
} __attribute__((packed));
7373

7474
struct touchpadXY {
7575
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 @@ class PS4Parser {
326326
void Reset() {
327327
uint8_t i;
328328
for (i = 0; i < sizeof(ps4Data.hatValue); i++)
329-
ps4Data.hatValue[i] = 127;
330-
for (i = 0; i < sizeof(PS4Buttons); i++) {
331-
ps4Data.btn.val[i] = 0;
332-
oldButtonState.val[i] = 0;
333-
}
329+
ps4Data.hatValue[i] = 127; // Center value
330+
ps4Data.btn.val = 0;
331+
oldButtonState.val = 0;
334332
for (i = 0; i < sizeof(ps4Data.trigger); i++)
335333
ps4Data.trigger[i] = 0;
336334
for (i = 0; i < sizeof(ps4Data.xy)/sizeof(ps4Data.xy[0]); i++) {

0 commit comments

Comments
 (0)