@@ -38,26 +38,14 @@ bool PS4Parser::checkDpad(ButtonEnum b) {
38
38
bool PS4Parser::getButtonPress (ButtonEnum b) {
39
39
if (b <= LEFT) // Dpad
40
40
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]));
47
43
}
48
44
49
45
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
61
49
return click;
62
50
}
63
51
@@ -83,7 +71,6 @@ void PS4Parser::Parse(uint8_t len, uint8_t *buf) {
83
71
}
84
72
#endif
85
73
86
-
87
74
if (buf[0 ] == 0x01 ) // Check report ID
88
75
memcpy (&ps4Data, buf + 1 , min (len - 1 , sizeof (ps4Data)));
89
76
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) {
96
83
return ;
97
84
}
98
85
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;
118
103
}
119
104
}
120
105
}
0 commit comments