-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
490 lines (462 loc) · 13.9 KB
/
Copy pathController.cpp
File metadata and controls
490 lines (462 loc) · 13.9 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/gpio.h"
#include "bsp/board_api.h"
#include "tusb.h"
//Part assignements
#define BUTTON 18
#define BUTTON2 19
#define GPIO_IN false
#define GPIO_OUT true
#define ISNESLED 22
#define ISSNESLED 20
#define ISGAMECUBELED 26
#define ISCONTROLLERLED 21
#define DATA 2
#define CLOCK 4
#define LATCH 3
#define GAMECUBE_DATA 5
enum ControllerType {
CONTROLLER_NES = 0,
CONTROLLER_SNES = 1,
CONTROLLER_GAMECUBE = 2
};
volatile enum ControllerType controllerType = CONTROLLER_NES;
volatile bool isController = false;
void button_callback(uint pin, uint32_t events){
if (pin == BUTTON){
controllerType = (controllerType + 1) % 3;
}
if (pin == BUTTON2){
isController = !isController;
}
}
void init(){
board_init();
tusb_init();
gpio_init(CLOCK);
gpio_init(LATCH);
gpio_init(DATA);
gpio_init(GAMECUBE_DATA);
if (BUTTON != -1) {
gpio_init(BUTTON);
gpio_set_dir(BUTTON,GPIO_IN);
gpio_pull_down(BUTTON);
gpio_set_irq_enabled_with_callback(BUTTON, GPIO_IRQ_EDGE_RISE, true, &button_callback);
}
if (BUTTON2 != -1) {
gpio_init(BUTTON2);
gpio_set_dir(BUTTON2,GPIO_IN);
gpio_pull_down(BUTTON2);
gpio_set_irq_enabled(BUTTON2, GPIO_IRQ_EDGE_RISE, true);
}
gpio_set_dir(DATA,GPIO_IN);
gpio_set_dir(LATCH,GPIO_OUT);
gpio_set_dir(CLOCK,GPIO_OUT);
gpio_set_dir(GAMECUBE_DATA, GPIO_OUT);
gpio_pull_up(DATA);
gpio_pull_up(GAMECUBE_DATA);
gpio_init(ISCONTROLLERLED);
gpio_set_dir(ISCONTROLLERLED,GPIO_OUT);
if (ISNESLED != -1) {
gpio_init(ISNESLED);
gpio_set_dir(ISNESLED,GPIO_OUT);
}
if (ISSNESLED != -1) {
gpio_init(ISSNESLED);
gpio_set_dir(ISSNESLED,GPIO_OUT);
}
if (ISGAMECUBELED != -1) {
gpio_init(ISGAMECUBELED);
gpio_set_dir(ISGAMECUBELED,GPIO_OUT);
}
gpio_put(CLOCK,1);
}
short getNESControllerInput(){
/*
0 - A
1 - B
2 - Select
3 - Start
4 - Up
5 - Down
6 - Left
7 - Right
*/
short controllerData = 0;
gpio_put(LATCH,1);
sleep_us(12);
gpio_put(LATCH,0);
for (int i = 0; i < 8; i++){
sleep_us(6);
gpio_put(CLOCK,0);
sleep_us(6);
controllerData = (controllerData | (!gpio_get(DATA) << i));
gpio_put(CLOCK,1);
}
return controllerData;
}
short getSNESControllerInput(){
/*
0 - B
1 - Y
2 - Select
3 - Start
4 - Up
5 - Down
6 - Left
7 - Right
8 - A
9 - X
10 - Left Bumper
11 - Right Bumper
*/
short controllerData = 0;
gpio_put(LATCH,1);
sleep_us(12);
gpio_put(LATCH,0);
for (int i = 0; i < 12; i++){
sleep_us(6);
gpio_put(CLOCK,0);
sleep_us(6);
controllerData = (controllerData | (!gpio_get(DATA) << i));
gpio_put(CLOCK,1);
}
return controllerData;
}
void emulateKeyboardNES(short controllerInput){
uint8_t modifier = 0;
uint8_t keycodes[6] = {0};
int keyIndex = 0;
if (controllerInput & 0x01) {
// A Button -> X
keycodes[keyIndex++] = HID_KEY_X;
}
if (controllerInput & 0x02) {
// B Button -> Z
keycodes[keyIndex++] = HID_KEY_Z;
}
if (controllerInput & 0x04) {
// Select -> Left Shift
modifier |= KEYBOARD_MODIFIER_LEFTSHIFT;
}
if (controllerInput& 0x08) {
// Start -> Enter
keycodes[keyIndex++] = HID_KEY_ENTER;
}
if (controllerInput & 0x10) {
// Up
keycodes[keyIndex++] = HID_KEY_ARROW_UP;
}
if (controllerInput & 0x20) {
// Down
keycodes[keyIndex++] = HID_KEY_ARROW_DOWN;
}
if (controllerInput & 0x40) {
// Left
keycodes[keyIndex++] = HID_KEY_ARROW_LEFT;
}
if (controllerInput & 0x80) {
// Right
keycodes[keyIndex++] = HID_KEY_ARROW_RIGHT;
}
tud_hid_keyboard_report(1, modifier, keycodes); // Report ID 1 for keyboard
}
void emulateControllerNES(short controllerInput){
uint8_t hat = GAMEPAD_HAT_CENTERED;
uint32_t buttons = 0;
// Map D-pad to hat switch (8 directions) Hat switches are 8 directional dpad
bool up = controllerInput & 0x10;
bool down = controllerInput & 0x20;
bool left = controllerInput & 0x40;
bool right = controllerInput & 0x80;
if (up && right) {
hat = GAMEPAD_HAT_UP_RIGHT;
} else if (up && left) {
hat = GAMEPAD_HAT_UP_LEFT;
} else if (down && right) {
hat = GAMEPAD_HAT_DOWN_RIGHT;
} else if (down && left) {
hat = GAMEPAD_HAT_DOWN_LEFT;
} else if (up) {
hat = GAMEPAD_HAT_UP;
} else if (down) {
hat = GAMEPAD_HAT_DOWN;
} else if (left) {
hat = GAMEPAD_HAT_LEFT;
} else if (right) {
hat = GAMEPAD_HAT_RIGHT;
}
// Map buttons to generic buttons
if (controllerInput & 0x01) {
buttons |= GAMEPAD_BUTTON_A;
} // A
if (controllerInput & 0x02) {
buttons |= GAMEPAD_BUTTON_B;
} // B
if (controllerInput & 0x04) {
buttons |= GAMEPAD_BUTTON_SELECT;
} // Select
if (controllerInput & 0x08) {
buttons |= GAMEPAD_BUTTON_START;
} // Start
tud_hid_gamepad_report(2, 0, 0, 0, 0, 0, 0, hat, buttons); // Report ID 2 for gamepad
}
void emulateKeyboardSNES(short controllerInput){
uint8_t modifier = 0;
uint8_t keycodes[6] = {0};
int keyIndex = 0;
if (controllerInput & 0x001) {
// B -> Z
keycodes[keyIndex++] = HID_KEY_Z;
}
if (controllerInput & 0x002) {
// Y -> A
keycodes[keyIndex++] = HID_KEY_A;
}
if (controllerInput & 0x004) {
// Select -> Left Shift
modifier |= KEYBOARD_MODIFIER_LEFTSHIFT;
}
if (controllerInput & 0x008) {
// Start -> Enter
keycodes[keyIndex++] = HID_KEY_ENTER;
}
if (controllerInput & 0x010) {
// Up
keycodes[keyIndex++] = HID_KEY_ARROW_UP;
}
if (controllerInput & 0x020) {
// Down
keycodes[keyIndex++] = HID_KEY_ARROW_DOWN;
}
if (controllerInput & 0x040) {
// Left
keycodes[keyIndex++] = HID_KEY_ARROW_LEFT;
}
if (controllerInput & 0x080) {
// Right
keycodes[keyIndex++] = HID_KEY_ARROW_RIGHT;
}
if (controllerInput & 0x100) {
// A -> X
keycodes[keyIndex++] = HID_KEY_X;
}
if (controllerInput & 0x200) {
// X -> S
keycodes[keyIndex++] = HID_KEY_S;
}
if (controllerInput & 0x400) {
// Left Bumper -> Q
keycodes[keyIndex++] = HID_KEY_Q;
}
if (controllerInput & 0x800) {
// Right Bumper -> W
keycodes[keyIndex++] = HID_KEY_W;
}
tud_hid_keyboard_report(1, modifier, keycodes); // Report ID 1 for keyboard
}
void emulateControllerSNES(short controllerInput){
uint8_t hat = GAMEPAD_HAT_CENTERED;
uint32_t buttons = 0;
// Map D-pad to hat switch (8 directions)
bool up = controllerInput & 0x010;
bool down = controllerInput & 0x020;
bool left = controllerInput & 0x040;
bool right = controllerInput & 0x080;
if (up && right) hat = GAMEPAD_HAT_UP_RIGHT;
else if (up && left) hat = GAMEPAD_HAT_UP_LEFT;
else if (down && right) hat = GAMEPAD_HAT_DOWN_RIGHT;
else if (down && left) hat = GAMEPAD_HAT_DOWN_LEFT;
else if (up) hat = GAMEPAD_HAT_UP;
else if (down) hat = GAMEPAD_HAT_DOWN;
else if (left) hat = GAMEPAD_HAT_LEFT;
else if (right) hat = GAMEPAD_HAT_RIGHT;
if (controllerInput & 0x001) {
buttons |= GAMEPAD_BUTTON_B;
} // B
if (controllerInput & 0x002) {
buttons |= GAMEPAD_BUTTON_Y;
} // Y
if (controllerInput & 0x004) {
buttons |= GAMEPAD_BUTTON_SELECT;
} // Select
if (controllerInput & 0x008) {
buttons |= GAMEPAD_BUTTON_START;
} // Start
if (controllerInput & 0x100) {
buttons |= GAMEPAD_BUTTON_A;
} // A
if (controllerInput & 0x200) {
buttons |= GAMEPAD_BUTTON_X;
} // X
if (controllerInput & 0x400) {
buttons |= GAMEPAD_BUTTON_TL;
} // Left Bumper
if (controllerInput & 0x800) {
buttons |= GAMEPAD_BUTTON_TR;
} // Right Bumper
tud_hid_gamepad_report(2, 0, 0, 0, 0, 0, 0, hat, buttons); // Report ID 2 for gamepad
}
struct GameCubeData {
uint8_t buttonHigh;
uint8_t buttonLow;
uint8_t stickX;
uint8_t stickY;
uint8_t triggerL;
uint8_t triggerR;
};
void gamecube_write_byte(uint8_t byte) {
for (int i = 7; i >= 0; i--) {
uint8_t bit = (byte >> i) & 1;
if (bit) {
gpio_put(GAMECUBE_DATA, 0);
sleep_us(1);
gpio_put(GAMECUBE_DATA, 1);
sleep_us(3);
} else {
gpio_put(GAMECUBE_DATA, 0);
sleep_us(3);
gpio_put(GAMECUBE_DATA, 1);
sleep_us(1);
}
}
}
uint8_t gamecube_read_byte(void) {
uint8_t byte = 0;
for (int i = 7; i >= 0; i--) {
uint32_t start = time_us_32();
while (!gpio_get(GAMECUBE_DATA) && (time_us_32() - start) < 10);
start = time_us_32();
while (gpio_get(GAMECUBE_DATA) && (time_us_32() - start) < 10);
uint32_t duration = time_us_32() - start;
if (duration > 1) {
byte |= (1 << i);
}
}
return byte;
}
struct GameCubeData getGameCubeControllerInput(void) {
struct GameCubeData data = {0};
gpio_set_dir(GAMECUBE_DATA, GPIO_OUT);
gamecube_write_byte(0x40);
sleep_us(100);
gpio_set_dir(GAMECUBE_DATA, GPIO_IN);
data.buttonHigh = gamecube_read_byte();
data.buttonLow = gamecube_read_byte();
data.stickX = gamecube_read_byte();
data.stickY = gamecube_read_byte();
data.triggerL = gamecube_read_byte();
data.triggerR = gamecube_read_byte();
return data;
}
void emulateKeyboardGameCube(struct GameCubeData data) {
uint8_t modifier = 0;
uint8_t keycodes[6] = {0};
int keyIndex = 0;
if (data.buttonHigh & 0x80) {
keycodes[keyIndex++] = HID_KEY_X;
}
if (data.buttonHigh & 0x40) {
keycodes[keyIndex++] = HID_KEY_Z;
}
if (data.buttonHigh & 0x20) {
keycodes[keyIndex++] = HID_KEY_A;
}
if (data.buttonHigh & 0x10) {
keycodes[keyIndex++] = HID_KEY_S;
}
if (data.buttonHigh & 0x04) {
keycodes[keyIndex++] = HID_KEY_C;
}
if (data.buttonHigh & 0x02) {
modifier |= KEYBOARD_MODIFIER_LEFTSHIFT;
}
if (data.buttonLow & 0x80) {
keycodes[keyIndex++] = HID_KEY_ARROW_UP;
}
if (data.buttonLow & 0x40) {
keycodes[keyIndex++] = HID_KEY_ARROW_DOWN;
}
if (data.buttonLow & 0x20) {
keycodes[keyIndex++] = HID_KEY_ARROW_LEFT;
}
if (data.buttonLow & 0x10) {
keycodes[keyIndex++] = HID_KEY_ARROW_RIGHT;
}
tud_hid_keyboard_report(1, modifier, keycodes);
}
void emulateControllerGameCube(struct GameCubeData data) {
uint8_t hat = GAMEPAD_HAT_CENTERED;
uint32_t buttons = 0;
int8_t x = (int8_t)(data.stickX - 128);
int8_t y = (int8_t)(data.stickY - 128);
bool up = data.buttonLow & 0x80;
bool down = data.buttonLow & 0x40;
bool left = data.buttonLow & 0x20;
bool right = data.buttonLow & 0x10;
if (up && right) hat = GAMEPAD_HAT_UP_RIGHT;
else if (up && left) hat = GAMEPAD_HAT_UP_LEFT;
else if (down && right) hat = GAMEPAD_HAT_DOWN_RIGHT;
else if (down && left) hat = GAMEPAD_HAT_DOWN_LEFT;
else if (up) hat = GAMEPAD_HAT_UP;
else if (down) hat = GAMEPAD_HAT_DOWN;
else if (left) hat = GAMEPAD_HAT_LEFT;
else if (right) hat = GAMEPAD_HAT_RIGHT;
if (data.buttonHigh & 0x80) buttons |= GAMEPAD_BUTTON_A;
if (data.buttonHigh & 0x40) buttons |= GAMEPAD_BUTTON_B;
if (data.buttonHigh & 0x20) buttons |= GAMEPAD_BUTTON_X;
if (data.buttonHigh & 0x10) buttons |= GAMEPAD_BUTTON_Y;
if (data.buttonHigh & 0x04) buttons |= GAMEPAD_BUTTON_TL;
if (data.buttonHigh & 0x02) buttons |= GAMEPAD_BUTTON_START;
if (data.triggerL > 100) buttons |= GAMEPAD_BUTTON_TL2;
if (data.triggerR > 100) buttons |= GAMEPAD_BUTTON_TR2;
tud_hid_gamepad_report(2, x, y, 0, 0, data.triggerL, data.triggerR, hat, buttons);
}
int main(){
init();
short controllerData = 0;
struct GameCubeData gcData = {0};
gpio_put(ISCONTROLLERLED, isController);
gpio_put(ISNESLED, (controllerType == CONTROLLER_NES) ? 1 : 0);
gpio_put(ISSNESLED, (controllerType == CONTROLLER_SNES) ? 1 : 0);
gpio_put(ISGAMECUBELED, (controllerType == CONTROLLER_GAMECUBE) ? 1 : 0);
while(1){
tud_task();
if (tud_hid_ready()){
switch(controllerType) {
case CONTROLLER_NES:
controllerData = getNESControllerInput();
if (isController) {
emulateControllerNES(controllerData);
} else {
emulateKeyboardNES(controllerData);
}
break;
case CONTROLLER_SNES:
controllerData = getSNESControllerInput();
if (isController) {
emulateControllerSNES(controllerData);
} else {
emulateKeyboardSNES(controllerData);
}
break;
case CONTROLLER_GAMECUBE:
gcData = getGameCubeControllerInput();
if (isController) {
emulateControllerGameCube(gcData);
} else {
emulateKeyboardGameCube(gcData);
}
break;
}
gpio_put(ISCONTROLLERLED, isController);
gpio_put(ISNESLED, (controllerType == CONTROLLER_NES) ? 1 : 0);
gpio_put(ISSNESLED, (controllerType == CONTROLLER_SNES) ? 1 : 0);
gpio_put(ISGAMECUBELED, (controllerType == CONTROLLER_GAMECUBE) ? 1 : 0);
}
sleep_ms(10);
}
return 0;
}