-
Notifications
You must be signed in to change notification settings - Fork 60
/
core.js
1858 lines (1601 loc) · 56.8 KB
/
core.js
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
var device = null;
var devname = "";
var mode = 0;
// bitmask: 1: clone, 2: update ds5 firmware, 4: battery low, 8: ds-edge not supported
var disable_btn = 0;
var last_disable_btn = 0;
// 1 if there is any change that can be stored permanently
var has_changes_to_write = 0;
var lang_orig_text = {};
var lang_cur = {};
var lang_disabled = true;
var lang_cur_direction = "ltr";
var gj = 0;
var gu = 0;
// Alphabetical order
var available_langs = {
"ar_ar": { "name": "العربية", "file": "ar_ar.json", "direction": "rtl"},
"bg_bg": { "name": "Български", "file": "bg_bg.json", "direction": "ltr"},
"cz_cz": { "name": "Čeština", "file": "cz_cz.json", "direction": "ltr"},
"de_de": { "name": "Deutsch", "file": "de_de.json", "direction": "ltr"},
"es_es": { "name": "Español", "file": "es_es.json", "direction": "ltr"},
"fr_fr": { "name": "Français", "file": "fr_fr.json", "direction": "ltr"},
"hu_hu": { "name": "Magyar", "file": "hu_hu.json", "direction": "ltr"},
"it_it": { "name": "Italiano", "file": "it_it.json", "direction": "ltr"},
"jp_jp": { "name": "日本語", "file": "jp_jp.json", "direction": "ltr"},
"ko_kr": { "name": "한국어", "file": "ko_kr.json", "direction": "ltr"},
"nl_nl": { "name": "Nederlands", "file": "nl_nl.json", "direction": "ltr"},
"pl_pl": { "name": "Polski", "file": "pl_pl.json", "direction": "ltr"},
"pt_br": { "name": "Português do Brasil", "file": "pt_br.json", "direction": "ltr"},
"ru_ru": { "name": "Русский", "file": "ru_ru.json", "direction": "ltr"},
"tr_tr": { "name": "Türkçe", "file": "tr_tr.json", "direction": "ltr"},
"ua_ua": { "name": "Українська", "file": "ua_ua.json", "direction": "ltr"},
"zh_cn": { "name": "中文", "file": "zh_cn.json", "direction": "ltr"},
"zh_tw": { "name": "中文(繁)", "file": "zh_tw.json", "direction": "ltr"}
};
function buf2hex(buffer) {
return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')) .join('');
}
function dec2hex(i) {
return (i+0x10000).toString(16).substr(-4).toUpperCase();
}
function dec2hex32(i) {
return (i+0x100000000).toString(16).substr(-8).toUpperCase();
}
function dec2hex8(i) {
return (i+0x100).toString(16).substr(-2).toUpperCase();
}
function ds5_hw_to_bm(hw_ver) {
a = (hw_ver >> 8) & 0xff;
if(a == 0x03) {
return "BDM-010";
} else if(a == 0x04) {
return "BDM-020";
} else if(a == 0x05) {
return "BDM-030";
} else if(a == 0x06) {
return "BDM-040";
} else if(a == 0x07 || a == 0x08) {
return "BDM-050";
} else {
return l("Unknown");
}
}
function ds4_hw_to_bm(hw_ver) {
a = hw_ver >> 8;
if(a == 0x31) {
return "JDM-001";
} else if(a == 0x43) {
return "JDM-011";
} else if(a == 0x54) {
return "JDM-030";
} else if(a >= 0x64 && a <= 0x74) {
return "JDM-040";
} else if((a > 0x80 && a < 0x84) || a == 0x93) {
return "JDM-020";
} else if(a == 0xa4 || a == 0x90) {
return "JDM-050";
} else if(a == 0xb0) {
return "JDM-055 (Scuf?)";
} else if(a == 0xb4) {
return "JDM-055";
} else {
if(is_rare(hw_ver))
return "WOW!";
return l("Unknown");
}
}
function is_rare(hw_ver) {
a = hw_ver >> 8;
b = a >> 4;
return ((b == 7 && a > 0x74) || (b == 9 && a != 0x93 && a != 0x90) || a == 0xa0);
}
async function ds4_info() {
try {
var ooc = l("unknown");
var is_clone = false;
const view = lf("ds4_info", await device.receiveFeatureReport(0xa3));
var cmd = view.getUint8(0, true);
if(cmd != 0xa3 || view.buffer.byteLength < 49) {
if(view.buffer.byteLength != 49) {
ooc = l("clone");
is_clone = true;
}
}
var k1 = new TextDecoder().decode(view.buffer.slice(1, 0x10));
var k2 = new TextDecoder().decode(view.buffer.slice(0x10, 0x20));
k1=k1.replace(/\0/g, '');
k2=k2.replace(/\0/g, '');
var hw_ver_major= view.getUint16(0x21, true)
var hw_ver_minor= view.getUint16(0x23, true)
var sw_ver_major= view.getUint32(0x25, true)
var sw_ver_minor= view.getUint16(0x25+4, true)
try {
if(!is_clone) {
const view = await device.receiveFeatureReport(0x81);
ooc = l("original");
}
} catch(e) {
la("clone");
is_clone = true;
ooc = "<font color='red'><b>" + l("clone") + "</b></font>";
disable_btn |= 1;
}
clear_info();
append_info(l("Build Date"), k1 + " " + k2);
append_info(l("HW Version"), "" + dec2hex(hw_ver_major) + ":" + dec2hex(hw_ver_minor));
append_info(l("SW Version"), dec2hex32(sw_ver_major) + ":" + dec2hex(sw_ver_minor));
append_info(l("Device Type"), ooc);
if(!is_clone) {
b_info = ' <a class="link-body-emphasis" href="#" onclick="board_model_info()">' +
'<svg class="bi" width="1.3em" height="1.3em"><use xlink:href="#info"/></svg></a>';
append_info(l("Board Model"), ds4_hw_to_bm(hw_ver_minor) + b_info);
// All ok, safe to lock NVS, query it and get BD Addr
await ds4_nvlock();
await ds4_nvstatus();
bd_addr = await ds4_getbdaddr();
append_info(l("Bluetooth Address"), bd_addr);
if(is_rare(hw_ver_minor)) {
show_popup("Wow, this is a rare/weird controller! Please write me an email at [email protected] or contact me on Discord (the_al)");
}
}
} catch(e) {
ooc = "<font color='red'><b>" + l("clone") + "</b></font>";
disable_btn |= 1;
}
return true;
}
async function ds4_flash() {
la("ds4_flash");
try {
await ds4_nvunlock();
await ds4_nvlock();
show_popup(l("Changes saved successfully"));
} catch(error) {
show_popup(l("Error while saving changes:") + " " + str(error));
}
}
async function ds5_flash() {
la("ds5_flash");
try {
await ds5_nvunlock();
await ds5_nvlock();
show_popup(l("Changes saved successfully"));
} catch(error) {
show_popup(l("Error while saving changes: ") + str(error));
}
}
async function ds4_reset() {
la("ds4_reset");
try {
await device.sendFeatureReport(0xa0, alloc_req(0xa0, [4,1,0]))
} catch(error) {
}
}
async function ds5_reset() {
la("ds5_reset");
try {
await device.sendFeatureReport(0x80, alloc_req(0x80, [1,1,0]))
} catch(error) {
}
}
async function ds4_calibrate_range_begin() {
la("ds4_calibrate_range_begin");
var err = l("Range calibration failed: ");
try {
// Begin
await device.sendFeatureReport(0x90, alloc_req(0x90, [1,1,2]))
// Assert
data = await device.receiveFeatureReport(0x91)
data2 = await device.receiveFeatureReport(0x92)
d1 = data.getUint32(0, false);
d2 = data2.getUint32(0, false);
if(d1 != 0x91010201 || d2 != 0x920102ff) {
la("ds4_calibrate_range_begin_failed", {"d1": d1, "d2": d2});
close_calibrate_window();
return show_popup(err + l("Error 1"));
}
} catch(e) {
la("ds4_calibrate_range_begin_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(err + e);
}
}
async function ds4_calibrate_range_end() {
la("ds4_calibrate_range_end");
var err = l("Range calibration failed: ");
try {
// Write
await device.sendFeatureReport(0x90, alloc_req(0x90, [2,1,2]))
data = await device.receiveFeatureReport(0x91)
data2 = await device.receiveFeatureReport(0x92)
d1 = data.getUint32(0, false);
d2 = data2.getUint32(0, false);
if(d1 != 0x91010202 || d2 != 0x92010201) {
la("ds4_calibrate_range_end_failed", {"d1": d1, "d2": d2});
close_calibrate_window();
return show_popup(err + l("Error 3"));
}
update_nvs_changes_status(1);
close_calibrate_window();
show_popup(l("Range calibration completed"));
} catch(e) {
la("ds4_calibrate_range_end_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(err + e);
}
}
async function ds4_calibrate_sticks_begin() {
la("ds4_calibrate_sticks_begin");
var err = l("Stick calibration failed: ");
try {
// Begin
await device.sendFeatureReport(0x90, alloc_req(0x90, [1,1,1]))
// Assert
data = await device.receiveFeatureReport(0x91);
data2 = await device.receiveFeatureReport(0x92);
d1 = data.getUint32(0, false);
d2 = data2.getUint32(0, false);
if(d1 != 0x91010101 || d2 != 0x920101ff) {
la("ds4_calibrate_sticks_begin_failed", {"d1": d1, "d2": d2});
show_popup(err + l("Error 1"));
return false;
}
return true;
} catch(e) {
la("ds4_calibrate_sticks_begin_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
show_popup(err + e);
return false;
}
}
async function ds4_calibrate_sticks_sample() {
la("ds4_calibrate_sticks_sample");
var err = l("Stick calibration failed: ");
try {
// Sample
await device.sendFeatureReport(0x90, alloc_req(0x90, [3,1,1]))
// Assert
data = await device.receiveFeatureReport(0x91);
data2 = await device.receiveFeatureReport(0x92);
if(data.getUint32(0, false) != 0x91010101 || data2.getUint32(0, false) != 0x920101ff) {
close_calibrate_window();
d1 = dec2hex32(data.getUint32(0, false));
d2 = dec2hex32(data2.getUint32(0, false));
la("ds4_calibrate_sticks_sample_failed", {"d1": d1, "d2": d2});
show_popup(err + l("Error 2") + " (" + d1 + ", " + d2 + " at i=" + i + ")");
return false;
}
return true;
} catch(e) {
await new Promise(r => setTimeout(r, 500));
show_popup(err + e);
return false;
}
}
async function ds4_calibrate_sticks_end() {
la("ds4_calibrate_sticks_end");
var err = l("Stick calibration failed: ");
try {
// Write
await device.sendFeatureReport(0x90, alloc_req(0x90, [2,1,1]))
if(data.getUint32(0, false) != 0x91010101 || data2.getUint32(0, false) != 0x920101FF) {
d1 = dec2hex32(data.getUint32(0, false));
d2 = dec2hex32(data2.getUint32(0, false));
la("ds4_calibrate_sticks_end_failed", {"d1": d1, "d2": d2});
show_popup(err + l("Error 3") + " (" + d1 + ", " + d2 + " at i=" + i + ")");
return false;
}
update_nvs_changes_status(1);
return true;
} catch(e) {
la("ds4_calibrate_sticks_end_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
show_popup(err + e);
return false;
}
}
async function ds4_calibrate_sticks() {
la("ds4_calibrate_sticks");
var err = l("Stick calibration failed: ");
try {
set_progress(0);
// Begin
await device.sendFeatureReport(0x90, alloc_req(0x90, [1,1,1]))
// Assert
data = await device.receiveFeatureReport(0x91);
data2 = await device.receiveFeatureReport(0x92);
d1 = data.getUint32(0, false);
d2 = data2.getUint32(0, false);
if(d1 != 0x91010101 || d2 != 0x920101ff) {
la("ds4_calibrate_sticks_failed", {"s": 1, "d1": d1, "d2": d2});
close_calibrate_window();
return show_popup(err + l("Error 1"));
}
set_progress(10);
await new Promise(r => setTimeout(r, 100));
for(var i=0;i<3;i++) {
// Sample
await device.sendFeatureReport(0x90, alloc_req(0x90, [3,1,1]))
// Assert
data = await device.receiveFeatureReport(0x91);
data2 = await device.receiveFeatureReport(0x92);
if(data.getUint32(0, false) != 0x91010101 || data2.getUint32(0, false) != 0x920101ff) {
d1 = dec2hex32(data.getUint32(0, false));
d2 = dec2hex32(data2.getUint32(0, false));
la("ds4_calibrate_sticks_failed", {"s": 2, "i": i, "d1": d1, "d2": d2});
close_calibrate_window();
return show_popup(err + l("Error 2") + " (" + d1 + ", " + d2 + " at i=" + i + ")");
}
await new Promise(r => setTimeout(r, 500));
set_progress(20 + i * 30);
}
// Write
await device.sendFeatureReport(0x90, alloc_req(0x90, [2,1,1]))
if(data.getUint32(0, false) != 0x91010101 || data2.getUint32(0, false) != 0x920101FF) {
d1 = dec2hex32(data.getUint32(0, false));
d2 = dec2hex32(data2.getUint32(0, false));
la("ds4_calibrate_sticks_failed", {"s": 3, "d1": d1, "d2": d2});
close_calibrate_window();
return show_popup(err + l("Error 3") + " (" + d1 + ", " + d2 + " at i=" + i + ")");
}
set_progress(100);
await new Promise(r => setTimeout(r, 500));
close_calibrate_window()
show_popup(l("Stick calibration completed"));
} catch(e) {
la("ds4_calibrate_sticks_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(err + e);
}
}
async function ds4_nvstatus() {
await device.sendFeatureReport(0x08, alloc_req(0x08, [0xff,0, 12]))
data = lf("ds4_nvstatus", await device.receiveFeatureReport(0x11))
// 1: temporary, 0: permanent
ret = data.getUint8(1, false);
if(ret == 1) {
$("#d-nvstatus").html("<font color='green'>" + l("locked") + "</font>");
} else if(ret == 0) {
$("#d-nvstatus").html("<font color='red'>" + l("unlocked") + "</font>");
} else {
$("#d-nvstatus").html("<font color='purple'>unk " + ret + "</font>");
}
return ret;
}
async function ds5_nvstatus() {
try {
await device.sendFeatureReport(0x80, alloc_req(0x80, [3,3]))
data = lf("ds5_nvstatus", await device.receiveFeatureReport(0x81))
ret = data.getUint32(1, false);
if(ret == 0x03030201) {
$("#d-nvstatus").html("<font color='green'>" + l("locked") + "</font>");
return 1; // temporary
} else if(ret == 0x03030200) {
$("#d-nvstatus").html("<font color='red'>" + l("unlocked") + "</font>");
return 0; // permanent
} else {
$("#d-nvstatus").html("<font color='purple'>unk " + dec2hex32(ret) + "</font>");
return ret; // unknown
}
} catch(e) {
$("#d-nvstatus").html("<font color='red'>" + l("error") + "</font>");
return 2; // error
}
}
async function ds4_getbdaddr() {
try {
data = lf("ds4_getbdaddr", await device.receiveFeatureReport(0x12));
out = ""
for(i=0;i<6;i++) {
if(i >= 1) out += ":";
out += dec2hex8(data.getUint8(6-i, false));
}
return out;
} catch(e) {
return "error";
}
}
async function ds5_getbdaddr() {
try {
await device.sendFeatureReport(0x80, alloc_req(0x80, [9,2]));
data = lf("ds5_getbdaddr", await device.receiveFeatureReport(0x81));
out = ""
for(i=0;i<6;i++) {
if(i >= 1) out += ":";
out += dec2hex8(data.getUint8(4 + 5 - i, false));
}
return out;
} catch(e) {
return "error";
}
}
async function ds4_nvlock() {
la("ds4_nvlock");
await device.sendFeatureReport(0xa0, alloc_req(0xa0, [10,1,0]))
}
async function ds4_nvunlock() {
la("ds4_nvunlock");
await device.sendFeatureReport(0xa0, alloc_req(0xa0, [10,2,0x3e,0x71,0x7f,0x89]))
}
async function ds5_system_info(base, num, length, decode = true) {
await device.sendFeatureReport(128, alloc_req(128, [base,num]))
var pcba_id = lf("ds5_pcba_id", await device.receiveFeatureReport(129));
console.log(pcba_id);
if(pcba_id.getUint8(1) != base || pcba_id.getUint8(2) != num || pcba_id.getUint8(3) != 2) {
return l("error");
} else {
if(decode)
return new TextDecoder().decode(pcba_id.buffer.slice(4, 4+length));
else
return buf2hex(pcba_id.buffer.slice(4, 4+length));
}
return l("Unknown");
}
async function ds5_info() {
try {
const view = lf("ds5_info", await device.receiveFeatureReport(0x20));
var cmd = view.getUint8(0, true);
if(cmd != 0x20 || view.buffer.byteLength != 64)
return false;
var build_date = new TextDecoder().decode(view.buffer.slice(1, 1+11));
var build_time = new TextDecoder().decode(view.buffer.slice(12, 20));
var fwtype = view.getUint16(20, true);
var swseries = view.getUint16(22, true);
var hwinfo = view.getUint32(24, true);
var fwversion = view.getUint32(28, true);
var deviceinfo = new TextDecoder().decode(view.buffer.slice(32, 32+12));
var updversion = view.getUint16(44, true);
var unk = view.getUint8(46, true);
var fwversion1 = view.getUint32(48, true);
var fwversion2 = view.getUint32(52, true);
var fwversion3 = view.getUint32(56, true);
clear_info();
b_info = ' <a class="link-body-emphasis" href="#" onclick="board_model_info()">' +
'<svg class="bi" width="1.3em" height="1.3em"><use xlink:href="#info"/></svg></a>';
append_info(l("Serial Number"), await ds5_system_info(1, 19, 17), "hw");
append_info_extra(l("MCU Unique ID"), await ds5_system_info(1, 9, 9, false), "hw");
append_info_extra(l("PCBA ID"), await ds5_system_info(1, 17, 14), "hw");
append_info_extra(l("Battery Barcode"), await ds5_system_info(1, 24, 23), "hw");
append_info_extra(l("VCM Left Barcode"), await ds5_system_info(1, 26, 16), "hw");
append_info_extra(l("VCM Right Barcode"), await ds5_system_info(1, 28, 16), "hw");
append_info(l("Board Model"), ds5_hw_to_bm(hwinfo) + b_info, "hw");
append_info(l("FW Build Date"), build_date + " " + build_time, "fw");
append_info_extra(l("FW Type"), "0x" + dec2hex(fwtype), "fw");
append_info_extra(l("FW Series"), "0x" + dec2hex(swseries), "fw");
append_info_extra(l("HW Model"), "0x" + dec2hex32(hwinfo), "hw");
append_info(l("FW Version"), "0x" + dec2hex32(fwversion), "fw");
append_info(l("FW Update"), "0x" + dec2hex(updversion), "fw");
append_info_extra(l("FW Update Info"), "0x" + dec2hex8(unk), "fw");
append_info_extra(l("SBL FW Version"), "0x" + dec2hex32(fwversion1), "fw");
append_info_extra(l("Venom FW Version"), "0x" + dec2hex32(fwversion2), "fw");
append_info_extra(l("Spider FW Version"), "0x" + dec2hex32(fwversion3), "fw");
append_info_extra(l("Touchpad ID"), await ds5_system_info(5, 2, 8, false), "hw");
append_info_extra(l("Touchpad FW Version"), await ds5_system_info(5, 4, 8, false), "fw");
old_controller = build_date.search(/ 2020| 2021/);
if(old_controller != -1) {
la("ds5_info_error", {"r": "old"})
disable_btn |= 2;
return true;
}
await ds5_nvlock();
await ds5_nvstatus();
bd_addr = await ds5_getbdaddr();
append_info(l("Bluetooth Address"), bd_addr, "hw");
} catch(e) {
la("ds5_info_error", {"r": e})
show_popup(l("Cannot read controller information"));
return false;
}
return true;
}
async function ds5_calibrate_sticks_begin() {
la("ds5_calibrate_sticks_begin");
var err = l("Range calibration failed: ");
try {
// Begin
await device.sendFeatureReport(0x82, alloc_req(0x82, [1,1,1]))
// Assert
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010101) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_sticks_begin_failed", {"d1": d1});
show_popup(err + l("Error 1") + " (" + d1 + ").");
return false;
}
return true;
} catch(e) {
la("ds5_calibrate_sticks_begin_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
show_popup(err + e);
return false;
}
}
async function ds5_calibrate_sticks_sample() {
la("ds5_calibrate_sticks_sample");
var err = l("Stick calibration failed: ");
try {
// Sample
await device.sendFeatureReport(0x82, alloc_req(0x82, [3,1,1]))
// Assert
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010101) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_sticks_sample_failed", {"d1": d1});
show_popup(err + l("Error 2") + " (" + d1 + ").");
return false;
}
return true;
} catch(e) {
la("ds5_calibrate_sticks_sample_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
show_popup(err + e);
return false;
}
}
async function ds5_calibrate_sticks_end() {
la("ds5_calibrate_sticks_end");
var err = l("Stick calibration failed: ");
try {
// Write
await device.sendFeatureReport(0x82, alloc_req(0x82, [2,1,1]))
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010102) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_sticks_end_failed", {"d1": d1});
show_popup(err + l("Error 3") + " (" + d1 + ").");
return false;
}
update_nvs_changes_status(1);
return true;
} catch(e) {
la("ds5_calibrate_sticks_end_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
show_popup(err + e);
return false;
}
}
async function ds5_calibrate_sticks() {
la("ds5_fast_calibrate_sticks");
var err = l("Stick calibration failed: ");
try {
set_progress(0);
// Begin
await device.sendFeatureReport(0x82, alloc_req(0x82, [1,1,1]))
// Assert
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010101) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_sticks_failed", {"s": 1, "d1": d1});
close_calibrate_window();
return show_popup(err + l("Error 1") + " (" + d1 + ").");
}
set_progress(10);
await new Promise(r => setTimeout(r, 100));
for(var i=0;i<3;i++) {
// Sample
await device.sendFeatureReport(0x82, alloc_req(0x82, [3,1,1]))
// Assert
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010101) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_sticks_failed", {"s": 2, "i": i, "d1": d1});
close_calibrate_window();
return show_popup(err + l("Error 2") + " (" + d1 + ").");
}
await new Promise(r => setTimeout(r, 500));
set_progress(20 + i * 20);
}
await new Promise(r => setTimeout(r, 200));
set_progress(80);
// Write
await device.sendFeatureReport(0x82, alloc_req(0x82, [2,1,1]))
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010102) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_sticks_failed", {"s": 3, "d1": d1});
close_calibrate_window();
return show_popup(err + l("Error 3") + " (" + d1 + ").");
}
set_progress(100);
update_nvs_changes_status(1);
await new Promise(r => setTimeout(r, 500));
close_calibrate_window()
show_popup(l("Stick calibration completed"));
} catch(e) {
la("ds5_calibrate_sticks_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(err + e);
}
}
async function ds5_calibrate_range_begin() {
la("ds5_calibrate_range_begin");
var err = l("Range calibration failed: ");
try {
// Begin
await device.sendFeatureReport(0x82, alloc_req(0x82, [1,1,2]))
// Assert
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010201) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_range_begin_failed", {"d1": d1});
close_calibrate_window();
return show_popup(err + l("Error 1") + " (" + d1 + ").");
}
} catch(e) {
la("ds5_calibrate_range_begin_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(err + e);
}
}
async function ds5_calibrate_range_end() {
la("ds5_calibrate_range_end");
var err = l("Range calibration failed: ");
try {
// Write
await device.sendFeatureReport(0x82, alloc_req(0x82, [2,1,2]))
// Assert
data = await device.receiveFeatureReport(0x83)
if(data.getUint32(0, false) != 0x83010202) {
d1 = dec2hex32(data.getUint32(0, false));
la("ds5_calibrate_range_end_failed", {"d1": d1});
close_calibrate_window();
return show_popup(err + l("Error 1") + " (" + d1 + ").");
}
update_nvs_changes_status(1);
close_calibrate_window();
show_popup(l("Range calibration completed"));
} catch(e) {
la("ds5_calibrate_range_end_failed", {"r": e});
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(err + e);
}
}
async function ds5_nvlock() {
la("ds5_nvlock");
try {
await device.sendFeatureReport(0x80, alloc_req(0x80, [3,1]))
data = await device.receiveFeatureReport(0x83)
} catch(e) {
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(l("NVS Lock failed: ") + e);
}
}
async function ds5_nvunlock() {
la("ds5_nvunlock");
try {
await device.sendFeatureReport(0x80, alloc_req(0x80, [3,2, 101, 50, 64, 12]))
data = await device.receiveFeatureReport(0x83)
} catch(e) {
await new Promise(r => setTimeout(r, 500));
close_calibrate_window();
return show_popup(l("NVS Unlock failed: ") + e);
}
}
async function disconnect() {
la("disconnect");
if(device == null)
return;
gj = 0;
update_nvs_changes_status(0);
mode = 0;
device.close();
device = null;
disable_btn = 0;
reset_circularity();
$("#offlinebar").show();
$("#onlinebar").hide();
$("#mainmenu").hide();
$("#d-nvstatus").text = l("Unknown");
$("#d-bdaddr").text = l("Unknown");
close_calibrate_window();
}
function handleDisconnectedDevice(e) {
la("disconnected");
console.log("Disconnected: " + e.device.productName)
disconnect();
}
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = encodeURIComponent(name) + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ')
c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0)
return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
function welcome_modal() {
var already_accepted = readCookie("welcome_accepted");
if(already_accepted == "1")
return;
curModal = new bootstrap.Modal(document.getElementById('welcomeModal'), {})
curModal.show();
}
function welcome_accepted() {
la("welcome_accepted");
createCookie("welcome_accepted", "1");
$("#welcomeModal").modal("hide");
}
function gboot() {
gu = crypto.randomUUID();
$("#infoshowall").hide();
window.addEventListener('DOMContentLoaded', function() {
lang_init();
welcome_modal();
$("#checkCircularity").on('change', on_circ_check_change);
on_circ_check_change();
});
if (!("hid" in navigator)) {
$("#offlinebar").hide();
$("#onlinebar").hide();
$("#missinghid").show();
return;
}
$("#offlinebar").show();
navigator.hid.addEventListener("disconnect", handleDisconnectedDevice);
}
function alloc_req(id, data=[]) {
len = data.length;
try {
fr = device.collections[0].featureReports;
fr.forEach((e) => { if(e.reportId == id) { len = e.items[0].reportCount; }});
} catch(e) {
console.log(e);
}
out = new Uint8Array(len);
for(i=0;i<data.length && i < len;i++) {
out[i] = data[i];
}
return out;
}
var last_lx = 0, last_ly = 0, last_rx = 0, last_ry = 0;
var ll_updated = false;
var ll_data=new Array(48);
var rr_data=new Array(48);
var enable_circ_test = false;
function reset_circularity() {
for(i=0;i<ll_data.length;i++) ll_data[i] = 0;
for(i=0;i<rr_data.length;i++) rr_data[i] = 0;
enable_circ_test = false;
ll_updated = false;
$("#checkCircularity").prop('checked', false);
refresh_stick_pos();
}
function refresh_stick_pos() {
var c = document.getElementById("stickCanvas");
var ctx = c.getContext("2d");
var sz = 60;
var hb = 20 + sz;
var yb = 15 + sz;
var w = c.width;
ctx.clearRect(0, 0, c.width, c.height);
ctx.lineWidth = 1;
ctx.fillStyle = '#ffffff';
ctx.strokeStyle = '#000000';
// Left circle
ctx.beginPath();
ctx.arc(hb, yb, sz, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
ctx.stroke();
// Right circle
ctx.beginPath();
ctx.arc(w - hb, yb, sz, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
ctx.stroke();
function cc_to_color(cc) {
var dd = Math.sqrt(Math.pow((1.0 - cc), 2));
if(cc <= 1.0)
hh = 220 - 220 * Math.min(1.0, Math.max(0, (dd - 0.05)) / 0.1);
else
hh = (245 + (360-245) * Math.min(1.0, Math.max(0, (dd - 0.05)) / 0.15)) % 360;
return hh;
}
if(enable_circ_test) {
var MAX_N = ll_data.length;
for(i=0;i<MAX_N;i++) {
var kd = ll_data[i];
var kd1 = ll_data[(i+1) % ll_data.length];
if (kd === undefined || kd1 === undefined) continue;
var ka = i * Math.PI * 2 / MAX_N;
var ka1 = ((i+1)%MAX_N) * 2 * Math.PI / MAX_N;
var kx = Math.cos(ka) * kd;
var ky = Math.sin(ka) * kd;
var kx1 = Math.cos(ka1) * kd1;
var ky1 = Math.sin(ka1) * kd1;
ctx.beginPath();
ctx.moveTo(hb, yb);
ctx.lineTo(hb+kx*sz, yb+ky*sz);
ctx.lineTo(hb+kx1*sz, yb+ky1*sz);
ctx.lineTo(hb, yb);
ctx.closePath();
var cc = (kd + kd1) / 2;
var hh = cc_to_color(cc);
ctx.fillStyle = 'hsla(' + parseInt(hh) + ', 100%, 50%, 0.5)';
ctx.fill();
}
for(i=0;i<MAX_N;i++) {
var kd = rr_data[i];
var kd1 = rr_data[(i+1) % rr_data.length];
if (kd === undefined || kd1 === undefined) continue;
var ka = i * Math.PI * 2 / MAX_N;
var ka1 = ((i+1)%MAX_N) * 2 * Math.PI / MAX_N;
var kx = Math.cos(ka) * kd;
var ky = Math.sin(ka) * kd;
var kx1 = Math.cos(ka1) * kd1;
var ky1 = Math.sin(ka1) * kd1;
ctx.beginPath();
ctx.moveTo(w-hb, yb);
ctx.lineTo(w-hb+kx*sz, yb+ky*sz);
ctx.lineTo(w-hb+kx1*sz, yb+ky1*sz);
ctx.lineTo(w-hb, yb);
ctx.closePath();
var cc = (kd + kd1) / 2;
var hh = cc_to_color(cc);
ctx.fillStyle = 'hsla(' + parseInt(hh) + ', 100%, 50%, 0.5)';
ctx.fill();
}
}
ctx.strokeStyle = '#aaaaaa';
ctx.beginPath();
ctx.moveTo(hb-sz, yb);
ctx.lineTo(hb+sz, yb);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(w-hb-sz, yb);
ctx.lineTo(w-hb+sz, yb);
ctx.closePath();