forked from progmem/Switch-Fightstick
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJoystick.c
executable file
·311 lines (283 loc) · 9.77 KB
/
Joystick.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Nintendo Switch Fightstick - Proof-of-Concept
Based on the LUFA library's Low-Level Joystick Demo
(C) Dean Camera
Based on the HORI's Pokken Tournament Pro Pad design
(C) HORI
This project implements a modified version of HORI's Pokken Tournament Pro Pad
USB descriptors to allow for the creation of custom controllers for the
Nintendo Switch. This also works to a limited degree on the PS3.
Since System Update v3.0.0, the Nintendo Switch recognizes the Pokken
Tournament Pro Pad as a Pro Controller. Physical design limitations prevent
the Pokken Controller from functioning at the same level as the Pro
Controller. However, by default most of the descriptors are there, with the
exception of Home and Capture. Descriptor modification allows us to unlock
these buttons for our use.
*/
/** \file
*
* Main source file for the Joystick demo. This file contains the main tasks of the demo and
* is responsible for the initial application hardware configuration.
*/
#include <LUFA/Drivers/Peripheral/Serial.h>
#include "Joystick.h"
uint8_t target = RELEASE;
uint16_t command;
void parseLine(char *line) {
char t[8];
char c[16];
sscanf(line, "%s %s", t, c);
if (strcasecmp(t, "Button") == 0) {
target = Button;
} else if (strcasecmp(t, "LX") == 0) {
target = LX;
} else if (strcasecmp(t, "LY") == 0) {
target = LY;
} else if (strcasecmp(t, "RX") == 0) {
target = RX;
} else if (strcasecmp(t, "RY") == 0) {
target = RY;
} else if (strcasecmp(t, "HAT") == 0) {
target = HAT;
} else {
target = RELEASE;
}
if (strcasecmp(c, "Y") == 0) {
command = SWITCH_Y;
} else if (strcasecmp(c, "B") == 0) {
command = SWITCH_B;
} else if (strcasecmp(c, "A") == 0) {
command = SWITCH_A;
} else if (strcasecmp(c, "X") == 0) {
command = SWITCH_X;
} else if (strcasecmp(c, "L") == 0) {
command = SWITCH_L;
} else if (strcasecmp(c, "R") == 0) {
command = SWITCH_R;
} else if (strcasecmp(c, "ZL") == 0) {
command = SWITCH_ZL;
} else if (strcasecmp(c, "ZR") == 0) {
command = SWITCH_ZR;
} else if (strcasecmp(c, "SELECT") == 0) {
command = SWITCH_SELECT;
} else if (strcasecmp(c, "START") == 0) {
command = SWITCH_START;
} else if (strcasecmp(c, "LCLICK") == 0) {
command = SWITCH_LCLICK;
} else if (strcasecmp(c, "RCLICK") == 0) {
command = SWITCH_RCLICK;
} else if (strcasecmp(c, "HOME") == 0) {
command = SWITCH_HOME;
} else if (strcasecmp(c, "CAPTURE") == 0) {
command = SWITCH_CAPTURE;
} else if (strcasecmp(c, "RELEASE") == 0) {
command = SWITCH_RELEASE;
} else if (strcasecmp(c, "MIN") == 0) {
command = STICK_MIN;
} else if (strcasecmp(c, "MAX") == 0) {
command = STICK_MAX;
} else if (strcasecmp(c, "TOP") == 0) {
command = HAT_TOP;
} else if (strcasecmp(c, "TOP_RIGHT") == 0) {
command = HAT_TOP_RIGHT;
} else if (strcasecmp(c, "RIGHT") == 0) {
command = HAT_RIGHT;
} else if (strcasecmp(c, "BOTTOM_RIGHT") == 0) {
command = HAT_BOTTOM_RIGHT;
} else if (strcasecmp(c, "BOTTOM") == 0) {
command = HAT_BOTTOM;
} else if (strcasecmp(c, "BOTTOM_LEFT") == 0) {
command = HAT_BOTTOM_LEFT;
} else if (strcasecmp(c, "LEFT") == 0) {
command = HAT_LEFT;
} else if (strcasecmp(c, "TOP_LEFT") == 0) {
command = HAT_TOP_LEFT;
} else if (strcasecmp(c, "CENTER") == 0) {
if (target == HAT) {
command = HAT_CENTER;
} else {
command = STICK_CENTER;
}
} else {
target = RELEASE;
}
}
#define MAX_BUFFER 32
char b[MAX_BUFFER];
uint8_t l = 0;
ISR(USART1_RX_vect) {
char c = fgetc(stdin);
if (Serial_IsSendReady()) {
printf("%c", c);
}
if (c == '\r') {
parseLine(b);
l = 0;
memset(b, 0, sizeof(b));
} else if (c != '\n' && l < MAX_BUFFER) {
b[l++] = c;
}
}
// Main entry point.
int main(void) {
Serial_Init(9600, false);
Serial_CreateStream(NULL);
sei();
UCSR1B |= (1 << RXCIE1);
// We'll start by performing hardware and peripheral setup.
SetupHardware();
// We'll then enable global interrupts for our use.
GlobalInterruptEnable();
// Once that's done, we'll enter an infinite loop.
for (;;)
{
// We need to run our task to process and deliver data for our IN and OUT endpoints.
HID_Task();
// We also need to run the main USB management task.
USB_USBTask();
}
}
// Configures hardware and peripherals, such as the USB peripherals.
void SetupHardware(void) {
// We need to disable watchdog if enabled by bootloader/fuses.
MCUSR &= ~(1 << WDRF);
wdt_disable();
// We need to disable clock division before initializing the USB hardware.
clock_prescale_set(clock_div_1);
// We can then initialize our hardware and peripherals, including the USB stack.
// The USB stack should be initialized last.
USB_Init();
}
// Fired to indicate that the device is enumerating.
void EVENT_USB_Device_Connect(void) {
// We can indicate that we're enumerating here (via status LEDs, sound, etc.).
}
// Fired to indicate that the device is no longer connected to a host.
void EVENT_USB_Device_Disconnect(void) {
// We can indicate that our device is not ready (via status LEDs, sound, etc.).
}
// Fired when the host set the current configuration of the USB device after enumeration.
void EVENT_USB_Device_ConfigurationChanged(void) {
bool ConfigSuccess = true;
// We setup the HID report endpoints.
ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_OUT_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
// We can read ConfigSuccess to indicate a success or failure at this point.
}
// Process control requests sent to the device from the USB host.
void EVENT_USB_Device_ControlRequest(void) {
// We can handle two control requests: a GetReport and a SetReport.
switch (USB_ControlRequest.bRequest)
{
// GetReport is a request for data from the device.
case HID_REQ_GetReport:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
// We'll create an empty report.
USB_JoystickReport_Input_t JoystickInputData;
// We'll then populate this report with what we want to send to the host.
GetNextReport(&JoystickInputData);
// Since this is a control endpoint, we need to clear up the SETUP packet on this endpoint.
Endpoint_ClearSETUP();
// Once populated, we can output this data to the host. We do this by first writing the data to the control stream.
Endpoint_Write_Control_Stream_LE(&JoystickInputData, sizeof(JoystickInputData));
// We then acknowledge an OUT packet on this endpoint.
Endpoint_ClearOUT();
}
break;
case HID_REQ_SetReport:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
// We'll create a place to store our data received from the host.
USB_JoystickReport_Output_t JoystickOutputData;
// Since this is a control endpoint, we need to clear up the SETUP packet on this endpoint.
Endpoint_ClearSETUP();
// With our report available, we read data from the control stream.
Endpoint_Read_Control_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData));
// We then send an IN packet on this endpoint.
Endpoint_ClearIN();
}
break;
}
}
// Process and deliver data from IN and OUT endpoints.
void HID_Task(void) {
// If the device isn't connected and properly configured, we can't do anything here.
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
// We'll start with the OUT endpoint.
Endpoint_SelectEndpoint(JOYSTICK_OUT_EPADDR);
// We'll check to see if we received something on the OUT endpoint.
if (Endpoint_IsOUTReceived())
{
// If we did, and the packet has data, we'll react to it.
if (Endpoint_IsReadWriteAllowed())
{
// We'll create a place to store our data received from the host.
USB_JoystickReport_Output_t JoystickOutputData;
// We'll then take in that data, setting it up in our storage.
Endpoint_Read_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData), NULL);
// At this point, we can react to this data.
// However, since we're not doing anything with this data, we abandon it.
}
// Regardless of whether we reacted to the data, we acknowledge an OUT packet on this endpoint.
Endpoint_ClearOUT();
}
// We'll then move on to the IN endpoint.
Endpoint_SelectEndpoint(JOYSTICK_IN_EPADDR);
// We first check to see if the host is ready to accept data.
if (Endpoint_IsINReady())
{
// We'll create an empty report.
USB_JoystickReport_Input_t JoystickInputData;
// We'll then populate this report with what we want to send to the host.
GetNextReport(&JoystickInputData);
// Once populated, we can output this data to the host. We do this by first writing the data to the control stream.
Endpoint_Write_Stream_LE(&JoystickInputData, sizeof(JoystickInputData), NULL);
// We then send an IN packet on this endpoint.
Endpoint_ClearIN();
/* Clear the report data afterwards */
// memset(&JoystickInputData, 0, sizeof(JoystickInputData));
}
}
// Prepare the next report for the host.
void GetNextReport(USB_JoystickReport_Input_t* const ReportData) {
/* Clear the report contents */
memset(ReportData, 0, sizeof(USB_JoystickReport_Input_t));
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
ReportData->Button |= SWITCH_RELEASE;
switch(target) {
case Button:
ReportData->Button |= command;
break;
case LX:
ReportData->LX = command;
break;
case LY:
ReportData->LY = command;
break;
case RX:
ReportData->RX = command;
break;
case RY:
ReportData->RY = command;
break;
case HAT:
ReportData->HAT = command;
break;
case RELEASE:
default:
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
ReportData->Button |= SWITCH_RELEASE;
break;
}
}
// vim: noexpandtab