@@ -36,89 +36,89 @@ bool PS4Parser::checkDpad(ButtonEnum b) {
36
36
}
37
37
38
38
bool PS4Parser::getButtonPress (ButtonEnum b) {
39
- if (b <= LEFT) // Dpad
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
- }
39
+ if (b <= LEFT) // Dpad
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
+ }
47
47
}
48
48
49
49
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
61
- return click;
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
61
+ return click;
62
62
}
63
63
64
64
uint8_t PS4Parser::getAnalogButton (ButtonEnum b) {
65
- if (b == L2) // These are the only analog buttons on the controller
66
- return ps4Data.trigger [0 ];
67
- else if (b == R2)
68
- return ps4Data.trigger [1 ];
69
- return 0 ;
65
+ if (b == L2) // These are the only analog buttons on the controller
66
+ return ps4Data.trigger [0 ];
67
+ else if (b == R2)
68
+ return ps4Data.trigger [1 ];
69
+ return 0 ;
70
70
}
71
71
72
72
uint8_t PS4Parser::getAnalogHat (AnalogHatEnum a) {
73
- return ps4Data.hatValue [(uint8_t )a];
73
+ return ps4Data.hatValue [(uint8_t )a];
74
74
}
75
75
76
76
void PS4Parser::Parse (uint8_t len, uint8_t *buf) {
77
- if (len > 0 && buf) {
77
+ if (len > 0 && buf) {
78
78
#ifdef PRINTREPORT
79
- Notify (PSTR (" \r\n " ), 0x80 );
80
- for (uint8_t i = 0 ; i < len; i++) {
81
- D_PrintHex<uint8_t > (buf[i], 0x80 );
82
- Notify (PSTR (" " ), 0x80 );
83
- }
79
+ Notify (PSTR (" \r\n " ), 0x80 );
80
+ for (uint8_t i = 0 ; i < len; i++) {
81
+ D_PrintHex<uint8_t > (buf[i], 0x80 );
82
+ Notify (PSTR (" " ), 0x80 );
83
+ }
84
84
#endif
85
85
86
86
87
- if (buf[0 ] == 0x01 ) // Check report ID
88
- memcpy (&ps4Data, buf + 1 , min (len - 1 , sizeof (ps4Data)));
89
- else if (buf[0 ] == 0x11 ) // This report is send via Bluetooth, it has an offset of 2 compared to the USB data
90
- memcpy (&ps4Data, buf + 3 , min (len - 3 , sizeof (ps4Data)));
91
- else {
87
+ if (buf[0 ] == 0x01 ) // Check report ID
88
+ memcpy (&ps4Data, buf + 1 , min (len - 1 , sizeof (ps4Data)));
89
+ else if (buf[0 ] == 0x11 ) // This report is send via Bluetooth, it has an offset of 2 compared to the USB data
90
+ memcpy (&ps4Data, buf + 3 , min (len - 3 , sizeof (ps4Data)));
91
+ else {
92
92
#ifdef DEBUG_USB_HOST
93
- Notify (PSTR (" \r\n Unknown report id: " ), 0x80 );
94
- D_PrintHex<uint8_t > (buf[0 ], 0x80 );
93
+ Notify (PSTR (" \r\n Unknown report id: " ), 0x80 );
94
+ D_PrintHex<uint8_t > (buf[0 ], 0x80 );
95
95
#endif
96
- return ;
97
- }
98
-
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
- }
118
- }
119
- }
120
- }
121
-
122
- if (ps4Output.reportChanged )
123
- sendOutputReport (&ps4Output); // Send output report
96
+ return ;
97
+ }
98
+
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
+ }
118
+ }
119
+ }
120
+ }
121
+
122
+ if (ps4Output.reportChanged )
123
+ sendOutputReport (&ps4Output); // Send output report
124
124
}
0 commit comments