-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.ts
2037 lines (1831 loc) · 59.8 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
enum PingUnit {
//% block="cm"
Centimeters,
//% block="inches"
Inches
}
enum RgbColors {
//% block=red
Red = 0xFF0000,
//% block=orange
Orange = 0xFFA500,
//% block=yellow
Yellow = 0xFFFF00,
//% block=green
Green = 0x00FF00,
//% block=blue
Blue = 0x0000FF,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xFF00FF,
//% block=white
White = 0xFFFFFF,
//% block=black
Black = 0x000000
}
enum RgbUltrasonics {
//% block=left
Left = 0x00,
//% block=right
Right = 0x01,
//% block=all
All = 0x02
}
enum ColorEffect {
//% block=none
None = 0x00,
//% block=breathing
Breathing = 0x01,
//% block=rotate
Rotate = 0x02,
//% block=flash
Flash = 0x03
}
enum rgb_ColorEffect {
//% block=none
None = 0x00,
//% block=breathing
Breathing = 0x01,
//% block=flash
Flash = 0x03
}
enum EM_DHT11Type {
//% block="temperature(℃)"
EM_DHT11_temperature_C = 0,
//% block="temperature(℉)"
EM_DHT11_temperature_F = 1,
//% block="humidity(0~100)"
EM_DHT11_humidity = 2
}
enum _selectpin {
//% block="Apin"
Apin = 0,
//% block="Bpin"
Bpin = 1,
//% block="Dpin"
Dpin = 2,
}
enum waterpin {
P0,
P1,
P2,
P3,
P4,
P10,
}
enum _rockerpin {
//% block="Xpin"
Xpin = 0,
//% block="Ypin"
Ypin = 1
}
enum ledon_off {
//% block="on"
_on = 1,
//% block="off"
_off = 0,
}
enum on_off {
//% block="on"
_on = 1,
//% block="off"
_off = 0,
}
enum _selectlight {
//% block="_yellow"
_yellow = 0,
//% block="_red"
_red = 1,
//% block="_green"
_green = 2,
}
enum _selectcolor {
//% block="_blue"
_blue = 0,
//% block="_red"
_red = 1,
//% block="_green"
_green = 2,
}
enum run_turn {
//% block="forward"
forward = 0,
//% block="reverse"
reverse = 1,
}
enum LcdBacklight {
//% block="on"
_on = 1,
//% block="off"
_off = 0,
}
enum Item {
//% block="on"
_on = 1,
//% block="off"
_off = 2,
//% block="clear"
_clear = 3,
}
enum Select {
//% block="on"
_on = 0,
//% block="off"
_off = 1,
//% block="clear"
_clear = 2,
}
enum barb_fitting {
//% block="LEFT"
BUTOON_LEFT = 0,
//% block="RIGHT"
BUTOON_RIGHT = 1,
//% block="UP"
BUTOON_UP = 2,
//% block="DOWN"
BUTOON_DOWN = 3,
//% block="BUTTON"
JOYSTICK_BUTTON = 4,
}
enum key_status {
//% block="DOWN"
PRESS_DOWN = 0, //按下
//% block="UP"
PRESS_UP = 1, //释放
//% block="CLICK1"
SINGLE_CLICK = 3, //单击
//% block="CLICK2"
DOUBLE_CLICK = 4, //双击
//% block="HOLD"
LONG_PRESS_HOLD = 6, //长按
//% block="PRESS"
NONE_PRESS = 8, //未按
}
enum Shaft{
//% block="X"
X_Shaft = 0,
//% block="Y"
Y_Shaft = 1,
}
enum Mode {
//% block="LOOP"
LOOP_MODE = 0, // 循环模式
//% block="BUTTON"
BUTTON_MODE = 1, // 按键模式
//% block="KEYWORDS"
KEYWORDS_MODE = 2, // 关键字模式
//% block="KEYWORDS_AND"
KEYWORDS_AND_BUTTON = 3, //关键字加按键模式
}
//% color="#FFA500" weight=10 icon="\uf2c9" block="Sensor:bit"
namespace sensors {
//% blockId=touchbutton block="touch |digital pin %pin" group="触摸模块"
//% weight=70
//% subcategory="基础输入模块"
export function touchButton(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return true;
} else {
return false;
}
}
//% blockId=button block="Button |digital pin %pin" group="按键模块"
//% weight=70
//% subcategory="基础输入模块"
export function Button(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return false;
} else {
return true;
}
}
//% blockId=crashbutton block="crashButton |digital pin %pin" group="触碰模块"
//% weight=70
//% subcategory="基础输入模块"
export function crashButton(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return false;
} else {
return true;
}
}
//% blockId=slideRheostat block="slideRheostat |analog pin %pin" group="滑动变阻器模块"
//% weight=70
//% subcategory="基础输入模块"
export function slideRheostat(pin: AnalogPin): number {
let row = pins.analogReadPin(pin)
return row
}
//% blockId=rotaryPotentiometer block="rotaryPotentiometer |analog pin %pin" group="旋转电位器模块"
//% weight=70
//% subcategory="基础输入模块"
export function rotaryPotentiometer(pin: AnalogPin): number {
let row = pins.analogReadPin(pin)
return row
}
let _SDO = 0
let _SCL = 0
/**
*
* @param SDO SDO eg: DigitalPin.P13
* @param SCL SCL eg: DigitalPin.P14
*/
//% blockId=actuator_keyborad_pin block="actuator_keyborad_pin|SDOPIN %SDO|SCLPIN %SCL" group="矩阵键盘模块"
//% weight=71
//% subcategory="基础输入模块"
export function actuator_keyborad_pin(SDO: DigitalPin, SCL: DigitalPin): void {
_SDO = SDO
_SCL = SCL
}
//% blockId=actuator_keyborad_read block="actuator_keyborad_read" group="矩阵键盘模块"
//% weight=70
//% subcategory="基础输入模块"
export function actuator_keyborad_read(): string {
let DATA = 0
pins.digitalWritePin(_SDO, 1)
control.waitMicros(93)
pins.digitalWritePin(_SDO, 0)
control.waitMicros(10)
for (let i = 0; i < 16; i++) {
pins.digitalWritePin(_SCL, 1)
pins.digitalWritePin(_SCL, 0)
DATA |= pins.digitalReadPin(_SDO) << i
}
control.waitMicros(2 * 1000)
// serial.writeString('' + DATA + '\n');
switch (DATA & 0xFFFF) {
case 0xFFFE: return "1"
case 0xFFFD: return "2"
case 0xFFFB: return "3"
case 0xFFEF: return "4"
case 0xFFDF: return "5"
case 0xFFBF: return "6"
case 0xFEFF: return "7"
case 0xFDFF: return "8"
case 0xFBFF: return "9"
case 0xDFFF: return "0"
case 0xFFF7: return "A"
case 0xFF7F: return "B"
case 0xF7FF: return "C"
case 0x7FFF: return "D"
case 0xEFFF: return "*"
case 0xBFFF: return "#"
default: return " "
}
}
let Xpin = 0
let Ypin = 0
let Bpin = 0
/**
*
* @param pinx eg: AnalogPin.P0
* @param piny eg: AnalogPin.P1
* @param pinb eg: DigitalPin.P8
*/
//% blockId=rockerPin block="rockerPin setup | pinX %pinx|pinY %piny|pinB %pinb" group="摇杆模块"
//% weight=70
//% subcategory="基础输入模块"
export function rockerPin(pinx: AnalogPin, piny: AnalogPin, pinb: DigitalPin): void {
Xpin = pinx
Ypin = piny
Bpin = pinb
}
//% blockId=_analogRead block="select analog pin %selectpin" group="摇杆模块"
//% weight=69
//% subcategory="基础输入模块"
export function _analogRead(selectpin: _rockerpin): number {
let a
if (selectpin == 0)
a = Xpin
else if (selectpin == 1)
a = Ypin
return pins.analogReadPin(a)
}
//% blockId=_digitalRead block="Is the rocker module pressed?" group="摇杆模块"
//% weight=68
//% subcategory="基础输入模块"
export function _digitalRead(): boolean {
// pins.digitalWritePin(Bpin, 0)
if (pins.digitalReadPin(Bpin) == 1) {
return false;
} else {
return true;
}
}
let _DIO = 0
let _CLK = 0
/**
*
* @param DIO eg: DigitalPin.P13
* @param CLK eg: DigitalPin.P14
*/
//% blockId=basic_piano_pin block="basic_piano_pin |DIO pin %DIO|CLK pin %CLK" group="触摸钢琴模块 V1"
//% weight=70
//% subcategory="基础输入模块"
export function basic_piano_pin(DIO: DigitalPin, CLK: DigitalPin): void {
_DIO = DIO
_CLK = CLK
}
//% blockId=basic_piano_play block="basic_piano_play" group="触摸钢琴模块 V1"
//% weight=69
//% subcategory="基础输入模块"
export function basic_piano_play(): void {
if (0 == pins.digitalReadPin(_DIO)) {
let list: number[] = []
for (let index = 0; index <= 15; index++) {
for (let index2 = 0; index2 < 4; index2++) {
pins.digitalWritePin(_CLK, 0)
}
for (let index2 = 0; index2 < 4; index2++) {
pins.digitalWritePin(_CLK, 1)
}
list[index] = pins.digitalReadPin(_DIO)
}
if (!(list[0])) {
music.playTone(262, music.beat(BeatFraction.Half))
} else if (!(list[1])) {
music.playTone(294, music.beat(BeatFraction.Half))
} else if (!(list[2])) {
music.playTone(330, music.beat(BeatFraction.Half))
} else if (!(list[3])) {
music.playTone(349, music.beat(BeatFraction.Half))
} else if (!(list[4])) {
music.playTone(392, music.beat(BeatFraction.Half))
} else if (!(list[5])) {
music.playTone(440, music.beat(BeatFraction.Half))
} else if (!(list[6])) {
music.playTone(494, music.beat(BeatFraction.Half))
} else if (!(list[7])) {
music.playTone(523, music.beat(BeatFraction.Half))
}
}
}
let _pianoDIO = 0
let _pianoCLK = 0
/**
*
* @param pianoDIO eg: DigitalPin.P13
* @param pianoCLK eg: DigitalPin.P14
*/
//% blockId=piano_v2_init block="piano_v2_init|DIO %pianoDIO|CLK %pianoCLK" group="触摸钢琴模块 V2"
//% weight=61
//% subcategory="基础输入模块"
export function piano_v2_init(pianoDIO: DigitalPin, pianoCLK: DigitalPin): void {
_pianoDIO = pianoDIO
_pianoCLK = pianoCLK
}
//% blockId=piano_v2_play block="piano_v2_read" group="触摸钢琴模块 V2"
//% weight=60
//% subcategory="基础输入模块"
export function piano_v2_play(): void {
let DATA = 0
pins.digitalWritePin(_pianoDIO, 1)
control.waitMicros(93)
pins.digitalWritePin(_pianoDIO, 0)
control.waitMicros(10)
for (let i = 0; i < 8; i++) {
pins.digitalWritePin(_pianoCLK, 1)
pins.digitalWritePin(_pianoCLK, 0)
DATA |= pins.digitalReadPin(_pianoDIO) << i
}
control.waitMicros(2 * 1000)
// serial.writeString('' + DATA + '\n');
switch (DATA & 0xFF) {
case 0xFE: music.playTone(262, music.beat(BeatFraction.Half)); break;
case 0xFD: music.playTone(294, music.beat(BeatFraction.Half)); break;
case 0xFB: music.playTone(330, music.beat(BeatFraction.Half)); break;
case 0xF7: music.playTone(349, music.beat(BeatFraction.Half)); break;
case 0xEF: music.playTone(392, music.beat(BeatFraction.Half)); break;
case 0xDF: music.playTone(440, music.beat(BeatFraction.Half)); break;
case 0xBF: music.playTone(494, music.beat(BeatFraction.Half)); break;
case 0x7F: music.playTone(523, music.beat(BeatFraction.Half)); break;
// default: return " "
}
}
/**
* 游戏手柄
*/
//% blockId=Gamepad_Press block="Gamepad buttons %button Is pressed?" group="PH2.0手柄"
//% weight=74
//% subcategory="基础输入模块"
//% inlineInputMode=inline
export function Gamepad_Press(button: barb_fitting): boolean {
if(Get_Button_Status(button) != NONE_PRESS && Get_Button_Status(button) != 0xff)
{
return true;
}
return false;
}
/**
* PH2.0手柄
*/
//% blockId=Gamepad_Release block="Gamepad buttons %button Is Released?" group="PH2.0手柄"
//% weight=74
//% subcategory="基础输入模块"
//% inlineInputMode=inline
export function Gamepad_Release(button: barb_fitting): boolean {
if(Get_Button_Status(button) == NONE_PRESS)
{
return true;
}
return false;
}
/**
* PH2.0手柄
*/
//% blockId=Gamepad_Shaft block="Game controller acquisition %shaft the value of" group="PH2.0手柄"
//% weight=74
//% subcategory="基础输入模块"
//% inlineInputMode=inline
export function Gamepad_Shaft(shaft: Shaft): number {
let value = 0;
if(shaft == 0){
value = i2cread(JOYSTICK_I2C_ADDR,JOYSTICK_LEFT_X_REG);
}
if(shaft == 1){
value = i2cread(JOYSTICK_I2C_ADDR,JOYSTICK_LEFT_Y_REG);
}
return value;
}
/**
* PH2.0手柄
*/
//% blockId=Gamepad_Status block="Button %button is it %status status?" group="PH2.0手柄"
//% weight=74
//% subcategory="基础输入模块"
//% inlineInputMode=inline
export function Gamepad_Status(button : barb_fitting,status : key_status): boolean {
if(Get_Button_Status(button) == status)
{
return true;
}else{
return false;
}
}
let VOICE_RESET_REG = 0x5;
let VOICE_IIC_ADDR = 0x79;
let VOICE_ADD_WORDS_REG = 0x04;
let VOICE_ASR_START_REG = 0x6;
let VOICE_RESULT_REG = 0;
let VOICE_CONFIG_TIME_REG = 0x3;
function i2cwrite(addr: number, reg: number, value: number) {
let buf = pins.createBuffer(2)
buf[0] = reg
buf[1] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2cwrite1(addr: number, reg: number, value: number ,value1: string) {
let lengths = value1.length
let buf = pins.createBuffer(2+lengths)
//let arr = value1.split('')
buf[0] = reg
buf[1] = value
let betys = []
betys = stringToBytes(value1)
for (let i = 0; i < betys.length; i++) {
buf[2+i] = betys[i]
}
pins.i2cWriteBuffer(addr, buf)
}
function i2ccmd(addr: number, value: number) {
let buf = pins.createBuffer(1)
buf[0] = value
pins.i2cWriteBuffer(addr, buf)
}
//% blockId=sensor_water block="Water vapor sensor pin %pines" group="水蒸气传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_water(pines: AnalogPin): number{
return pins.analogReadPin(pines);
}
//% blockId=sensor_temperature block="Pin %pin reads the analog value of the LM35" group="LM35温度传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_temperature(pin: AnalogPin): number {
let temp = (pins.analogReadPin(pin) / 1023) * 3.3 * 100;
return temp
}
//% blockId=sensor_infraredTracking block="Pin %pin reads the digital value of the infraredTracking sensor" group="红外循迹传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_infraredTracking(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return true;
} else {
return false;
}
}
//% blockId=sensor_incline block="sensor_incline pin |digitalpin %pin" group="倾斜传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_incline(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return false;
} else {
return true;
}
// return pins.digitalReadPin(pin)
}
/**
* 光敏传感器
*/
//% blockId=sensor_illumination block="sensor_illumination pin |analogpin %pin" group="光敏传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_illumination(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
/**
* 热敏传感器
*/
//% blockId=sensor_thermosensitive block="sensor_thermosensitive pin |analogpin %pin" group="热敏传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_thermosensitive(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
/**
* 水深传感器
*/
//% blockId=sensor_waterLevel block="sensor_waterLevel pin |analogpin %pin" group="水深传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_waterLevel(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
/**
* 土壤湿度传感器
*/
//% blockId=sensor_soilMoisture block="sensor_soilMoisture pin |analogpin %pin" group="土壤湿度传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_soilMoisture(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
/**
* 灰度传感器
*/
//% blockId=sensor_grayLevel block="sensor_grayLevel pin |analogpin %pin" group="灰度传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_grayLevel(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
/**
* 避障传感器
*/
//% blockId=sensor_obstacleAvoid block="sensor_obstacleAvoid pin |digitalpin %pin" group="避障传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_obstacleAvoid(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return false;
} else {
return true;
}
// return pins.digitalReadPin(pin)
}
/**
* 磁簧开关传感器
*/
//% blockId=sensor_reedSwitch block="sensor_reedSwitch pin |digitalpin %pin" group="磁簧开关传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_reedSwitch(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return false;
} else {
return true;
}
}
/**
* 人体热释电传感器
*/
//% blockId=sensor_humanBody block="sensor_humanBody pin |digitalpin %pin" group="人体热释电传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_humanBody(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return true;
} else {
return false;
}
}
//% blockId=sensor_flame block="Pin %pin reads the digital value of the flame sensor" group="火焰传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_flame(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return false;
} else {
return true;
}
}
//% blockId=sensor_flame_analog block="Pin %pin reads the analog value of the flame sensor" group="火焰传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_flame_analog(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
/**
* get distance
* @param trig eg: DigitalPin.P13
* @param echo eg: DigitalPin.P14
* @param unit
* @param maxCmDistance
* @returns
*/
//% blockId="sensor_ping" block="ping trig %trig|echo %echo|unit %unit" group="普通超声波传感器"
//% weight=75
//% inlineInputMode=inline
//% subcategory="传感器"
export function ping(trig: DigitalPin, echo: DigitalPin, unit: PingUnit, maxCmDistance = 500): number {
// send pulse
pins.setPull(trig, PinPullMode.PullNone);
pins.digitalWritePin(trig, 0);
control.waitMicros(2);
pins.digitalWritePin(trig, 1);
control.waitMicros(10);
pins.digitalWritePin(trig, 0);
// read pulse
const d = pins.pulseIn(echo, PulseValue.High, maxCmDistance * 58);
switch (unit) {
case PingUnit.Centimeters: return Math.idiv(d, 58);
case PingUnit.Inches: return Math.idiv(d, 148);
default: return d;
}
}
/**
* 震动传感器
*/
//% blockId=sensor_quake block="sensor_quake pin |digitalpin %pin" group="震动传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_quake(pin: DigitalPin): boolean {
// pins.digitalWritePin(pin, 0)
if (pins.digitalReadPin(pin) == 1) {
return false;
} else {
return true;
}
}
/**
* 震动传感器
*/
//% blockId=sensor_quake_analog block="sensor_quake pin |digitalpin %pin" group="震动传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_quake_analog(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
/**
* 声音传感器
*/
//% blockId=sensor_sound_analogread block="Pin %pin reads the analog value of the sound sensor" group="声音传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_sound_analogread(_AS: AnalogPin): number {
return pins.analogReadPin(_AS)
}
//% blockId=sensor_sound_digitalread block="Pin %pin reads the digital value of the sound sensor" group="声音传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_sound_digitalread(_DS: DigitalPin): boolean {
// pins.digitalWritePin(_DS, 0)
if (pins.digitalReadPin(_DS) == 1) {
return false;
} else {
return true;
}
}
/**
* 雨滴传感器
*/
//% blockId=sensor_rain_analogread block="Pin %pin reads the analog value of the rain sensor" group="雨滴传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_rain_analogread(_DR: AnalogPin): number {
return pins.analogReadPin(_DR)
}
//% blockId=sensor_rain_digitalread block="Pin %pin reads the digital value of the rain sensor" group="雨滴传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_rain_digitalread(_DR: DigitalPin): boolean {
// pins.digitalWritePin(_DR, 0)
if (pins.digitalReadPin(_DR) == 1) {
return false;
} else {
return true;
}
}
/**
* 气体传感器
*/
//% blockId=sensor_gas_analogread block="Pin %pin reads the analog value of the MQ4-gas sensor" group="MQ4气体传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_gas_analogread(_AG: AnalogPin): number {
return pins.analogReadPin(_AG)
}
//% blockId=sensor_gas_digitalread block="Pin %pin reads the digital value of the MQ4-gas sensor" group="MQ4气体传感器"
//% weight=70
//% inlineInputMode=inline
//% subcategory="传感器"
export function sensor_gas_digitalread(_DG: DigitalPin): boolean {
// pins.digitalWritePin(_DG, 0)
if (pins.digitalReadPin(_DG) == 1) {
return true;
} else {
return false;
}
}
let initialized = false
//let neoStrip: neopixel.Strip;
let emRGBLight: EMRGBLight.EmakefunRGBLight;
let board_emRGBLight: EMRGBLight.EmakefunRGBLight;
let matBuf = pins.createBuffer(17);
let distanceBuf = 0;
/**
* Get RUS04 distance
* @param pin Microbit ultrasonic pin; eg: DigitalPin.P2
*/
//% blockId=Ultrasonic block="Read RgbUltrasonic Distance at pin %pin(cm)" group="RGB超声波传感器"
//% weight=76
//% inlineInputMode=inline
//% subcategory="传感器"
export function Ultrasonic(pin: DigitalPin): number {
pins.setPull(pin, PinPullMode.PullNone);
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(50);
pins.digitalWritePin(pin, 0);
control.waitMicros(1000);
while(!pins.digitalReadPin(pin));
// read pulse
let d = pins.pulseIn(pin, PulseValue.High, 25000);
let ret = d;
// filter timeout spikes
if (ret == 0 && distanceBuf != 0) {
ret = distanceBuf;
}
distanceBuf = d;
//return d;
return Math.floor(ret * 9 / 6 / 58);
//return Math.floor(ret / 40 + (ret / 800));
// Correction
}
function RgbDisplay(indexstart: number, indexend: number, rgb: RgbColors): void {
for (let i = indexstart; i <= indexend; i++) {
emRGBLight.setPixelColor(i, rgb);
}
emRGBLight.show();
}
function board_RgbDisplay(indexstart: number, indexend: number, rgb: RgbColors): void {
for (let i = indexstart; i <= indexend; i++) {
board_emRGBLight.setPixelColor(i, rgb);
}
board_emRGBLight.show();
}
export function rus04_rgb(pin: DigitalPin, offset: number, index: number, rgb: number, effect: number): void {
let start = 0, end = 0;
if (!emRGBLight) {
emRGBLight = EMRGBLight.create(pin, 10, EMRGBPixelMode.RGB)
}
//if(offset >= 4 || offset == 0){
if (index == RgbUltrasonics.Left) {
start = 0;
end = 2;
} else if (index == RgbUltrasonics.Right) {
start = 3;
end = 5;
} else if (index == RgbUltrasonics.All) {
start = 0;
end = 5;
}
// }
start += offset;
end += offset;
switch (effect) {
case ColorEffect.None:
emRGBLight.setBrightness(255);
RgbDisplay(start, end, rgb);
break;
case ColorEffect.Breathing:
for (let i = 0; i < 255; i += 2) {
emRGBLight.setBrightness(i);
RgbDisplay(start, end, rgb);
//basic.pause((255 - i)/2);
basic.pause((i < 50) ? 10 : (255 / i));
}
for (let i = 255; i > 0; i -= 2) {
emRGBLight.setBrightness(i);
RgbDisplay(start, end, rgb);
basic.pause((i < 50) ? 10 : (255 / i));
}
break;
case ColorEffect.Rotate:
emRGBLight.setBrightness(255);
for (let i = 0; i < 4; i++) {
emRGBLight.setPixelColor(start, rgb);
emRGBLight.setPixelColor(start + 1, 0);
emRGBLight.setPixelColor(start + 2, 0);
if (index == RgbUltrasonics.All) {
emRGBLight.setPixelColor(end - 2, rgb);
emRGBLight.setPixelColor(end - 1, 0);
emRGBLight.setPixelColor(end, 0);
}
emRGBLight.show();
basic.pause(150);
emRGBLight.setPixelColor(start, 0);
emRGBLight.setPixelColor(start + 1, rgb);
emRGBLight.setPixelColor(start + 2, 0);
if (index == RgbUltrasonics.All) {
emRGBLight.setPixelColor(end - 2, 0);
emRGBLight.setPixelColor(end - 1, rgb);
emRGBLight.setPixelColor(end, 0);
}
emRGBLight.show();
basic.pause(150);
emRGBLight.setPixelColor(start, 0);
emRGBLight.setPixelColor(start + 1, 0);
emRGBLight.setPixelColor(start + 2, rgb);
if (index == RgbUltrasonics.All) {
emRGBLight.setPixelColor(end - 2, 0);
emRGBLight.setPixelColor(end - 1, 0);
emRGBLight.setPixelColor(end, rgb);
}
emRGBLight.show();
basic.pause(150);
emRGBLight.setBrightness(0);
}
RgbDisplay(4, 9, 0);
break;