forked from taingmat/pokemonss-autobreeder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Joystick.c
executable file
·752 lines (644 loc) · 18.3 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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*
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.
*/
// make && sudo dfu-programmer atmega16u2 erase && sudo dfu-programmer atmega16u2 flash Joystick.hex
// make && ./teensy_loader_cli -mmcu=atmega32u4 -w Joystick.hex
#include "Joystick.h"
#include <time.h>
#include <stdio.h>
#define ECHOES 2
int echoes = 0;
USB_JoystickReport_Input_t last_report;
int report_count = 0;
int xpos = 0;
int ypos = 0;
int duration_count = 0;
int portsval = 0;
int collectCycle = 0;
int numReleased = 0;
int boxCycle = 0;
int baseeggstepmult = 2;
bool boxOpened = false;
typedef enum {
COLLECTING,
HATCHING,
RELEASING,
RAIDRESETTING,
FLY,
BUG
} Modes;
Modes mode = BUG;
int eggChecks = 330;
int numBoxes = 10;
static const command sync[] = {
// Setup controller
{ NOTHING, 250 },
{ TRIGGERS, 5 },
{ NOTHING, 100 },
{ TRIGGERS, 5 },
{ NOTHING, 100 },
{ A, 5 },
{ NOTHING, 50 }
};
static const command run[] = {
{ LEFT, 80},
{ NOTHING, 5},
{ RIGHT, 70},
{ NOTHING, 5},
{ UPRIGHT, 40},
{ NOTHING, 10}
};
// //Hatching:
static const command openPC[] = {
{X, 5},
{NOTHING, 45},
{A, 5},
{NOTHING, 70},
{R, 5},
{NOTHING, 70},
{Y, 5},
{NOTHING, 5},
{Y, 5},
{NOTHING, 5}
};
//Note move to the correct column first
static const command grabColumn[] = {
{A, 5},
{NOTHING, 5},
{DOWN, 5},
{NOTHING, 5},
{DOWN, 5},
{NOTHING, 5},
{DOWN, 5},
{NOTHING, 5},
{DOWN, 5},
{NOTHING, 5},
{A, 5},
{NOTHING, 5}
};
//Allows drops column after
static const command spin[] = {
//20 cycle
{SPIN, 2800}
//40 cycle
//{SPIN, 4900}
};
//move left a certain number of times first if needed
static const command movePokemon[] = {
//Move left
{LEFT, 5},
{NOTHING, 5},
//Move right
{RIGHT, 5},
{NOTHING, 5},
//Places eggs down
{DOWN, 5},
{NOTHING, 5},
{A, 5},
{NOTHING, 5}
};
static const command release[] = {
//Release pokemon
//a
{A, 5},
{NOTHING, 10},
//up
{UP, 5},
{NOTHING, 5},
//up
{UP, 5},
{NOTHING, 5},
//a
{A, 5},
{NOTHING, 40},
//up0
{UP, 5},
{NOTHING, 5},
//a
{A, 5},
{NOTHING, 65},
{A, 5},
{NOTHING, 40},
};
static const command bMovement[] = {
{UP, 5},
{RIGHT, 5},
{DOWN, 5},
{LEFT, 5}
};
static const command nothing[] = {
{NOTHING, 5},
{NOTHING, 10},
{NOTHING, 20},
{NOTHING, 30},
{NOTHING, 40},
};
static const command buttons[] = {
{HOME, 5},
{A, 5},
{B, 5},
{X, 5},
{Y, 5}
};
// aabaaaaaxl
// Main entry point.
int main(void) {
// We'll start by performing hardware and peripheral setup.
SetupHardware();
// We'll then enable global interrupts for our use.
GlobalInterruptEnable();
command temp = {TRIGGERS, 50};
runCommand(temp);
command temp2 = {NOTHING, 5};
runCommand(temp2);
command temp3 = {A, 50};
runCommand(temp3);
for (int i = 0; i < 300; i++) {
// A
runCommand(buttons[1]);
runCommand(nothing[4]);
runCommand(nothing[4]);
// up
runCommand(bMovement[0]);runCommand(nothing[2]);
// up
runCommand(bMovement[0]);runCommand(nothing[2]);
// A 回到坐骑形态
runCommand(buttons[1]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[2]);
// A 要让故勒顿变成坐骑形态吗
runCommand(buttons[1]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[2]);
// A 是
runCommand(buttons[1]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[2]);
// A 故勒顿变回坐骑形态了
runCommand(buttons[1]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[2]);
// Up * 5
runCommand(bMovement[0]);runCommand(nothing[2]);
runCommand(bMovement[0]);runCommand(nothing[2]);
runCommand(bMovement[0]);runCommand(nothing[2]);
runCommand(bMovement[0]);runCommand(nothing[2]);
runCommand(bMovement[0]);runCommand(nothing[2]);
// Right
runCommand(bMovement[1]);
runCommand(nothing[2]);
// A 盒子
runCommand(buttons[1]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[4]);
// X
runCommand(buttons[3]);runCommand(nothing[4]);
// X
runCommand(buttons[3]);runCommand(nothing[4]);
// L
command PRESS_L = {L, 5};
runCommand(PRESS_L);runCommand(nothing[4]);
// A 要拿故勒顿怎么办
runCommand(buttons[1]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[2]);
// Up
runCommand(bMovement[0]);runCommand(nothing[2]);
// Up
runCommand(bMovement[0]);runCommand(nothing[2]);
// Up
runCommand(bMovement[0]);runCommand(nothing[2]);
// A 收回持有物
runCommand(buttons[1]);
runCommand(nothing[4]);
// B
runCommand(buttons[2]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[4]);
runCommand(nothing[2]);
// down * 4
runCommand(bMovement[2]);runCommand(nothing[2]);
runCommand(bMovement[2]);runCommand(nothing[2]);
runCommand(bMovement[2]);runCommand(nothing[2]);
runCommand(bMovement[2]);runCommand(nothing[2]);
// Left
runCommand(bMovement[3]);runCommand(nothing[2]);
// Down
runCommand(bMovement[2]);runCommand(nothing[2]);
}
}
void runCommandList(command moves[]) {
int a = 0;
for (a = 0; a < (sizeof(moves) / sizeof(moves[0])); a++) {
runCommand(moves[a]);
}
}
void runCommand(command move) {
duration_count = 0;
while(duration_count < move.duration) {
// We need to run our task to process and deliver data for our IN and OUT endpoints.
HID_Task(move);
// We also need to run the main USB management task.
USB_USBTask();
}
}
void collect() {
//Walk left to right
int a;
for (a = 0; a < 3; a ++) {
runCommand(run[0]);
runCommand(run[1]);
runCommand(run[2]);
runCommand(run[3]);
runCommand(run[4]);
runCommand(run[5]);
}
//Talk to day care lady
command a1 = {A, 5};
runCommand(a1);
command a2 = {NOTHING, 40};
runCommand(a2);
command a3 = {A, 5};
runCommand(a3);
command a4 = {NOTHING, 50};
runCommand(a4);
//MASH B
int b;
for (b = 0; b < 13; b++) {
command b1 = {B, 15};
runCommand(b1);
command b2 = {NOTHING, 5};
runCommand(b2);
}
mode = FLY;
}
void hatch() {
int numCol;
for (numCol = 0; numCol < 6; numCol++) {
openBox();
if (numCol > 0) {
//put pokemon away
runCommand(movePokemon[0]);
runCommand(movePokemon[1]);
runCommand(movePokemon[4]);
runCommand(movePokemon[5]);
selectColumn();
//move to appropriate column
int currcol;
for (currcol = 0; currcol < numCol; currcol++) {
runCommand(movePokemon[2]);
runCommand(movePokemon[3]);
}
command up = {UP, 5};
command up2 = {NOTHING, 5};
runCommand(up);
runCommand(up2);
runCommand(grabColumn[0]);
runCommand(grabColumn[1]);
//Move to the right by 1
runCommand(movePokemon[2]);
runCommand(movePokemon[3]);
}
//Grab first set of eggs
selectColumn();
//Move them to party
int currcol2;
for (currcol2 = 0; currcol2 <= numCol; currcol2++) {
runCommand(movePokemon[0]);
runCommand(movePokemon[1]);
}
runCommand(movePokemon[4]);
runCommand(movePokemon[5]);
runCommand(movePokemon[6]);
runCommand(movePokemon[7]);
//Mash B
int b;
for (b = 0; b < 18; b++) {
command b1 = {B, 15};
runCommand(b1);
command b2 = {NOTHING, 5};
runCommand(b2);
}
//Hatch eggs
runCommand(spin[0]);
int numEggs;
for (numEggs = 0; numEggs < 5; numEggs++) {
int c;
for (c = 0; c < 37; c++) {
command b1 = {B, 15};
runCommand(b1);
command b2 = {NOTHING, 5};
runCommand(b2);
}
command shortspin = {SPIN, 20};
runCommand(shortspin);
}
}
openBox();
//put pokemon away
runCommand(movePokemon[0]);
runCommand(movePokemon[1]);
runCommand(movePokemon[4]);
runCommand(movePokemon[5]);
selectColumn();
//move to appropriate column
int currcol;
for (currcol = 0; currcol < 6; currcol++) {
runCommand(movePokemon[2]);
runCommand(movePokemon[3]);
}
command up = {UP, 5};
command up2 = {NOTHING, 5};
runCommand(up);
runCommand(up2);
runCommand(grabColumn[0]);
runCommand(grabColumn[1]);
runCommand(up);
runCommand(up2);
runCommand(movePokemon[2]);
runCommand(movePokemon[3]);
int d;
for (d = 0; d < 20; d++) {
command b1 = {B, 15};
runCommand(b1);
command b2 = {NOTHING, 5};
runCommand(b2);
}
}
void openBox() {
runCommand(openPC[0]);
runCommand(openPC[1]);
runCommand(openPC[2]);
runCommand(openPC[3]);
runCommand(openPC[4]);
runCommand(openPC[5]);
runCommand(openPC[6]);
runCommand(openPC[7]);
runCommand(openPC[8]);
runCommand(openPC[9]);
}
void selectColumn() {
runCommand(grabColumn[0]);
runCommand(grabColumn[1]);
runCommand(grabColumn[2]);
runCommand(grabColumn[3]);
runCommand(grabColumn[4]);
runCommand(grabColumn[5]);
runCommand(grabColumn[6]);
runCommand(grabColumn[7]);
runCommand(grabColumn[8]);
runCommand(grabColumn[9]);
runCommand(grabColumn[10]);
runCommand(grabColumn[11]);
}
void putPokemonAway(int numCol) {
openBox();
command a5 = {L, 5};
runCommand(a5);
runCommand(nothing[1]);
runCommand(movePokemon[0]);
runCommand(movePokemon[1]);
runCommand(movePokemon[4]);
runCommand(movePokemon[5]);
selectColumn();
int currcol;
for (currcol = 0; currcol < numCol; currcol++) {
runCommand(movePokemon[2]);
runCommand(movePokemon[3]);
}
command up = {UP, 5};
command up2 = {NOTHING, 5};
runCommand(up);
runCommand(up2);
runCommand(grabColumn[0]);
runCommand(grabColumn[1]);
//Move to the right by 1
runCommand(movePokemon[2]);
runCommand(movePokemon[3]);
}
// 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.
#ifdef ALERT_WHEN_DONE
// Both PORTD and PORTB will be used for the optional LED flashing and buzzer.
#warning LED and Buzzer functionality enabled. All pins on both PORTB and \
PORTD will toggle when printing is done.
DDRD = 0xFF; //Teensy uses PORTD
PORTD = 0x0;
//We'll just flash all pins on both ports since the UNO R3
DDRB = 0xFF; //uses PORTB. Micro can use either or, but both give us 2 LEDs
PORTB = 0x0; //The ATmega328P on the UNO will be resetting, so unplug it?
#endif
// 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.
// Not used here, it looks like we don't receive control request from the Switch.
}
// Process and deliver data from IN and OUT endpoints.
void HID_Task(command move) {
// 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.
while(Endpoint_Read_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData), NULL) != ENDPOINT_RWSTREAM_NoError);
// 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, move);
// Once populated, we can output this data to the host. We do this by first writing the data to the control stream.
while(Endpoint_Write_Stream_LE(&JoystickInputData, sizeof(JoystickInputData), NULL) != ENDPOINT_RWSTREAM_NoError);
// We then send an IN packet on this endpoint.
Endpoint_ClearIN();
}
}
typedef enum {
SYNC_CONTROLLER,
SYNC_POSITION,
BREATHE,
PROCESS,
CLEANUP,
DONE
} State_t;
State_t state = SYNC_CONTROLLER;Endpoint_Write_Stream;
// Prepare the next report for the host.
void GetNextReport(USB_JoystickReport_Input_t* const ReportData, command move) {
// Prepare an empty report
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;
// Repeat ECHOES times the last report
if (echoes > 0)
{
memcpy(ReportData, &last_report, sizeof(USB_JoystickReport_Input_t));
echoes--;
return;
}
// States and moves management
switch (state)
{
case SYNC_CONTROLLER:
state = BREATHE;
break;
case SYNC_POSITION:
ReportData->Button = 0;
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
state = BREATHE;
break;
case BREATHE:
state = PROCESS;
break;
case PROCESS:
switch (move.button)
{
case UP:
ReportData->LY = STICK_MIN;
break;
case UPRIGHT:
ReportData->LY = STICK_MIN;
ReportData->LX = STICK_MAX;
break;
case LEFT:
ReportData->LX = STICK_MIN;
break;
case DOWN:
ReportData->LY = STICK_MAX;
break;
case RIGHT:
ReportData->LX = STICK_MAX;
break;
case PLUS:
ReportData->Button |= SWITCH_PLUS;
break;
case MINUS:
ReportData->Button |= SWITCH_MINUS;
break;
case A:
ReportData->Button |= SWITCH_A;
break;
case B:
ReportData->Button |= SWITCH_B;
break;
case X:
ReportData->Button |= SWITCH_X;
break;
case Y:
ReportData->Button |= SWITCH_Y;
break;
case R:
ReportData->Button |= SWITCH_R;
break;
case L:
ReportData->Button |= SWITCH_L;
break;
case THROW:
ReportData->LY = STICK_MIN;
ReportData->Button |= SWITCH_R;
break;
case HOME:
ReportData->Button |= SWITCH_HOME;
break;
case TRIGGERS:
ReportData->Button |= SWITCH_L | SWITCH_R;
break;
case SPIN:
ReportData->LX = STICK_MIN;
ReportData->RX = STICK_MAX;
break;
default:
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
break;
}
duration_count++;
break;
case CLEANUP:
state = DONE;
break;
case DONE:
#ifdef ALERT_WHEN_DONE
portsval = ~portsval;
PORTD = portsval; //flash LED(s) and sound buzzer if attached
PORTB = portsval;
_delay_ms(250);
#endif
return;
}
// Prepare to echo this report
memcpy(&last_report, ReportData, sizeof(USB_JoystickReport_Input_t));
echoes = ECHOES;
}