-
Notifications
You must be signed in to change notification settings - Fork 779
/
PS5Parser.cpp
174 lines (150 loc) · 6.94 KB
/
PS5Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Copyright (C) 2021 Kristian Sloth Lauszus. All rights reserved.
This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2[b] requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").
Contact information
-------------------
Kristian Sloth Lauszus
Web : https://lauszus.com
e-mail : [email protected]
Thanks to Joseph Duchesne for the initial code.
Based on Ludwig Füchsl's https://github.com/Ohjurot/DualSense-Windows PS5 port
and the series of patches found here: https://patchwork.kernel.org/project/linux-input/cover/[email protected]/
*/
#include "PS5Parser.h"
enum DPADEnum {
DPAD_UP = 0x0,
DPAD_UP_RIGHT = 0x1,
DPAD_RIGHT = 0x2,
DPAD_RIGHT_DOWN = 0x3,
DPAD_DOWN = 0x4,
DPAD_DOWN_LEFT = 0x5,
DPAD_LEFT = 0x6,
DPAD_LEFT_UP = 0x7,
DPAD_OFF = 0x8,
};
// To enable serial debugging see "settings.h"
//#define PRINTREPORT // Uncomment to print the report send by the PS5 Controller
int8_t PS5Parser::getButtonIndexPS5(ButtonEnum b) {
const int8_t index = ButtonIndex(b);
if ((uint8_t) index >= (sizeof(PS5_BUTTONS) / sizeof(PS5_BUTTONS[0]))) return -1;
return index;
}
bool PS5Parser::checkDpad(ButtonEnum b) {
switch (b) {
case UP:
return ps5Data.btn.dpad == DPAD_LEFT_UP || ps5Data.btn.dpad == DPAD_UP || ps5Data.btn.dpad == DPAD_UP_RIGHT;
case RIGHT:
return ps5Data.btn.dpad == DPAD_UP_RIGHT || ps5Data.btn.dpad == DPAD_RIGHT || ps5Data.btn.dpad == DPAD_RIGHT_DOWN;
case DOWN:
return ps5Data.btn.dpad == DPAD_RIGHT_DOWN || ps5Data.btn.dpad == DPAD_DOWN || ps5Data.btn.dpad == DPAD_DOWN_LEFT;
case LEFT:
return ps5Data.btn.dpad == DPAD_DOWN_LEFT || ps5Data.btn.dpad == DPAD_LEFT || ps5Data.btn.dpad == DPAD_LEFT_UP;
default:
return false;
}
}
bool PS5Parser::getButtonPress(ButtonEnum b) {
const int8_t index = getButtonIndexPS5(b); if (index < 0) return 0;
if (index <= LEFT) // Dpad
return checkDpad(b);
else
return ps5Data.btn.val & (1UL << pgm_read_byte(&PS5_BUTTONS[index]));
}
bool PS5Parser::getButtonClick(ButtonEnum b) {
const int8_t index = getButtonIndexPS5(b); if (index < 0) return 0;
uint32_t mask = 1UL << pgm_read_byte(&PS5_BUTTONS[index]);
bool click = buttonClickState.val & mask;
buttonClickState.val &= ~mask; // Clear "click" event
return click;
}
uint8_t PS5Parser::getAnalogButton(ButtonEnum b) {
const int8_t index = getButtonIndexPS5(b); if (index < 0) return 0;
if (index == ButtonIndex(L2)) // These are the only analog buttons on the controller
return ps5Data.trigger[0];
else if (index == ButtonIndex(R2))
return ps5Data.trigger[1];
return 0;
}
uint8_t PS5Parser::getAnalogHat(AnalogHatEnum a) {
return ps5Data.hatValue[(uint8_t)a];
}
void PS5Parser::Parse(uint8_t len, uint8_t *buf) {
if (len > 1 && buf) {
#ifdef PRINTREPORT
Notify(PSTR("\r\nLen: "), 0x80); Notify(len, 0x80);
Notify(PSTR(", data: "), 0x80);
for (uint8_t i = 0; i < len; i++) {
D_PrintHex<uint8_t > (buf[i], 0x80);
Notify(PSTR(" "), 0x80);
}
#endif
if (buf[0] == 0x01) // Check report ID
memcpy(&ps5Data, buf + 1, min((uint8_t)(len - 1), MFK_CASTUINT8T sizeof(ps5Data)));
else if (buf[0] == 0x31) { // This report is send via Bluetooth, it has an offset of 1 compared to the USB data
if (len < 3) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nReport is too short: "), 0x80);
D_PrintHex<uint8_t > (len, 0x80);
#endif
return;
}
memcpy(&ps5Data, buf + 2, min((uint8_t)(len - 2), MFK_CASTUINT8T sizeof(ps5Data)));
} else {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nUnknown report id: "), 0x80);
D_PrintHex<uint8_t > (buf[0], 0x80);
Notify(PSTR(", len: "), 0x80);
D_PrintHex<uint8_t > (len, 0x80);
#endif
return;
}
if (ps5Data.btn.val != oldButtonState.val) { // Check if anything has changed
buttonClickState.val = ps5Data.btn.val & ~oldButtonState.val; // Update click state variable
oldButtonState.val = ps5Data.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;
}
}
message_counter++;
}
if (ps5Output.reportChanged || leftTrigger.reportChanged || rightTrigger.reportChanged)
sendOutputReport(&ps5Output); // Send output report
}
void PS5Parser::Reset() {
uint8_t i;
for (i = 0; i < sizeof(ps5Data.hatValue); i++)
ps5Data.hatValue[i] = 127; // Center value
ps5Data.btn.val = 0;
oldButtonState.val = 0;
for (i = 0; i < sizeof(ps5Data.trigger); i++)
ps5Data.trigger[i] = 0;
for (i = 0; i < sizeof(ps5Data.xy.finger)/sizeof(ps5Data.xy.finger[0]); i++)
ps5Data.xy.finger[i].touching = 1; // The bit is cleared if the finger is touching the touchpad
ps5Data.btn.dpad = DPAD_OFF;
oldButtonState.dpad = DPAD_OFF;
buttonClickState.dpad = 0;
oldDpad = 0;
leftTrigger.Reset();
rightTrigger.Reset();
ps5Output.bigRumble = ps5Output.smallRumble = 0;
ps5Output.microphoneLed = 0;
ps5Output.disableLeds = 0;
ps5Output.playerLeds = 0;
ps5Output.r = ps5Output.g = ps5Output.b = 0;
ps5Output.reportChanged = false;
};