forked from LOBOT-ROBOT/Startbit
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.ts
executable file
·1875 lines (1689 loc) · 57.5 KB
/
main.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
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
startbit package
*/
//% weight=10 icon="\uf013" color=#2896ff
namespace startbit {
export enum startbit_Colors {
//% block="Red"
Red = 0x01,
//% block="Green"
Green = 0x02,
//% block="Blue"
Blue = 0x03,
//% block="Black"
Black = 0x04,
//% block="White"
White = 0x05,
//% block="None"
None = 0x06
}
export enum startbit_lineFollower {
//% blockId="S1_OUT_S2_OUT" block="Sensor1 and sensor2 are out black line"
S1_OUT_S2_OUT = 0x00,
//% blockId="S1_OUT_S2_IN" block="Sensor2 in black line but sensor1 not"
S1_OUT_S2_IN = 0x01,
//% blockId="S1_IN_S2_OUT" block="Sensor1 in black line but sensor2 not"
S1_IN_S2_OUT = 0x02,
//% blockId="S1_IN_S2_IN" block="Sensor1 and sensor2 are in black line "
S1_IN_S2_IN = 0x03
}
export enum startbit_colorSensorPort {
//% block="Port 4"
port4 = 0x04
}
export enum startbit_Servos {
//% block="servo 1"
Servo1 = 0x01,
//% block="servo 2"
Servo2 = 0x02
}
export enum startbit_ultrasonicPort {
//% block="Port 1"
port1 = 0x01,
//% block="Port 2"
port2 = 0x02
}
export enum startbit_touchKeyPort {
//% block="Port 1"
port1 = 0x01,
//% block="Port 2"
port2 = 0x02,
//% block="Port 3"
port3 = 0x03
}
export enum startbit_lineFollowPort {
//% block="Port 1"
port1 = 0x01
}
export enum startbit_PinIOStatus {
//% block="Low"
Low = 0x00,
//% block="High"
Hight = 0x01
}
export enum startbit_LineFollowerSensor {
//% block="Sensor 1"
LFSensor_1 = 0x00,
//% block="Sensor 2"
LFSensor_2 = 0x01
}
export enum startbit_busServoPort {
//% block="Port 6"
port6 = 0x06
}
export enum startbit_knobPort {
//% block="Port 1"
port1 = 0x01
}
export enum startbit_photosensitivePort {
//% block="Port 1"
port1 = 0x01
}
export enum startbit_PhotosensitiveSensor {
//% block="Port 1"
port1 = 0x00
}
export enum startbit_fanPort {
//% block="Port 1"
port1,
//% block="Port 2"
port2
}
export enum startbit_servorange {
//% block="180"
range1 = 180,
//% block="270"
range2 =270
}
export enum startbit_digitaltubePort {
//% block="Port 1"
port1 = 0x01,
//% block="Port 2"
port2 = 0x02
}
export enum startbit_CmdType {
//% block="Invalid command"
NO_COMMAND = 0,
//% block="car run"
CAR_RUN = 1,
//% block="robot run"
ROBOT_RUN = 1,
//% block="Servo"
SERVO = 2,
//% block="Ultrasonic distance"
ULTRASONIC = 3,
//% block="Temperature"
TEMPERATURE = 4,
//% block="Sound"
SOUND = 5,
//% block="Light"
LIGHT = 6,
//% block="Voltage"
BAT = 7,
//% block="Rgb light"
RGB_LIGHT = 8,
//% block="Honk horn"
DIDI = 9,
//% block="Read firmware version"
VERSION = 10,
//% block="Read angle"
READ_ANGLE = 11,
//% block="Light belt"
RGB_BELT = 12,
//% block="WIFI mode"
WIFI_MODE = 13,
//% block="Get mac"
GET_MAC = 14,
//% block="Get hand cmd"
GET_HAND_CMD = 15
}
export enum startbit_CarRunCmdType {
//% block="Stop"
STOP = 0,
//% block="Go ahead"
GO_AHEAD,
//% block="Back"
GO_BACK,
//% block="Turn left"
TURN_LEFT,
//% block="Turn right"
TURN_RIGHT,
//% block="Go ahead slowly"
GO_AHEAD_SLOW,
//% block="Turn left slowly"
TURN_LEFT_SLOW,
//% block="Turn right slowly"
TURN_RIGHT_SLOW,
//% block="Invalid command"
COMMAND_ERRO
}
/**
* Startbit initialization, please execute at boot time
*/
//% weight=100 blockId=startbit_Init block="Initialize Startbit"
export function startbit_Init() {
startbit_initRGBLight();
serial.redirect(
SerialPin.P12,
SerialPin.P8,
BaudRate.BaudRate115200);
basic.forever(() => {
getHandleCmd();
});
basic.pause(2000);
}
let handleCmd: string = "";
let currentVoltage: number = 0;
let volume: number = 0;
let lhRGBLight: StartbitRGBLight.LHstartbitRGBLight;
let lhRGBLightBelt: StartbitRGBLight.LHstartbitRGBLight;
let P14_ad = 0;
let MESSAGE_MAC = 0xff;
let MESSAGE_ANGLE = 0x100;
let i2cPortValid: boolean = true;
let connectStatus: boolean = false;
let servo1Angle: number = 0xfff;
let servo2Angle: number = 0xfff;
let macStr: string = "";
let actiongroup_finished = true;
let Digitaltube:startbit_TM1640LEDs
let TM1640_CMD1 = 0x40;
let TM1640_CMD2 = 0xC0;
let TM1640_CMD3 = 0x80;
let _SEGMENTS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71];
/**
* Get the handle command.
*/
function getHandleCmd() {
let charStr: string = serial.readString();
handleCmd = handleCmd.concat(charStr);
let cnt: number = countChar(handleCmd, "$");
if (cnt == 0)
return;
let index = findIndexof(handleCmd, "$", 0);
if (index != -1) {
let cmd: string = handleCmd.substr(0, index);
if (cmd.charAt(0).compare("A") == 0) {
if (cmd.length == 7) {
let arg1Int: number = strToNumber(cmd.substr(1, 2));
let arg2Int: number = strToNumber(cmd.substr(3, 2));
let arg3Int: number = strToNumber(cmd.substr(5, 2));
P14_ad = arg1Int;
if (arg3Int != -1) {
currentVoltage = arg3Int * 10353 / 400;
currentVoltage = Math.round(currentVoltage);
}
if (arg2Int != -1) {
volume = arg2Int;
}
} else if (cmd.length == 5) {
actiongroup_finished = true;
} else {
}
}
if (cmd.charAt(0).compare("C") == 0 && cmd.length == 11) {
if (lhRGBLightBelt != null) {
for (let i = 0; i < 10; i++) {
let color = converOneChar(cmd.charAt(i + 1));
if (color != -1)
lhRGBLightBelt.setPixelColor(i, color);
}
lhRGBLightBelt.show();
}
}
if (cmd.charAt(0).compare("M") == 0 && cmd.length == 18) {
macStr = cmd.substr(1, 17);
control.raiseEvent(MESSAGE_MAC, 1);
}
if (cmd.compare("WIFI_S_CONNECT") == 0) {
connectStatus = true;
}
if (cmd.compare("WIFI_S_DISCONNECT") == 0) {
connectStatus = false;
}
if (cmd.charAt(0).compare("S") == 0 && cmd.length == 5) {
let arg1Int: number = strToNumber(cmd.substr(1, 1));
let arg2Str = cmd.substr(2, 3);
if (arg2Str.compare("XXX") == 0) {
return;
}
let arg2Int: number = 0;
if (arg2Str.charAt(0).compare("F") != 0) {
arg2Int = strToNumber(arg2Str);
}
if (arg2Int > 1000)
arg2Int = 1000;
if (arg1Int == 1) {
servo1Angle = mapRGB(arg2Int, 0, 1000, 0, 240);
servo1Angle -= 120;
control.raiseEvent(MESSAGE_ANGLE, 1);
}
else if (arg1Int == 2) {
servo2Angle = mapRGB(arg2Int, 0, 1000, 0, 240);
servo2Angle -= 120;
control.raiseEvent(MESSAGE_ANGLE, 2);
}
}
}
handleCmd = "";
}
function checkADPortValue(value: number): number {
if (value == -1)
return 2;
if (value <= 0x2E)
return 0;
else if (value >= 0xAA)
return 1;
else
return 2;//未识别电平状态
}
function findIndexof(src: string, strFind: string, startIndex: number): number {
for (let i = startIndex; i < src.length; i++) {
if (src.charAt(i).compare(strFind) == 0) {
return i;
}
}
return -1;
}
function countChar(src: string, strFind: string): number {
let cnt: number = 0;
for (let i = 0; i < src.length; i++) {
if (src.charAt(i).compare(strFind) == 0) {
cnt++;
}
}
return cnt;
}
function strToNumber(str: string): number {
let num: number = 0;
for (let i = 0; i < str.length; i++) {
let tmp: number = converOneChar(str.charAt(i));
if (tmp == -1)
return -1;
if (i > 0)
num *= 16;
num += tmp;
}
return num;
}
function decStrToNumber(str: string): number {
let num: number = 0;
for (let i = 0; i < str.length; i++) {
let tmp: number = converOneChar(str.charAt(i));
if (tmp == -1)
return -1;
if (i > 0)
num *= 10;
num += tmp;
}
return num;
}
function converOneChar(str: string): number {
if (str.compare("0") >= 0 && str.compare("9") <= 0) {
return parseInt(str);
}
else if (str.compare("A") >= 0 && str.compare("F") <= 0) {
if (str.compare("A") == 0) {
return 10;
}
else if (str.compare("B") == 0) {
return 11;
}
else if (str.compare("C") == 0) {
return 12;
}
else if (str.compare("D") == 0) {
return 13;
}
else if (str.compare("E") == 0) {
return 14;
}
else if (str.compare("F") == 0) {
return 15;
}
return -1;
}
else
return -1;
}
/**
* Set the angle of servo 1 to 8, range of 0~270 degree
*/
//% weight=100 blockId=setServo block="Set pwm servo range|range %range|index %index|angle %angle|duration %duration"
//% angle.min=0 angle.max=270
//% inlineInputMode=inline
//% subcategory=Servo
export function setServo(range:startbit_servorange, index: number = 1, angle: number, duration: number = 300) {
let position = mapRGB(angle, 0, range, 500, 2500);
let buf = pins.createBuffer(10);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x08;
buf[3] = 0x03;//cmd type
buf[4] = 0x01;
buf[5] = duration & 0xff;
buf[6] = (duration >> 8) & 0xff;
buf[7] = index;
buf[8] = position & 0xff;
buf[9] = (position >> 8) & 0xff;
serial.writeBuffer(buf);
}
/**
* Set the servo controller to run a actiongroup
* @param times Running times. eg: 1
*/
//% weight=94 blockId=startbit_runActionGroup block="Run ActionGroup|index %index|times %times"
//% subcategory=Servo
export function startbit_runActionGroup(index: number, times: number = 1) {
let buf = pins.createBuffer(7);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x05;
buf[3] = 0x06;//cmd type CMD_ACTION_GROUP_RUN
buf[4] = index & 0xff;
buf[5] = times & 0xff;
buf[6] = (times >> 8) & 0xff;
actiongroup_finished = false;
serial.writeBuffer(buf);
}
/**
* Stop running actiongroup
*/
//% weight=92 blockId=startbit_stopnActionGroup block="Stop ActionGroup"
//% subcategory=Servo
export function startbit_stopActionGroup() {
let buf = pins.createBuffer(7);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x02;
buf[3] = 0x07;//cmd type CMD_ACTION_GROUP_STOP
actiongroup_finished = false;
serial.writeBuffer(buf);
}
/**
* Wait for Actiongroup Finishing
*/
//% weight=93 blockId=startbit_actionRunover block="Action run over"
//% subcategory=Servo
export function startbit_actionRunover(): boolean {
// let ret = false;
if (actiongroup_finished == true) {
// ret = true;
actiongroup_finished = true;
}
else {
actiongroup_finished = false;
}
return actiongroup_finished;
}
/**
* Send read startbit servos angle command
*/
//% weight=99 blockId=startbit_readAngle block="Send |%servo|angle command "
//% subcategory=Servo
export function startbit_readAngle(servo: startbit_Servos) {
let buf = pins.createBuffer(6);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x04;
buf[3] = 0x3E;//cmd type
buf[4] = 0x05;
buf[5] = servo;
serial.writeBuffer(buf);
}
/**
* Do someting when Startbit receive angle
* @param body code to run when event is raised
*/
//% weight=97 blockId=onStartbit_getAngle blockGap=50 block="on Startbit|%servo|get angle"
//% subcategory=Servo
export function onStartbit_getAngle(servo: startbit_Servos, body: Action) {
control.onEvent(MESSAGE_ANGLE, servo, body);
}
/**
* Get servos angle
*/
//% weight=98 blockId=getServosAngle block="Get|%servo|angle(-120~120)"
//% subcategory=Servo
export function getServosAngle(servo: startbit_Servos): number {
if (servo == startbit_Servos.Servo1) {
return servo1Angle;
}
else if (servo == startbit_Servos.Servo2) {
return servo2Angle;
}
else
return 0xFFF;
}
/**
* Send robot attitude to the servo controller
* @param pitch eg: 0
* @param roll eg: 0
*/
//% weight=91 blockId=startbit_sendAttitude block="Send pitch|%pitch|and roll|%roll"
/*
export function startbit_sendAttitude(pitch: number, roll: number) {
pitch < -90 ? -90 : pitch;
pitch > 90 ? 90 : pitch;
roll < -90 ? -90 : roll;
roll > 90 ? 90 : roll;
let buf = pins.createBuffer(6);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x04;
buf[3] = 0x5A;
buf[4] = pitch;
buf[5] = roll;
serial.writeBuffer(buf);
}
*/
/**
* Set the speed of the number 1 motor and number 2 motor, range of -100~100, that can control the tank to go advance or turn of.
*/
//% weight=96 blockId=startbit_setMotorSpeed block="Set motor1 speed(-100~100)|%speed1|and motor2|speed %speed2"
//% speed1.min=-100 speed1.max=100
//% speed2.min=-100 speed2.max=100
//% subcategory=Servo
export function startbit_setMotorSpeed(speed1: number, speed2: number) {
if (speed1 > 100 || speed1 < -100 || speed2 > 100 || speed2 < -100) {
return;
}
speed1 = speed1 * -1;
speed2 = speed2 * -1;
let buf = pins.createBuffer(6);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x04;
buf[3] = 0x32;//cmd type
buf[4] = speed1;
buf[5] = speed2;
serial.writeBuffer(buf);
}
/**
* Set the speed of the fan, range of -100~100.
*/
//% weight=95 blockId=startbit_setFanSpeed blockGap=50 block="Set |%port fan speed(-100~100)|%speed1"
//% speed1.min=-100 speed1.max=100
//% subcategory=Servo
export function startbit_setFanSpeed(port: startbit_fanPort, speed1: number) {
if (speed1 > 100 || speed1 < -100) {
return;
}
let pin1 = AnalogPin.P1;
let pin2 = AnalogPin.P2;
if (port == startbit_fanPort.port2) {
pin1 = AnalogPin.P13;
pin2 = AnalogPin.P14;
}
if (speed1 < 0) {
pins.analogWritePin(pin2, 0);
pins.analogWritePin(pin1, pins.map(-speed1, 0, 100, 0, 1023));
}
else if (speed1 > 0) {
pins.analogWritePin(pin1, 0);
pins.analogWritePin(pin2, pins.map(speed1, 0, 100, 0, 1023));
}
else {
pins.analogWritePin(pin2, 0);
pins.analogWritePin(pin1, 0);
}
}
/**
* Get the volume level detected by the sound sensor, range 0 to 255
*/
//% weight=94 blockId=startbit_getSoundVolume block="Sound volume"
//% subcategory=Sensor
export function startbit_getSoundVolume(): number {
return volume;
}
/**
* Get startbit current voltage,the unit is mV
*/
//% weight=93 blockGap=50 blockId=startbit_getBatVoltage block="Get startbit current voltage (mV)"
//% subcategory=Sensor
export function startbit_getBatVoltage(): number {
return currentVoltage;
}
/**
* TM1640 LED display
*/
export class startbit_TM1640LEDs {
buf: Buffer;
clk: DigitalPin;
dio: DigitalPin;
_ON: number;
brightness: number;
count: number; // number of LEDs
/**
* initial TM1640
*/
init(): void {
pins.digitalWritePin(this.clk, 0);
pins.digitalWritePin(this.dio, 0);
this._ON = 8;
this.buf = pins.createBuffer(this.count);
this.clear();
}
/**
* Start
*/
_start() {
pins.digitalWritePin(this.dio, 0);
pins.digitalWritePin(this.clk, 0);
}
/**
* Stop
*/
_stop() {
pins.digitalWritePin(this.dio, 0);
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.dio, 1);
}
/**
* send command1
*/
_write_data_cmd() {
this._start();
this._write_byte(TM1640_CMD1);
this._stop();
}
/**
* send command3
*/
_write_dsp_ctrl() {
this._start();
this._write_byte(TM1640_CMD3 | this._ON | this.brightness);
this._stop();
}
/**
* send a byte to 2-wire interface
*/
_write_byte(b: number) {
for (let i = 0; i < 8; i++) {
pins.digitalWritePin(this.clk, 0);
pins.digitalWritePin(this.dio, (b >> i) & 1);
pins.digitalWritePin(this.clk, 1);
}
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.clk, 0);
}
intensity(val: number = 7) {
if (val < 1) {
this.off();
return;
}
if (val > 8) val = 8;
this._ON = 8;
this.brightness = val - 1;
this._write_data_cmd();
this._write_dsp_ctrl();
}
/**
* set data to TM1640, with given bit
*/
_dat(bit: number, dat: number) {
this._write_data_cmd();
this._start();
this._write_byte(TM1640_CMD2 | (bit % this.count))
this._write_byte(dat);
this._stop();
this._write_dsp_ctrl();
}
showbit(num: number = 5, bit: number = 0) {
this.buf[bit % this.count] = _SEGMENTS[num % 16]
this._dat(bit, _SEGMENTS[num % 16])
}
showNumber(num: number) {
if (num < 0) {
this._dat(0, 0x40) // '-'
num = -num
}
else
this.showbit(Math.idiv(num, 1000) % 10)
this.showbit(num % 10, 3)
this.showbit(Math.idiv(num, 10) % 10, 2)
this.showbit(Math.idiv(num, 100) % 10, 1)
}
showHex(num: number) {
if (num < 0) {
this._dat(0, 0x40) // '-'
num = -num
}
else
this.showbit((num >> 12) % 16)
this.showbit(num % 16, 3)
this.showbit((num >> 4) % 16, 2)
this.showbit((num >> 8) % 16, 1)
}
showDP(bit: number = 1, show: boolean = true) {
bit = bit % this.count
if (show) this._dat(bit, this.buf[bit] | 0x80)
else this._dat(bit, this.buf[bit] & 0x7F)
}
clear() {
for (let i = 0; i < this.count; i++) {
this._dat(i, 0)
this.buf[i] = 0
}
}
on() {
this._ON = 8;
this._write_data_cmd();
this._write_dsp_ctrl();
}
off() {
this._ON = 0;
this._write_data_cmd();
this._write_dsp_ctrl();
}
}
/**
* 创建 TM1640 对象.
* @param clk the CLK pin for TM1640, eg: DigitalPin.P1
* @param dio the DIO pin for TM1640, eg: DigitalPin.P2
* @param intensity the brightness of the LED, eg: 7
* @param count the count of the LED, eg: 4
*/
function startbit_TM1640create(port: startbit_digitaltubePort, intensity: number, count: number): startbit_TM1640LEDs {
let digitaltube = new startbit_TM1640LEDs();
switch (port) {
case startbit_digitaltubePort.port1:
digitaltube.clk = DigitalPin.P2;
digitaltube.dio = DigitalPin.P1;
break;
case startbit_digitaltubePort.port2:
digitaltube.clk = DigitalPin.P14;
digitaltube.dio = DigitalPin.P13;
break;
}
if ((count < 1) || (count > 5)) count = 4;
digitaltube.count = count;
digitaltube.brightness = intensity;
digitaltube.init();
return digitaltube;
}
/**
* @param clk the CLK pin for TM1640, eg: DigitalPin.P1
* @param dio the DIO pin for TM1640, eg: DigitalPin.P2
* @param intensity the brightness of the LED, eg: 7
* @param count the count of the LED, eg: 4
*/
//% weight=96 blockId=startbit_digitaltube block="digitaltube|%port|intensity %intensity|LED count %count"
export function startbit_digitaltube(port: startbit_digitaltubePort, intensity: number, count: number) {
Digitaltube = startbit_TM1640create(port, intensity, count);
}
/**
* show a number.
* @param num is a number, eg: 0
*/
//% weight=91 blockId=startbit_showNumber block="digitaltube show number| %num"
//% subcategory=LED
export function startbit_showNumber(num: number) {
Digitaltube.showNumber(num);
}
/**
* show a number in given position.
* @param num number will show, eg: 5
* @param bit the position of the LED, eg: 0
*/
//% weight=89 blockId=startbit_showbit block="digitaltube show digit| %num|at %bit"
//% subcategory=LED
export function startbit_showbit(num: number = 5, bit: number = 0) {
Digitaltube.showbit(num, bit);
}
/**
* show a hex number.
* @param num is a hex number, eg: 0
*/
//% weight=90 blockId=startbit_showhex block="digitaltube show hex number| %num"
//% subcategory=LED
export function startbit_showhex(num: number) {
Digitaltube.showHex(num);
}
/**
* show or hide dot point.
* @param bit is the position, eg: 1
* @param show is show/hide dp, eg: true
*/
//% weight=88 blockId=startbit_showDP block="digitaltube DotPoint at| %bit|show %show"
//% subcategory=LED
export function startbit_showDP(bit: number = 1, show: boolean = true) {
Digitaltube.showDP(bit, show);
}
/**
* set TM1640 intensity, range is [0-8], 0 is off.
* @param val the brightness of the TM1640, eg: 7
*/
//% weight=92 blockId=startbit_intensity block=" digitaltube set intensity %val"
//% subcategory=LED
export function startbit_intensity(val: number = 7) {
Digitaltube.intensity(val);
}
/**
* turn off LED.
*/
//% weight=86 blockId=startbit_off block="turn off digitaltube"
//% subcategory=LED
export function startbit_off() {
Digitaltube.off();
}
/**
* turn on LED.
*/
//% weight=87 blockId=startbit_on block="turn on digitaltube"
//% subcategory=LED
export function startbit_on() {
Digitaltube.on();
}
/**
* clear LED.
*/
//%weight=85 blockId=startbit_clear blockGap=50 block="clear digitaltube"
//% subcategory=LED
export function startbit_clear() {
Digitaltube.clear();
}
const APDS9960_I2C_ADDR = 0x39;
const APDS9960_ID_1 = 0xA8;
const APDS9960_ID_2 = 0x9C;
/* APDS-9960 register addresses */
const APDS9960_ENABLE = 0x80;
const APDS9960_ATIME = 0x81;
const APDS9960_WTIME = 0x83;
const APDS9960_AILTL = 0x84;
const APDS9960_AILTH = 0x85;
const APDS9960_AIHTL = 0x86;
const APDS9960_AIHTH = 0x87;
const APDS9960_PERS = 0x8C;
const APDS9960_CONFIG1 = 0x8D;
const APDS9960_PPULSE = 0x8E;
const APDS9960_CONTROL = 0x8F;
const APDS9960_CONFIG2 = 0x90;
const APDS9960_ID = 0x92;
const APDS9960_STATUS = 0x93;
const APDS9960_CDATAL = 0x94;
const APDS9960_CDATAH = 0x95;
const APDS9960_RDATAL = 0x96;
const APDS9960_RDATAH = 0x97;
const APDS9960_GDATAL = 0x98;
const APDS9960_GDATAH = 0x99;
const APDS9960_BDATAL = 0x9A;
const APDS9960_BDATAH = 0x9B;
const APDS9960_POFFSET_UR = 0x9D;
const APDS9960_POFFSET_DL = 0x9E;
const APDS9960_CONFIG3 = 0x9F;
const APDS9960_GCONF4 = 0xAB;
const APDS9960_AICLEAR = 0xE7;
/* LED Drive values */
const LED_DRIVE_100MA = 0;
/* ALS Gain (AGAIN) values */
const AGAIN_4X = 1;
/* Default values */
const DEFAULT_ATIME = 219; // 103ms
const DEFAULT_WTIME = 246; // 27ms
const DEFAULT_PROX_PPULSE = 0x87; // 16us, 8 pulses
const DEFAULT_POFFSET_UR = 0; // 0 offset
const DEFAULT_POFFSET_DL = 0; // 0 offset
const DEFAULT_CONFIG1 = 0x60; // No 12x wait (WTIME) factor
const DEFAULT_AILT = 0xFFFF; // Force interrupt for calibration
const DEFAULT_AIHT = 0;
const DEFAULT_PERS = 0x11; // 2 consecutive prox or ALS for int.
const DEFAULT_CONFIG2 = 0x01; // No saturation interrupts or LED boost
const DEFAULT_CONFIG3 = 0; // Enable all photodiodes, no SAI
const DEFAULT_LDRIVE = LED_DRIVE_100MA;
const DEFAULT_AGAIN = AGAIN_4X;
const OFF = 0;
const POWER = 0;
const AMBIENT_LIGHT = 1;
const ALL = 7;
const red_wb = 2130;
const green_wb = 3500;
const blue_wb = 4620;
function i2cwrite(reg: number, value: number) {
let buf = pins.createBuffer(2);
buf[0] = reg;
buf[1] = value;
pins.i2cWriteBuffer(APDS9960_I2C_ADDR, buf);
}
function i2cread(reg: number): number {
pins.i2cWriteNumber(APDS9960_I2C_ADDR, reg, NumberFormat.UInt8BE);
let val = pins.i2cReadNumber(APDS9960_I2C_ADDR, NumberFormat.UInt8BE);
return val;
}
function InitColor(): boolean {
let id = i2cread(APDS9960_ID);
// serial.writeLine("id:")
// serial.writeNumber(id);
// if (!(id == APDS9960_ID_1 || id == APDS9960_ID_2)) {
// return false;
// }
// serial.writeLine("set mode:")
setMode(ALL, OFF);
i2cwrite(APDS9960_ATIME, DEFAULT_ATIME);
i2cwrite(APDS9960_WTIME, DEFAULT_WTIME);
i2cwrite(APDS9960_PPULSE, DEFAULT_PROX_PPULSE);
i2cwrite(APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR);
i2cwrite(APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL);
i2cwrite(APDS9960_CONFIG1, DEFAULT_CONFIG1);
setLEDDrive(DEFAULT_LDRIVE);
setAmbientLightGain(DEFAULT_AGAIN);
setLightIntLowThreshold(DEFAULT_AILT);
setLightIntHighThreshold(DEFAULT_AIHT);
i2cwrite(APDS9960_PERS, DEFAULT_PERS);
i2cwrite(APDS9960_CONFIG2, DEFAULT_CONFIG2);
i2cwrite(APDS9960_CONFIG3, DEFAULT_CONFIG3);
return true;
}
function setLEDDrive(drive: number) {
let val = i2cread(APDS9960_CONTROL);
/* Set bits in register to given value */
drive &= 0b00000011;
drive = drive << 6;
val &= 0b00111111;
val |= drive;
i2cwrite(APDS9960_CONTROL, val);
}
function setLightIntLowThreshold(threshold: number) {
let val_low = threshold & 0x00FF;
let val_high = (threshold & 0xFF00) >> 8;
i2cwrite(APDS9960_AILTL, val_low);
i2cwrite(APDS9960_AILTH, val_high);
}