-
Notifications
You must be signed in to change notification settings - Fork 14
/
huskylens.ts
987 lines (921 loc) · 30.5 KB
/
huskylens.ts
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
/**
* @file pxt-DFRobot_HuskyLens/huskylens.ts
* @brief DFRobot's huskylens makecode library.
* @n [Get the module here](https://github.com/DFRobot/pxt-DFRobot_HaskyLens)
* @n HuskyLens is an easy-to-use AI vision sensor with six built-in functions: face recognition, object tracking, object recognition, line tracking, color recognition, and label (qr code) recognition.
* Only one button is needed to complete the AI training, which can get rid of tedious training and complicated visual algorithm and help users stay focused on the conception and implementation of the project.
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright MIT Lesser General Public License
*
* @author [email](jie.tang@dfrobot.com)
* @date 2020-3-17
*/
enum Content1 {
//% block="X center"
xCenter = 1,
//% block="Y center"
yCenter = 2,
//% block="width"
width = 3,
//% block="height"
height = 4
}
enum Content2 {
//% block="X beginning"
xOrigin = 1,
//% block="Y beginning"
yOrigin = 2,
//% block="X endpoint"
xTarget = 3,
//% block="Y endpoint"
yTarget = 4
}
enum Content3 {
//% block="ID"
ID = 5,
//% block="X center"
xCenter = 1,
//% block="Y center"
yCenter = 2,
//% block="width"
width = 3,
//% block="height"
height = 4
}
enum Content4 {
//% block="ID"
ID = 5,
//% block="X beginning"
xOrigin = 1,
//% block="Y beginning"
yOrigin = 2,
//% block="X endpoint"
xTarget = 3,
//% block="Y endpoint"
yTarget = 4
}
enum HUSKYLENSResultType_t {
//%block="frame"
HUSKYLENSResultBlock = 1,
//%block="arrow"
HUSKYLENSResultArrow = 2,
}
enum HUSKYLENSMode{
//%block="save"
SAVE,
//%block="load"
LOAD,
}
enum HUSKYLENSphoto{
//%block="photo"
PHOTO,
//%block="screenshot"
SCREENSHOT
}
enum protocolCommand {
COMMAND_REQUEST = 0x20,
COMMAND_REQUEST_BLOCKS = 0x21,
COMMAND_REQUEST_ARROWS = 0x22,
COMMAND_REQUEST_LEARNED = 0x23,
COMMAND_REQUEST_BLOCKS_LEARNED = 0x24,
COMMAND_REQUEST_ARROWS_LEARNED = 0x25,
COMMAND_REQUEST_BY_ID = 0x26,
COMMAND_REQUEST_BLOCKS_BY_ID = 0x27,
COMMAND_REQUEST_ARROWS_BY_ID = 0x28,
COMMAND_RETURN_INFO = 0x29,
COMMAND_RETURN_BLOCK = 0x2A,
COMMAND_RETURN_ARROW = 0x2B,
COMMAND_REQUEST_KNOCK = 0x2C,
COMMAND_REQUEST_ALGORITHM = 0x2D,
COMMAND_RETURN_OK = 0x2E,
COMMAND_REQUEST_LEARN = 0x2F,
COMMAND_REQUEST_FORGET = 0x30,
COMMAND_REQUEST_SENSOR = 0x31,
}
enum protocolAlgorithm {
//%block="Face Recognition"
ALGORITHM_FACE_RECOGNITION = 0,
//%block="Object Tracking"
ALGORITHM_OBJECT_TRACKING = 1,
//%block="Object Recognition"
ALGORITHM_OBJECT_RECOGNITION = 2,
//%block="Line Tracking"
ALGORITHM_LINE_TRACKING = 3,
//%block="Color Recognition"
ALGORITHM_COLOR_RECOGNITION = 4,
//%block="Tag Recognition"
ALGORITHM_TAG_RECOGNITION = 5,
//%block="Object classification"
OBJECTCLASSIFICATION,
//%block="QR recogmition (Edu only)"
QRRECOGMITION,
//%block="Barcode recognition (Edu only)"
BARCODERECOGNITION,
}
//% weight=100 color=#e7660b icon="\uf083" block="HuskyLens"
namespace huskylens {
let protocolPtr: number[][] = [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]
let Protocol_t: number[] = [0, 0, 0, 0, 0, 0]
let i = 1;
let FRAME_BUFFER_SIZE = 128
let HEADER_0_INDEX = 0
let HEADER_1_INDEX = 1
let ADDRESS_INDEX = 2
let CONTENT_SIZE_INDEX = 3
let COMMAND_INDEX = 4
let CONTENT_INDEX = 5
let PROTOCOL_SIZE = 6
let send_index = 0;
let receive_index = 0;
let COMMAND_REQUEST = 0x20;
let receive_buffer: number[] = [];
let send_buffer: number[] = [];
let buffer: number[] = [];
let send_fail = false;
let receive_fail = false;
let content_current = 0;
let content_end = 0;
let content_read_end = false;
let command: number
let content: number
/**
* HuskyLens init I2C until success
*/
//%block="HuskyLens initialize I2C until success"
//% weight=90
export function initI2c(): void {
while(!readKnock());
yes();
}
/**
* HuskyLens change mode algorithm until success.
*/
//%block="HuskyLens switch algorithm to %mode"
//% weight=85
export function initMode(mode: protocolAlgorithm) {
while (!writeAlgorithm(mode, protocolCommand.COMMAND_REQUEST_ALGORITHM));
yes();
}
/**
* HuskyLens requests data and stores it in the result.
*/
//% block="HuskyLens request data once and save into the result"
//% weight=80
export function request(): void {
protocolWriteCommand(protocolCommand.COMMAND_REQUEST)
processReturn();
}
/**
* HuskyLens get the number of the learned ID from result.
*/
//%block="HuskyLens get a total number of learned IDs from the result"
//% weight=79
export function getIds(): number {
return Protocol_t[2];
}
/**
* The box or arrow HuskyLens got from result appears in screen?
*/
//%block="HuskyLens check if %Ht is on screen from the result"
//% weight=78
export function isAppear_s(Ht: HUSKYLENSResultType_t): boolean {
switch (Ht) {
case 1:
return countBlocks_s() != 0 ? true:false;
case 2:
return countArrows_s() != 0 ? true:false;
default:
return false;
}
}
/**
* HuskyLens get the parameter of box near the screen center from result.
*/
//% block="HuskyLens get %data of frame closest to the center of screen from the result"
//% weight=77
export function readBox_s(data: Content3): number {
let hk_x
let hk_y = readBlockCenterParameterDirect();
if (hk_y != -1) {
switch (data) {
case 1:
hk_x = protocolPtr[hk_y][1]; break;
case 2:
hk_x = protocolPtr[hk_y][2]; break;
case 3:
hk_x = protocolPtr[hk_y][3]; break;
case 4:
hk_x = protocolPtr[hk_y][4]; break;
default:
hk_x = protocolPtr[hk_y][5];
}
}
else hk_x = -1
return hk_x;
}
/**
* HuskyLens get the parameter of arrow near the screen center from result.
*/
//% block="HuskyLens get %data of arrow closest to the center of screen from the result"
//% weight=77
export function readArrow_s(data: Content4): number {
let hk_x
let hk_y = readArrowCenterParameterDirect()
if (hk_y != -1) {
switch (data) {
case 1:
hk_x = protocolPtr[hk_y][1]; break;
case 2:
hk_x = protocolPtr[hk_y][2]; break;
case 3:
hk_x = protocolPtr[hk_y][3]; break;
case 4:
hk_x = protocolPtr[hk_y][4]; break;
default:
hk_x = protocolPtr[hk_y][5];
}
}
else hk_x = -1
return hk_x;
}
/**
* The ID Huskylens got from result has been learned before?
* @param id to id ,eg: 1
*/
//% block="HuskyLens check if ID %id is learned from the result"
//% weight=76
export function isLearned(id: number): boolean {
let hk_x = countLearnedIDs();
if (id <= hk_x) return true;
return false;
}
/**
* The box or arrow corresponding to ID obtained by HuskyLens from result appears in screen?
* @param id to id ,eg: 1
*/
//% block="HuskyLens check if ID %id %Ht is on screen from the result"
//% weight=75
export function isAppear(id: number, Ht: HUSKYLENSResultType_t): boolean {
switch (Ht) {
case 1:
return countBlocks(id) != 0 ? true : false ;
case 2:
return countArrows(id) != 0 ? true : false;
default:
return false;
}
}
/**
* HuskyLens get the parameter of the box corresponding to ID from result.
* @param id to id ,eg: 1
*/
//%block="HuskyLens get $number1 of ID $id frame from the result"
//% weight=65
export function readeBox( id: number,number1: Content1): number {
let hk_y = cycle_block(id, 1);
let hk_x
if (countBlocks(id) != 0) {
if (hk_y != null) {
switch (number1) {
case 1:
hk_x = protocolPtr[hk_y][1]; break;
case 2:
hk_x = protocolPtr[hk_y][2]; break;
case 3:
hk_x = protocolPtr[hk_y][3]; break;
case 4:
hk_x = protocolPtr[hk_y][4]; break;
}
}
else hk_x = -1;
}
else hk_x = -1;
return hk_x;
}
/**
* HuskyLens get the parameter of the arrow corresponding to ID from result.
* @param id to id ,eg: 1
*/
//%block="HuskyLens get $number1 of ID $id arrow from the result"
//% weight=60
export function readeArrow(id: number,number1: Content2): number {
let hk_y = cycle_arrow(id, 1);
let hk_x
if (countArrows(id) != 0) {
if (hk_y != null) {
switch (number1) {
case 1:
hk_x = protocolPtr[hk_y][1]; break;
case 2:
hk_x = protocolPtr[hk_y][2]; break;
case 3:
hk_x = protocolPtr[hk_y][3]; break;
case 4:
hk_x = protocolPtr[hk_y][4]; break;
default:
hk_x = -1;
}
}
else hk_x = -1;
}
else hk_x = -1;
return hk_x;
}
/**
* HuskyLens get the box or arrow total number from result.
*
*/
//%block="HuskyLens get a total number of %Httotal from the result"
//% weight=90
//% advanced=true
export function getBox(Ht: HUSKYLENSResultType_t): number {
switch (Ht) {
case 1:
return countBlocks_s();
case 2:
return countArrows_s();
default:
return 0;
}
}
/**
* HuskyLens get the parameter of Nth box from result.
* @param index to index ,eg: 1
*/
//% block="HuskyLens get $data of the No. $index frame from the result"
//% weight=60
//% advanced=true
export function readBox_ss(index: number, data: Content3): number {
let hk_x = -1
let hk_i = index - 1
if (protocolPtr[hk_i][0] == protocolCommand.COMMAND_RETURN_BLOCK) {
switch (data) {
case 1:
hk_x = protocolPtr[hk_i][1]; break;
case 2:
hk_x = protocolPtr[hk_i][2]; break;
case 3:
hk_x = protocolPtr[hk_i][3]; break;
case 4:
hk_x = protocolPtr[hk_i][4]; break;
default:
hk_x = protocolPtr[hk_i][5];
}
} else hk_x = -1;
return hk_x;
}
/**
* HuskyLens get the parameter of the Nth arrow from result.
* @param index to index ,eg: 1
*/
//% block="HuskyLens get $data of the No. $index arrow from the result"
//% weight=60
//% advanced=true
export function readArrow_ss(index: number, data: Content4): number {
let hk_x
let hk_i = index - 1
if (protocolPtr[hk_i][0] == protocolCommand.COMMAND_RETURN_ARROW) {
switch (data) {
case 1:
hk_x = protocolPtr[hk_i][1]; break;
case 2:
hk_x = protocolPtr[hk_i][2]; break;
case 3:
hk_x = protocolPtr[hk_i][3]; break;
case 4:
hk_x = protocolPtr[hk_i][4]; break;
default:
hk_x = protocolPtr[hk_i][5];
}
} else hk_x = -1;
//protocolPtr[hk_i][0] = 0;
return hk_x;
}
/**
* HuskyLens get the total number of box or arrow from result.
* @param id to id ,eg: 1
*/
//%block="HuskyLens get a total number of ID %id %Httotal from the result"
//% weight=55
//% advanced=true
export function getBox_S(id: number, Ht: HUSKYLENSResultType_t): number {
switch (Ht) {
case 1:
return countBlocks(id);
case 2:
return countArrows(id);
default:
return 0;
}
}
/**
* HuskyLens get the parameter of the Nth box corresponding to ID from result.
* @param id to id ,eg: 1
* @param index to index ,eg: 1
*/
//%block="HuskyLens get $number1 of the ID $id No. $index frame from the result"
//% weight=45
//% advanced=true
export function readeBox_index(id: number, index: number, number1: Content1): number {
let hk_y = cycle_block(id, index);
let hk_x
if (countBlocks(id) != 0) {
if (hk_y != null) {
switch (number1) {
case 1:
hk_x = protocolPtr[hk_y][1]; break;
case 2:
hk_x = protocolPtr[hk_y][2]; break;
case 3:
hk_x = protocolPtr[hk_y][3]; break;
case 4:
hk_x = protocolPtr[hk_y][4]; break;
default:
hk_x = -1;
}
}
else hk_x = -1;
}
else hk_x = -1;
return hk_x;
}
/**
* HuskyLens get the parameter of the Nth arrow corresponding to ID from result.
* @param id to id ,eg: 1
* @param index to index ,eg: 1
*/
//%block="HuskyLens get $number1 of the ID $id No. $index arrow from the result"
//% weight=35
//% advanced=true
export function readeArrow_index(index: number, id: number, number1: Content2): number {
let hk_y = cycle_arrow(id, index);
let hk_x
if (countArrows(id) != 0) {
if (hk_y != null) {
switch (number1) {
case 1:
hk_x = protocolPtr[hk_y][1]; break;
case 2:
hk_x = protocolPtr[hk_y][2]; break;
case 3:
hk_x = protocolPtr[hk_y][3]; break;
case 4:
hk_x = protocolPtr[hk_y][4]; break;
default:
hk_x = -1;
}
}
else hk_x = -1;
}
else hk_x = -1;
return hk_x;
}
/**
* Huskylens automatic learning ID
* @param id to id ,eg: 1
*/
//%block="HuskyLens learn ID %id once automatically"
//% weight=30
//% advanced=true
export function writeLearn1(id: number):void{
while(!writeAlgorithm(id, 0X36));
}
/**
* Huskylens forget all learning data of the current algorithm
*/
//%block="HuskyLens forget all learning data of the current algorithm"
//% weight=29
//% advanced=true
export function forgetLearn():void{
while(!writeAlgorithm(0x47, 0X37));
}
/**
* Set ID name
* @param id to id ,eg: 1
* @param name to name ,eg: "DFRobot"
*/
//%block="HuskyLens name ID %id of the current algorithm as %name"
//% weight=28
//% advanced=true
export function writeName(id:number,name:string):void{
do
{
let newname = name;
let buffer = husky_lens_protocol_write_begin(0x2f);
send_buffer[send_index] = id;
send_buffer[send_index+1] = (newname.length + 1) * 2;
send_index += 2;
for(let i=0;i<newname.length;i++){
send_buffer[send_index] = newname.charCodeAt(i);
//serial.writeNumber(newname.charCodeAt(i))
send_index ++;
}
send_buffer[send_index]=0;
send_index += 1;
let length = husky_lens_protocol_write_end();
let Buffer = pins.createBufferFromArray(buffer);
protocolWrite(Buffer);
}while(!wait(protocolCommand.COMMAND_RETURN_OK));
}
/**
* Display characters on the screen
* @param name to name ,eg: "DFRobot"
* @param x to x ,eg: 150
* @param y to y ,eg: 30
*/
//%block="HuskyLens show custom texts %name at position x %x y %y on screen"
//% weight=27
//% advanced=true
//% x.min=0 x.max=319
//% y.min=0 y.max=210
export function writeOSD(name:string, x:number, y:number):void{
do
{
let buffer = husky_lens_protocol_write_begin(0x34);
send_buffer[send_index] = name.length;
if(x>255){
send_buffer[send_index+2] = (x%255);
send_buffer[send_index+1] = 0xff;
}else{
send_buffer[send_index+1] = 0;
send_buffer[send_index+2] = x;
}
send_buffer[send_index+3] = y;
send_index += 4;
for(let i=0;i<name.length;i++){
send_buffer[send_index] = name.charCodeAt(i);
//serial.writeNumber(name.charCodeAt(i));
send_index ++;
}
let length = husky_lens_protocol_write_end();
let Buffer = pins.createBufferFromArray(buffer);
protocolWrite(Buffer);
}while(!wait(protocolCommand.COMMAND_RETURN_OK));
}
/**
* HuskyLens clear characters in the screen
*/
//%block="HuskyLens clear all custom texts on screen"
//% weight=26
//% advanced=true
export function clearOSD():void{
while(!writeAlgorithm(0x45, 0X35));
}
/**
* Photos and screenshots
*/
//%block="HuskyLens take %request and save to SD card"
//% weight=25
//% advanced=true
export function takePhotoToSDCard(request:HUSKYLENSphoto):void{
switch(request){
case HUSKYLENSphoto.PHOTO:
while(!writeAlgorithm(0x40, 0X30));
break;
case HUSKYLENSphoto.SCREENSHOT:
while(!writeAlgorithm(0x49, 0X39));
break;
default:
while(!writeAlgorithm(0x40, 0X30));
}
}
/**
* Save data model
*/
//%block="HuskyLens %command current algorithm data as No. %data model of SD card"
//% weight=24
//% advanced=true
//% data.min=0 data.max=5
export function saveModelToTFCard(command:HUSKYLENSMode,data:number):void{
switch(command){
case HUSKYLENSMode.SAVE:
while(!writeAlgorithm(data,0x32));
break;
case HUSKYLENSMode.LOAD:
while(!writeAlgorithm(data,0x33));
break;
default:
while(!writeAlgorithm(data,0x32));
}
}
function validateCheckSum() {
let stackSumIndex = receive_buffer[3] + CONTENT_INDEX;
let hk_sum = 0;
for (let i = 0; i < stackSumIndex; i++) {
hk_sum += receive_buffer[i];
}
hk_sum = hk_sum & 0xff;
return (hk_sum == receive_buffer[stackSumIndex]);
}
function husky_lens_protocol_write_end() {
if (send_fail) { return 0; }
if (send_index + 1 >= FRAME_BUFFER_SIZE) { return 0; }
send_buffer[CONTENT_SIZE_INDEX] = send_index - CONTENT_INDEX;
//serial.writeValue("618", send_buffer[CONTENT_SIZE_INDEX])
let hk_sum = 0;
for (let i = 0; i < send_index; i++) {
hk_sum += send_buffer[i];
}
hk_sum = hk_sum & 0xff;
send_buffer[send_index] = hk_sum;
send_index++;
return send_index;
}
function husky_lens_protocol_write_begin(command = 0) {
send_fail = false;
send_buffer[HEADER_0_INDEX] = 0x55;
send_buffer[HEADER_1_INDEX] = 0xAA;
send_buffer[ADDRESS_INDEX] = 0x11;
//send_buffer[CONTENT_SIZE_INDEX] = datalen;
send_buffer[COMMAND_INDEX] = command;
send_index = CONTENT_INDEX;
return send_buffer;
}
function protocolWrite(buffer: Buffer) {
pins.i2cWriteBuffer(0x32, buffer, false);
}
function processReturn() {
if (!wait(protocolCommand.COMMAND_RETURN_INFO)) return false;
protocolReadFiveInt16(protocolCommand.COMMAND_RETURN_INFO);
for (let i = 0; i < Protocol_t[1]; i++) {
if (!wait()) return false;
if (protocolReadFiveInt161(i, protocolCommand.COMMAND_RETURN_BLOCK)) continue;
else if (protocolReadFiveInt161(i, protocolCommand.COMMAND_RETURN_ARROW)) continue;
else return false;
}
return true;
}
function wait(command = 0) {
timerBegin();
while(!timerAvailable()) {
if (protocolAvailable()) {
if (command) {
if (husky_lens_protocol_read_begin(command)) {
return true;
}
}
else {
return true;
}
}else{
return false;
}
}
return false;
}
function husky_lens_protocol_read_begin(command = 0) {
if (command == receive_buffer[COMMAND_INDEX]) {
content_current = CONTENT_INDEX;
content_read_end = false;
receive_fail = false;
return true;
}
return false;
}
let timeOutDuration = 100;
let timeOutTimer: number
function timerBegin() {
timeOutTimer = input.runningTime();
}
function timerAvailable() {
return (input.runningTime() - timeOutTimer > timeOutDuration);
}
let m_i = 16
function protocolAvailable() {
let buf = pins.createBuffer(16)
if (m_i == 16) {
buf = pins.i2cReadBuffer(0x32, 16, false);
m_i = 0;
}
for (let i = m_i; i < 16; i++) {
if (husky_lens_protocol_receive(buf[i])) {
m_i++;
return true;
}
m_i++;
}
return false
}
function husky_lens_protocol_receive(data: number): boolean {
switch (receive_index) {
case HEADER_0_INDEX:
if (data != 0x55) { receive_index = 0; return false; }
receive_buffer[HEADER_0_INDEX] = 0x55;
break;
case HEADER_1_INDEX:
if (data != 0xAA) { receive_index = 0; return false; }
receive_buffer[HEADER_1_INDEX] = 0xAA;
break;
case ADDRESS_INDEX:
receive_buffer[ADDRESS_INDEX] = data;
break;
case CONTENT_SIZE_INDEX:
if (data >= FRAME_BUFFER_SIZE - PROTOCOL_SIZE) { receive_index = 0; return false; }
receive_buffer[CONTENT_SIZE_INDEX] = data;
break;
default:
receive_buffer[receive_index] = data;
if (receive_index == receive_buffer[CONTENT_SIZE_INDEX] + CONTENT_INDEX) {
content_end = receive_index;
receive_index = 0;
return validateCheckSum();
}
break;
}
receive_index++;
return false;
}
function husky_lens_protocol_write_int16(content = 0) {
let x: number = ((content.toString()).length)
if (send_index + x >= FRAME_BUFFER_SIZE) { send_fail = true; return; }
send_buffer[send_index] = content & 0xff;
send_buffer[send_index + 1] = (content >> 8) & 0xff;
send_index += 2;
}
function protocolReadFiveInt16(command = 0) {
if (husky_lens_protocol_read_begin(command)) {
Protocol_t[0] = command;
Protocol_t[1] = husky_lens_protocol_read_int16();
Protocol_t[2] = husky_lens_protocol_read_int16();
Protocol_t[3] = husky_lens_protocol_read_int16();
Protocol_t[4] = husky_lens_protocol_read_int16();
Protocol_t[5] = husky_lens_protocol_read_int16();
husky_lens_protocol_read_end();
return true;
}
else {
return false;
}
}
function protocolReadFiveInt161(i: number, command = 0) {
if (husky_lens_protocol_read_begin(command)) {
protocolPtr[i][0] = command;
protocolPtr[i][1] = husky_lens_protocol_read_int16();
protocolPtr[i][2] = husky_lens_protocol_read_int16();
protocolPtr[i][3] = husky_lens_protocol_read_int16();
protocolPtr[i][4] = husky_lens_protocol_read_int16();
protocolPtr[i][5] = husky_lens_protocol_read_int16();
husky_lens_protocol_read_end();
return true;
}
else {
return false;
}
}
function husky_lens_protocol_read_int16() {
if (content_current >= content_end || content_read_end) { receive_fail = true; return 0; }
let result = receive_buffer[content_current + 1] << 8 | receive_buffer[content_current];
content_current += 2
return result;
}
function husky_lens_protocol_read_end() {
if (receive_fail) {
receive_fail = false;
return false;
}
return content_current == content_end;
}
function countLearnedIDs() {
return Protocol_t[2]
}
function countBlocks(ID: number) {
let counter = 0;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_BLOCK && protocolPtr[i][5] == ID) counter++;
}
return counter;
}
function countBlocks_s() {
let counter = 0;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_BLOCK) counter++;
}
return counter;
}
function countArrows(ID: number) {
let counter = 0;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_ARROW && protocolPtr[i][5] == ID) counter++;
}
return counter;
}
function countArrows_s() {
let counter = 0;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_ARROW) counter++;
}
return counter;
}
function readKnock() {
for (let i = 0; i < 5; i++) {
protocolWriteCommand(protocolCommand.COMMAND_REQUEST_KNOCK);//I2C
if (wait(protocolCommand.COMMAND_RETURN_OK)) {
return true;
}
}
return false;
}
function writeForget() {
for (let i = 0; i < 5; i++) {
protocolWriteCommand(protocolCommand.COMMAND_REQUEST_FORGET);
if (wait(protocolCommand.COMMAND_RETURN_OK)) {
return true;
}
}
return false;
}
function protocolWriteCommand(command = 0) {
Protocol_t[0] = command;
let buffer = husky_lens_protocol_write_begin(Protocol_t[0]);
let length = husky_lens_protocol_write_end();
let Buffer = pins.createBufferFromArray(buffer);
protocolWrite(Buffer);
}
function protocolReadCommand(command = 0) {
if (husky_lens_protocol_read_begin(command)) {
Protocol_t[0] = command;
husky_lens_protocol_read_end();
return true;
}
else {
return false;
}
}
function writeAlgorithm(algorithmType : number,comemand = 0) {
protocolWriteOneInt16(algorithmType, comemand);
return wait(protocolCommand.COMMAND_RETURN_OK);
}
function writeLearn(algorithmType: number) {
protocolWriteOneInt16(algorithmType, protocolCommand.COMMAND_REQUEST_LEARN);
return wait(protocolCommand.COMMAND_RETURN_OK);
}
function protocolWriteOneInt16(algorithmType: number, command = 0) {
let buffer = husky_lens_protocol_write_begin(command);
husky_lens_protocol_write_int16(algorithmType);
let length = husky_lens_protocol_write_end();
let Buffer = pins.createBufferFromArray(buffer);
protocolWrite(Buffer);
}
function cycle_block(ID: number, index = 1): number {
let counter = 0;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_BLOCK && protocolPtr[i][5] == ID) {
counter++;
if (index == counter) return i;
}
}
return null;
}
function cycle_arrow(ID: number, index = 1): number {
let counter = 0;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_ARROW && protocolPtr[i][5] == ID) {
counter++;
if (index == counter) return i;
}
}
return null;
}
function readBlockCenterParameterDirect(): number {
let distanceMinIndex = -1;
let distanceMin = 65535;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_BLOCK) {
let distance = Math.round(Math.sqrt(Math.abs(protocolPtr[i][1] - 320 / 2))) + Math.round(Math.sqrt(Math.abs(protocolPtr[i][2] - 240 / 2)));
if (distance < distanceMin) {
distanceMin = distance;
distanceMinIndex = i;
}
}
}
return distanceMinIndex
}
function readArrowCenterParameterDirect(): number {
let distanceMinIndex = -1;
let distanceMin = 65535;
for (let i = 0; i < Protocol_t[1]; i++) {
if (protocolPtr[i][0] == protocolCommand.COMMAND_RETURN_ARROW) {
let distance = Math.round(Math.sqrt(Math.abs(protocolPtr[i][1] - 320 / 2))) + Math.round(Math.sqrt(Math.abs(protocolPtr[i][2] - 240 / 2)));
if (distance < distanceMin) {
distanceMin = distance;
distanceMinIndex = i;
}
}
}
return distanceMinIndex
}
function no():void
{
basic.showIcon(IconNames.No);
basic.pause(100);
basic.clearScreen();
basic.pause(100);
}
function yes():void
{
basic.showIcon(IconNames.Yes);
basic.pause(100);
basic.clearScreen();
}
}