-
Notifications
You must be signed in to change notification settings - Fork 7
/
proconcon.c
2078 lines (1844 loc) · 51.7 KB
/
proconcon.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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
スプラトゥーン3 マウスコンバーター for RaspberryPi 4b
2022/08/28 ぬこいばらき(unvirus)
how to build
gcc proconcon.c -o proconcon.out -l pthread -lm -O3 -Wall
Version history
ver.0.01 2022/08/27 First release
ver.0.02 2022/08/28 マウスズレ調整
ver.0.03 2022/09/03 デバイスの選択を自動化、ソース整理
ver.0.04 2022/09/11 イカロールを出しやすくした
ver.0.05 2022/09/20 自動ドット打ち処理に不具合があるので削除、メイン連射追加
ver 0.06 2022/10/04 排他処理修正、復活地点にスーパージャンプを追加
ver 0.07 2022/10/29 スティック補正を不要にした
ver 0.08 2022/11/01 プロコン検出処理のバグを修正、スーパージャンプのバグを修正
ver 0.09 2022/11/25 Firmware Ver4.33で、ジャイロ加速度値が変更されているので仮対応した
ver 0.10 2022/11/27 プロコン接続を不要にした
ver 0.11 2022/12/02 Swicthのサスペンド時のプロコンコマンドに対応、コメント追加
ver 0.12 2022/12/11 人イカ逆転モードを廃止、サブ慣性キャンセル機能を追加
ver 0.13 2022/12/16 センタリング時、少し上を向くので微調整した
ver 0.14 2023/01/08 マウスを左右に振った時の追従性を向上
ver 0.15 2023/02/12 自動イカロール機能を追加、反対方向入力で自動でイカロールする
ver 0.16 2023/05/06 マウスを上下に強く動かすと座標が変になる不具合を修正、センターリングホールドモードを追加
ver 0.17 2023/07/08 冗長なソースコードを整理しました。センターリングホールドモードは使いにくいので削除した
ver 0.18 2023/10/17 SHIFTキーを押している間、ゆっくり動作が中断されない不具合を修正した
ver 0.19 2024/01/28 操作中にターミナルで余計な文字が出ないようにした、64BitOSで動作確認した
ver 0.20 2024/02/03 プログラムの終了処理を調整した
ver 0.21 2024/05/31 低速連射モード追加
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#include <math.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <termios.h>
//debug
#define ENUM_HID_DEVICE //list up hid input device
/*
スプラトゥーンでは、左右はジャイロの加速度で判断する
上下はジャイロの加速度と角度で判断する
マウスの座標系とは異なるのでユーザー毎に調整が必要になる
*/
#define X_SENSITIVITY (17.2f) //マウス操作、左右感度
#define Y_SENSITIVITY (20.5f) //マウス操作、上下感度
#define Y_FOLLOWING (1.00f) //マウス操作、上下追従補正
//スティック入力値
#define AXIS_CENTER (1920)
#define AXIS_MAX_INPUT (1920)
/*
スロー速度を設定する。0.83fならば全速力の83%になる
イカ速に応じて調整が必要
*/
#define AXIS_HALF_INPUT_FACTOR (0.65f)
#define Y_ANGLE_UPPPER_LIMIT (3000) //Y angle imit
#define Y_ANGLE_LOWER_LIMIT (-1500) //Y angle imit
#define Y_ACCEL_UPPPER_LIMIT (16000) //Y acceleration limit
#define Y_ACCEL_LOWER_LIMIT (-16000) //Y acceleration limit
#define MAX_NAME_LEN (256)
#define MAX_PACKET_LEN (64)
#define MAX_BUFFER_LEN (512)
#define MAC_ADDRESS_LEN (6)
#define GADGET_NAME "/dev/hidg0"
#define GADGET_DETACH "echo "" > /sys/kernel/config/usb_gadget/procon/UDC"
#define GADGET_ATTACH "ls /sys/class/udc > /sys/kernel/config/usb_gadget/procon/UDC"
#define DEV_KEYBOARD (0)
#define DEV_MOUSE (1)
#define PAD_INPUT_WAIT (16) //コントローラーの入力間隔(ms)
#define PAD_INPUT_WAIT_MARGIN (500000)
#define INERTIA_CANCEL_ENABLE //自動サブ慣性キャンセル機能を無効にする場合はコメントアウトする
//#define SQUID_ROLL_ENABLE //自動イカロール機能を無効にする場合はコメントアウトする
#define DELEY_FOR_AFTER_JUMP (50) //ジャンプ後、慣性キャンセルを行うようになるまでの時間、16ms単位
#define DELEY_FOR_AFTER_MAIN_WP (12) //メイン攻撃後、慣性キャンセルを行うようになるまでの時間、16ms単位
#define DELEY_FOR_AFTER_SUB_WP (12) //サブ攻撃後、慣性キャンセルを行うようになるまでの時間、16ms単位
#define MOVE_STOP_TIME (12) //動作停止までの時間、16ms単位
#define ROLL_INPUT_TIME (15) //イカロール受付時間、16ms単位
#define ROLL_JUMP_TIME (25) //イカロールジャンプ入力時間、16ms単位
#define STICK_DIR_UP (0x08)
#define STICK_DIR_RIGHT_UP (0x0C)
#define STICK_DIR_RIGHT (0x04)
#define STICK_DIR_RIGHT_DOWN (0x06)
#define STICK_DIR_DOWN (0x02)
#define STICK_DIR_LEFT_DOWN (0x03)
#define STICK_DIR_LEFT (0x01)
#define STICK_DIR_LEFT_UP (0x09)
/*
各自で利用するキーボードとマウスを指定する
指定する名称は以下で確認する
ls /dev/input/by-id
例
ls /dev/input/by-id
usb-Logitech_G403_Prodigy_Gaming_Mouse_148B38643831-event-mouse
usb-Logitech_G403_Prodigy_Gaming_Mouse_148B38643831-if01-event-kbd
usb-Logitech_G403_Prodigy_Gaming_Mouse_148B38643831-mouse
usb-Nintendo_Co.__Ltd._Pro_Controller_000000000001-event-joystick
usb-Nintendo_Co.__Ltd._Pro_Controller_000000000001-joystick
usb-SIGMACHIP_USB_Keyboard-event-if01
usb-SIGMACHIP_USB_Keyboard-event-kbd
usb-Topre_Corporation_Realforce_108-event-kbd
*/
//#define KEYBOARD_NAME "Topre_Corporation_Realforce_108"
//#define KEYBOARD_NAME "usb-SIGMACHIP_USB_Keyboard"
#define KEYBOARD_NAME "usb-SINO_WEALTH_Gaming_KB"
//#define MOUSE_NAME "Logitech_G403_Prodigy_Gaming_Mouse"
#define MOUSE_NAME "usb-Logitech_G403_HERO_Gaming_Mouse"
#define ROM_FILE_NAME "./flashrom.bin"
typedef struct {
short Y_Angle; //約4200から約-4200、平置き時約-668
short X_Angle; //約4200から約-4200、平置き時約-28
short Z_Angle; //解析情報をあさるとZ角度だが何かが変、平置き時約4075
short X_Accel; //約16000から約-16000
short Y_Accel; //約16000から約-16000
short Z_Accel; //約16000から約-16000
} ProconGyroData;
typedef struct {
//0
unsigned char ReportId; //Report ID. value is 0x30.
//1
unsigned char TimeStamp; //Time stamp increase moderately.
//2
unsigned char ConnectNo:4; //Controller number?
unsigned char BatteryLevel:4; //Battery Level?
//3
unsigned char Y:1;
unsigned char X:1;
unsigned char B:1;
unsigned char A:1;
unsigned char SR_R:1; //not use
unsigned char SL_R:1; //not use
unsigned char R:1;
unsigned char ZR:1;
//4
unsigned char Minus:1;
unsigned char Plus:1;
unsigned char StickR:1; //Right stick push.
unsigned char StickL:1; //Left stick push.
unsigned char Home:1;
unsigned char Capture:1;
unsigned char None:1; //not use
unsigned char Grip:1; //not use
//5
unsigned char Down:1;
unsigned char Up:1;
unsigned char Right:1;
unsigned char Left:1;
unsigned char SR_L:1; //not use
unsigned char SL_L:1; //not use
unsigned char L:1;
unsigned char ZL:1;
//6 - 8
unsigned char L_Axis[3]; //12Bit単位でX値、Y値が入っている
//9 - 11
unsigned char R_Axis[3]; //12Bit単位でX値、Y値が入っている
//12
unsigned char Reserved; //unknown value (Vibrator_input_report)
//13 - 63
unsigned char GyroData[51]; //Gyro sensor data is repeated 3 times. Each with 5ms sampling.
} ProconData;
typedef struct {
int X;
int Y;
int Wheel;
unsigned char L;
unsigned char R;
unsigned char Middle;
unsigned char Side; //Side button 1
unsigned char Extra; //Side button 2
} MouseData;
int Processing;
int fGadget;
int fKeyboard;
int fMouse;
int thKeyboardCreated;
int thMouseCreated;
int thOutputReportCreated;
int thInputReportCreated;
int YTotal;
int Slow;
int Straight;
int StraightHalf;
int Diagonal;
int DiagonalHalf;
int RapidFireCnt;
int RapidFireWait;
int MWBtnToggle;
int ReturnToBase;
int ReturnToBaseCnt;
int HidMode;
int GyroEnable;
int InertiaCancelCnt;
unsigned int RomSize;
unsigned int MainWpTick;
unsigned int SubWpTick;
unsigned int JumpTick;
unsigned int InputTick;
unsigned int RollKeyTick;
unsigned int RollTick;
unsigned int RollOn;
float XSensitivity;
float YSensitivity;
float YFollowing;
pthread_t thKeyboard;
pthread_t thMouse;
pthread_t thOutputReport;
pthread_t thInputReport;
pthread_mutex_t MouseMtx;
pthread_mutex_t UsbMtx;
MouseData MouseMap;
unsigned char *pRomBuf;
unsigned char DirPrev;
unsigned char DirPrevCnt;
unsigned char RollDirPrev;
unsigned char KeyMap[KEY_WIMAX];
unsigned char BakupProconData[11];
int ReadCheck(int Fd)
{
int ret;
fd_set rfds;
struct timeval tv;
if (Processing == 0)
{
return -1;
}
tv.tv_sec = 1;
tv.tv_usec = 000000;
FD_ZERO(&rfds);
FD_SET(Fd, &rfds);
ret = select(Fd + 1, &rfds, NULL, NULL, &tv);
if (ret < 0)
{
Processing = 0;
}
if (Processing == 0)
{
ret = -1;
}
//if read data incoming, returns 1.
//0 is timeout.
return ret;
}
void* KeybordThread(void *p)
{
int ret;
struct input_event event;
printf("KeybordThread start.\n");
while (Processing)
{
ret = ReadCheck(fKeyboard);
if (ret <= 0)
{
continue;
}
ret = read(fKeyboard, &event, sizeof(event));
if (ret != sizeof(event))
{
printf("Keybord read error %d.\n", errno);
Processing = 0;
continue;
}
if (event.type == EV_KEY)
{
//printf("code=0x%04x value=0x%08x.\n", event.code, event.value);
//event.value is 0=Off, 1=On, 2=Repeat
if (event.value == 2)
{
//do nothing
continue;
}
//update keyboard data
KeyMap[event.code] = event.value;
if (KeyMap[KEY_Z])
{
//super jump to base
ReturnToBase = 1;
}
if (KeyMap[KEY_7])
{
RapidFireWait = 4;
}
if (KeyMap[KEY_8])
{
RapidFireWait = 1;
}
if (KeyMap[KEY_9])
{
if (MWBtnToggle)
{
//main weapon button(Mouse L) is single shot mode
MWBtnToggle = 0;
RapidFireCnt = 0;
}
else
{
//main weapon button(Mouse L) is rapid fire mode
MWBtnToggle = 1;
RapidFireCnt = 0;
}
printf("MWBtnToggle=%d\n", MWBtnToggle);
}
Slow = KeyMap[KEY_LEFTSHIFT];
//debug
//Adjust mouse sensitivity
if (KeyMap[KEY_F5])
{
XSensitivity += 0.1f;
printf("X_SENSITIVITY=%f\n", XSensitivity);
}
if (KeyMap[KEY_F6])
{
XSensitivity -= 0.1f;
printf("X_SENSITIVITY=%f\n", XSensitivity);
}
if (KeyMap[KEY_F7])
{
YSensitivity += 0.1f;
printf("Y_SENSITIVITY=%f\n", YSensitivity);
}
if (KeyMap[KEY_F8])
{
YSensitivity -= 0.1f;
printf("Y_SENSITIVITY=%f\n", YSensitivity);
}
if (KeyMap[KEY_F9])
{
YFollowing += 0.1f;
printf("Y_FOLLOWING=%f\n", YFollowing);
}
if (KeyMap[KEY_F10])
{
YFollowing -= 0.1f;
printf("Y_FOLLOWING=%f\n", YFollowing);
}
}
}
printf("KeybordThread exit.\n");
return NULL;
}
void* MouseThread(void *p)
{
int ret;
struct input_event event;
printf("MouseThread start.\n");
while (Processing)
{
ret = ReadCheck(fMouse);
if (ret <= 0)
{
continue;
}
ret = read(fMouse, &event, sizeof(event));
if (ret != sizeof(event))
{
printf("Mouse read error %d.\n", errno);
Processing = 0;
continue;
}
if (event.type == EV_KEY)
{
//printf("code=0x%04x value=0x%08x.\n", event.code, event.value);
//event.value is 0=Off, 1=On, 2=Repeat
if (event.value == 2)
{
//do nothing
continue;
}
pthread_mutex_lock(&MouseMtx);
switch (event.code)
{
case BTN_LEFT:
MouseMap.L = event.value;
break;
case BTN_RIGHT:
MouseMap.R = event.value;
break;
case BTN_MIDDLE:
MouseMap.Middle = event.value;
break;
case BTN_SIDE:
MouseMap.Side = event.value;
break;
case BTN_EXTRA:
MouseMap.Extra = event.value;
break;
default:
break;
}
pthread_mutex_unlock(&MouseMtx);
}
else if (event.type == EV_REL)
{
//printf("code=0x%04x value=0x%08x.\n", event.code, event.value);
pthread_mutex_lock(&MouseMtx);
switch (event.code)
{
case REL_X:
MouseMap.X += event.value;
break;
case REL_Y:
MouseMap.Y += event.value;
break;
case REL_WHEEL:
MouseMap.Wheel = event.value;
break;
default:
break;
}
pthread_mutex_unlock(&MouseMtx);
}
}
printf("MouseThread exit.\n");
return NULL;
}
unsigned short XValGet(unsigned char *pBuf)
{
unsigned short ret;
ret = ((short)pBuf[1] & 0x0F) << 8;
ret |= (short)pBuf[0];
return ret;
}
unsigned short YValGet(unsigned char *pBuf)
{
unsigned short ret;
ret = (short)pBuf[1] >> 4;
ret |= (short)pBuf[2] << 4;
return ret;
}
void XValSet(unsigned char *pBuf, unsigned short X)
{
pBuf[0] = (unsigned char)(X & 0x00FF);
pBuf[1] &= 0xF0;
pBuf[1] |= (unsigned char)((X >> 8) & 0x000F);
}
void YValSet(unsigned char *pBuf, unsigned short Y)
{
pBuf[1] &= 0x0F;
pBuf[1] |= (unsigned char)((Y << 4) & 0x00F0);
pBuf[2] = (unsigned char)((Y >> 4) & 0x00FF);
}
void* OutputReportThread(void *p)
{
int ret;
int len;
int i;
unsigned int spiAddr;
unsigned char timStamp;
unsigned char rd[MAX_PACKET_LEN];
unsigned char wt[MAX_PACKET_LEN];
printf("OutputReportThread start.\n");
timStamp = 0;
while (Processing)
{
ret = ReadCheck(fGadget);
if (ret <= 0)
{
continue;
}
ret = read(fGadget, rd, sizeof(rd));
if (ret == -1)
{
printf("Gadget OutputReport read error %d.\n", errno);
Processing = 0;
continue;
}
if (ret == 0)
{
continue;
}
memset(wt, 0, sizeof(wt));
len = 0;
switch (rd[0])
{
case 0x00:
//do nothing
break;
case 0x01:
//https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/bluetooth_hid_subcommands_notes.md
if (rd[10] == 0x01)
{
//Subcommand 0x01: Bluetooth manual pairing
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x81;
wt[14] = rd[10];
wt[15] = 0x03; //saves pairing info in Joy-Con
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x02)
{
//Subcommand 0x02: Request device info
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x82;
wt[14] = rd[10];
wt[15] = 0x03; //firm ver 3.89
wt[16] = 0x48; //firm ver 3.89
wt[17] = 0x03; //Pro Controller
wt[18] = 0x02; //always 0x02
//Gyro data must be encrypted for firmware version 4.00 and above
//MAC address in Big Endian
memcpy(&wt[19], &pRomBuf[21], MAC_ADDRESS_LEN);
wt[25] = 0x01; //always 0x01
wt[26] = 0x01; //If 0x01, colors in SPI are used
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x03)
{
//Subcommand 0x03: Set input report mode
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00;
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x04)
{
//Subcommand 0x04: Trigger buttons elapsed time
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x83;
wt[14] = rd[10];
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x06)
{
//Subcommand 0x06: Set HCI state (disconnect/page/pair/turn off)
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00;
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x08)
{
//Subcommand 0x08: Set shipment low power state
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00;
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x10)
{
//Subcommand 0x10: SPI flash read
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x90; //subcommand reply
wt[14] = rd[10]; //subcommand reply
memcpy(&spiAddr, &rd[11], 4);
memcpy(&wt[15], &spiAddr, sizeof(spiAddr)); //spi address
wt[19] = rd[15]; //length
memcpy(&wt[20], &pRomBuf[spiAddr], wt[19]);
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x11)
{
//Subcommand 0x11: SPI flash Write
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80; //subcommand reply
wt[14] = rd[10]; //subcommand reply
wt[15] = 0x00; //success=0x00, write protect=0x01
len = MAX_PACKET_LEN;
//write data
memcpy(&spiAddr, &rd[11], 4);
memcpy(&pRomBuf[spiAddr], &rd[16], rd[15]);
}
else if (rd[10] == 0x12)
{
//Subcommand 0x12: SPI sector erase
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80; //subcommand reply
wt[14] = rd[10]; //subcommand reply
wt[15] = 0x00; //success=0x00, write protect=0x01
len = MAX_PACKET_LEN;
//erase data
memcpy(&spiAddr, &rd[11], 4);
memset(&pRomBuf[spiAddr], 0xFF, rd[15]);
}
else if (rd[10] == 0x21)
{
//Subcommand 0x21: Set NFC/IR MCU configuration
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = rd[11];
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x22)
{
//Subcommand 0x22: Set NFC/IR MCU state
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00; //suspend=0x00, resume=0x01, resume for update=0x02
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x30)
{
//Subcommand 0x30: Set player lights
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00;
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x33)
{
//https://greggman.github.io/html5-gamepad-test/
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x03;
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x40)
{
//Subcommand 0x40: Enable IMU (6-Axis sensor)
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00;
len = MAX_PACKET_LEN;
GyroEnable = 1;
}
else if (rd[10] == 0x41)
{
//https://greggman.github.io/html5-gamepad-test/
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00;
len = MAX_PACKET_LEN;
}
else if (rd[10] == 0x48)
{
//Subcommand 0x48: Enable vibration
wt[0] = 0x21;
wt[1] = timStamp++;
memcpy(&wt[2], BakupProconData, sizeof(BakupProconData));
wt[13] = 0x80;
wt[14] = rd[10];
wt[15] = 0x00;
len = MAX_PACKET_LEN;
}
else
{
//Add commands if needed
printf("Output Report=[0]:0x%02x [10]:0x%02x\n", rd[0], rd[10]);
}
break;
case 0x10:
//do noting
break;
case 0x80:
//https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/USB-HID-Notes.md
if (rd[1] == 0x01)
{
//get mac address
wt[0] = 0x81;
wt[1] = rd[1];
wt[2] = 0x00;
wt[3] = 0x03;
//FlashRom store MacAddress in reverse order.
for (i = 0; i < MAC_ADDRESS_LEN; i++)
{
wt[4 + i] = pRomBuf[26 - i];
}
len = MAX_PACKET_LEN;
}
else if (rd[1] == 0x02)
{
//hand shake
wt[0] = 0x81;
wt[1] = rd[1];
len = MAX_PACKET_LEN;
}
else if (rd[1] == 0x03)
{
//baudrate to 3Mbit
wt[0] = 0x81;
wt[1] = rd[1];
len = MAX_PACKET_LEN;
}
else if (rd[1] == 0x04)
{
//hid mode
HidMode = 1;
//no response
}
else if (rd[1] == 0x05)
{
//bt mode
HidMode = 0;
//no response
}
else
{
//Add commands if needed
printf("Output Report=[0]:0x%02x [1]:0x%02x\n", rd[0], rd[1]);
}
break;
default:
//Add commands if needed
printf("Output Report=[0]:0x%02x\n", rd[0]);
break;
}
if (len)
{
pthread_mutex_lock(&UsbMtx);
ret = write(fGadget, &wt, len);
pthread_mutex_unlock(&UsbMtx);
if (ret == -1)
{
printf("Gadget OutputReport write error %d.\n", errno);
Processing = 0;
continue;
}
}
}
printf("OutputReportThread exit.\n");
return NULL;
}
void StickInputL(unsigned char *pAxis, unsigned char Dir)
{
int stopping = 0;
if ((Dir == 0) && (MouseMap.Side == 1))
{
//Player is not pressing the L stick and Squid condition
if (DirPrevCnt <= MOVE_STOP_TIME)
{
//停止まで方向入力を維持
DirPrevCnt++;
Dir = DirPrev;
stopping = 1;
}
else
{
//動きを止める
DirPrev = 0;
RollKeyTick = 0;
}
}
else if ((Dir) && (MouseMap.Side == 1))
{
//イカダッシュ中
DirPrevCnt = 0;
RollKeyTick++;
}
if (Slow)
{
//ゆっくりイカ移動
DirPrevCnt = MOVE_STOP_TIME + 1;
RollKeyTick = 0;
}
if (Dir == STICK_DIR_UP)
{
if (Slow)
{
XValSet(pAxis, AXIS_CENTER);
YValSet(pAxis, AXIS_CENTER + StraightHalf);
}
else
{
if (stopping)
{
XValSet(pAxis, AXIS_CENTER);
YValSet(pAxis, AXIS_CENTER + (StraightHalf / 2));
}
else
{
XValSet(pAxis, AXIS_CENTER);
YValSet(pAxis, AXIS_CENTER + Straight);
}
}
}
else if (Dir == STICK_DIR_RIGHT_UP)
{
if (Slow)
{
XValSet(pAxis, AXIS_CENTER + DiagonalHalf);
YValSet(pAxis, AXIS_CENTER + DiagonalHalf);
}
else
{
if (stopping)
{
XValSet(pAxis, AXIS_CENTER + (DiagonalHalf / 2));
YValSet(pAxis, AXIS_CENTER + (DiagonalHalf / 2));
}
else
{
XValSet(pAxis, AXIS_CENTER + Diagonal);
YValSet(pAxis, AXIS_CENTER + Diagonal);
}
}
}
else if (Dir == STICK_DIR_RIGHT)
{
if (Slow)
{
XValSet(pAxis, AXIS_CENTER + StraightHalf);
YValSet(pAxis, AXIS_CENTER);
}
else
{
if (stopping)
{
XValSet(pAxis, AXIS_CENTER + (StraightHalf / 2));
YValSet(pAxis, AXIS_CENTER);
}
else
{
XValSet(pAxis, AXIS_CENTER + Straight);
YValSet(pAxis, AXIS_CENTER);
}
}
}
else if (Dir == STICK_DIR_RIGHT_DOWN)
{
if (Slow)
{
XValSet(pAxis, AXIS_CENTER + DiagonalHalf);
YValSet(pAxis, AXIS_CENTER - DiagonalHalf);
}
else
{
if (stopping)
{
XValSet(pAxis, AXIS_CENTER + (DiagonalHalf / 2));
YValSet(pAxis, AXIS_CENTER - (DiagonalHalf / 2));
}
else
{
XValSet(pAxis, AXIS_CENTER + Diagonal);
YValSet(pAxis, AXIS_CENTER - Diagonal);
}
}
}
else if (Dir == STICK_DIR_DOWN)
{
if (Slow)
{
XValSet(pAxis, AXIS_CENTER);
YValSet(pAxis, AXIS_CENTER - StraightHalf);
}
else
{
if (stopping)
{
XValSet(pAxis, AXIS_CENTER);
YValSet(pAxis, AXIS_CENTER - (StraightHalf / 2));
}
else
{
XValSet(pAxis, AXIS_CENTER);
YValSet(pAxis, AXIS_CENTER - Straight);
}
}
}
else if (Dir == STICK_DIR_LEFT_DOWN)
{
if (Slow)
{
XValSet(pAxis, AXIS_CENTER - DiagonalHalf);
YValSet(pAxis, AXIS_CENTER - DiagonalHalf);
}
else
{
if (stopping)