-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
2535 lines (2252 loc) · 79.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
/*
KittenBot Team
A series of sensors
"sugar": "file:../pxt-sugar"
*/
function i2cwrite(addr: number, reg: number, value: number[]) {
let a = [reg]
if (value.length)
a = a.concat(value)
return pins.i2cWriteBuffer(addr, Buffer.fromArray(a))
}
function i2cread(addr: number, reg: number, size: number) {
pins.i2cWriteNumber(addr, reg, NumberFormat.UInt8BE);
return pins.i2cReadBuffer(addr, size);
}
function gestureMap(gestureId: number) {
let gestureTxt = ""
switch (gestureId) {
case 0x01:
gestureTxt = "right"
break;
case 0x02:
gestureTxt = "left"
break;
case 0x03:
gestureTxt = "back"
break;
case 0x04:
gestureTxt = "front"
break;
case 0x05:
gestureTxt = "pull"
break;
case 0x06:
gestureTxt = "down"
break;
case 0x07:
gestureTxt = "leave"
break;
case 0x08:
gestureTxt = "hover"
break;
default:
gestureTxt = null
break;
}
return gestureTxt;
}
//% color="#49cef7" weight=10 icon="\uf1b0" block="Sugar" blockId="Sugar"
namespace Sugar {
const TTS_INTEGER_CMD = 0x20 // tts id : signed integer
const TTS_DOUBLE_CMD = 0x1f // tts id : signed double
const TTS_TIME_CMD = 0x03 // tts id : clock => (hour,minute)
const TTS_DATE_CMD = 0x04 // tts id : date => (year,mouth,day)
const INTEGER_LEN = 4 // 4 bytes int
const DOUBLE_LEN = 8 // 8 bytes double
const CLOCK_LEN = 2 // 2 bytes clock(1,1)
const DATE_LEN = 6 // 2 bytes date(4,1,1)
let QRcodeId = 11
let TopicMesId = 22
let AsrTextId = 1122
type EvtAct = () => void
let ledEvt: EvtAct = null;
let actEvt: EvtAct = null;
let meaEvt: EvtAct = null;
let cosEvt: EvtAct = null;
let asrEventId = 6666
let fpvEventId = 7777
let gestureEventId = 8888
let rfidEventId = 9999
let cmd = 0
let asrText: string = ''
let qrcode: string = ''
let ipAddress: string = '0.0.0.0'
let mqttMessage: string = ''
let mqttTopicL: string = ''
let distanceBuf = 0
let temp = 25
let GestureId = 33
let gesturesOperate = "";
``
const PortSerial = [
[SerialPin.P0, SerialPin.P8],
[SerialPin.P1, SerialPin.P12],
[SerialPin.P2, SerialPin.P13],
[SerialPin.P14, SerialPin.P15]
]
export enum ValueUnit {
//% block="mm"
Millimeter,
//% block="cm"
Centimeters
}
export enum SerialPorts {
//% block="PORT1(TX:P0 / RX:P8)"
PORT1 = 0,
//% block="PORT2(TX:P1 / RX:P12)"
PORT2 = 1,
//% block="PORT3(TX:P2 / RX:P13)"
PORT3 = 2,
//% block="PORT4(TX:P14 / RX:P15)"
PORT4 = 3
}
export enum BTNCmd {
//% block="A"
A = 1,
//% block="B"
B = 2,
//% block="A+B"
AB = 3
}
export enum GestureType {
//% block="right"
right = 0x01,
//% block="left"
left = 0x02,
//% block="back"
back = 0x03,
//% block="front"
front = 0x04,
//% block="pull"
pull = 0x05,
//% block="down"
down = 0x06,
//% block="leave"
leave = 0x07,
//% block="hover"
hover = 0x08,
}
export enum ColorPreset {
//% block="red"
red = 0xff0000,
//% block="blue"
blue = 0x0000ff,
//% block="green"
green = 0x00ff00,
//% block="yellow"
yellow = 0xffff00,
//% block="purple"
purple = 0xff00ff,
//% block="cyan"
cyan = 0x00ffff,
//% block="white"
white = 0xffffff,
}
export enum LEDCmd {
//% block="lamp on / light On"
lamp_on = 200,
//% block="lamp off / light off"
lamp_off = 201,
//% block="brighter"
brighter = 202,
//% block="dimmer"
dimmer = 203,
//% block="red light on"
red_light_on = 204,
//% block="green light on"
green_light_on = 205,
//% block="yellow light on"
yellow_light_on = 206,
//% block="blue light on"
blue_light_on = 207,
//% block="sitting loom light on"
sitting_room_light_on = 208,
//% block="sitting room light off"
sitting_room_light_off = 209,
//% block="kitchen light on"
kitchen_light_on = 210,
//% block="kitchen light off"
kitchen_light_off = 211,
//% block="bedroom light on"
bedroom_light_on = 212,
//% block="bedroom light off"
bedroom_light_off = 213,
//% block="balcony light on"
balcony_light_on = 214,
//% block="balcony light off"
balcony_light_off = 215,
//% block="bathroom light on"
bathroom_light_on = 216,
//% block="bathroom light off"
bathroom_light_off = 217,
//% block="all light on"
all_light_on = 218,
//% block="all light off"
all_light_off = 219
}
export enum ActCmd {
//% block="open door"
open_door = 300,
//% block="close door"
close_door = 301,
//% block="open window"
open_window = 302,
//% block="close window"
close_window = 303,
//% block="open curtains"
open_curtains = 304,
//% block="close curtains"
close_curtains = 305,
//% block="hanger out"
hanger_out = 306,
//% block="hanger in"
hanger_in = 307,
//% block="fan on"
fan_on = 308,
//% block="fan off"
fan_off = 309,
//% block="speed up"
speed_up = 310,
//% block="slow down"
slow_down = 311,
//% block="air conditioner on"
air_conditioner_on = 312,
//% block="air conditioner off"
air_conditioner_off = 313,
//% block="music on"
music_on = 314,
//% block="music off"
music_off = 315,
//% block="pause"
pause = 316,
//% block="previous song"
previous_song = 317,
//% block="next song"
next_song = 318,
//% block="volume up"
volume_up = 319,
//% block="volume down"
volume_down = 320,
//% block="robot on"
robot_on = 321,
//% block="robot off"
robot_off = 322,
//% block="robot stop"
robot_stop = 323,
//% block="move forward"
move_forward = 324,
//% block="move backward"
move_backward = 325,
//% block="turn left"
turn_left = 326,
//% block="turn right"
turn_right = 327,
//% block="lift on"
lift_on_on = 328,
//% block="first floor"
first_floor_off = 329,
//% block="second floor"
second_floor = 330,
//% block="third floor"
third_floor = 331
}
export enum MeasureCmd {
//% block="check temperature"
check_temperature = 400,
//% block="check humidity"
check_humidity = 401,
//% block="check weather"
check_weather = 402,
//% block="check time"
check_time = 403,
//% block="check date"
check_date = 404,
//% block="measure distance"
measure_distance = 405,
//% block="measure temperature"
measure_temperature = 406,
//% block="measure weight"
measure_weight = 407,
//% block="measure height"
measure_height = 408
}
export enum CustomCmd {
//% block="command 1"
command_one = 901,
//% block="command 2"
command_two = 902,
//% block="command 3"
command_three = 903,
//% block="command 4"
command_four = 904,
//% block="command 5"
command_five = 905,
//% block="command 6"
command_six = 906,
//% block="command 7"
command_seven = 907,
//% block="command 8"
command_eight = 908,
//% block="command 9"
command_nine = 909,
//% block="command 10"
command_ten = 910
}
export enum WordsID {
//% block="temperature is"
temperature_is = 0x01,
//% block="Humidity is"
humidity_is = 0x02,
//% block="welcome"
welcome = 0x05,
//% block="distance is"
distance_is = 0x06,
//% block="millimeter"
millimeter = 0x07,
//% block="centimeter"
centimeter = 0x08,
//% block="meter"
meter = 0x09,
//% block="body temperature is"
body_temperature_is = 0x0a,
//% block="weight is"
weight_is = 0x0b,
//% block="gram"
gram = 0x0c,
//% block="kilogram"
kilogram = 0x0d,
//% block="please say the password"
please_say_the_password = 0x0e,
//% block="The weather is"
The_weather_is = 0x0f,
//% block="sunny"
sunny = 0x10,
//% block="cloudy"
cloudy = 0x11,
//% block="raining"
raining = 0x12,
//% block="snowing"
snowing = 0x13,
//% block="haze"
haze = 0x14,
//% block="big"
big = 0x15,
//% block="middle"
middle = 0x16,
//% block="small"
small = 0x17,
//% block="which floor are you going to"
which_floor_are_you_going_to = 0x18,
//% block="yes"
yes = 0x19,
//% block="no"
no = 0x1a,
//% block="percent"
percent = 0x1b,
//% block="you are right"
you_are_right = 0x1c,
//% block="you are wrong"
you_are_wrong = 0x1d,
//% block="degree centigrade"
degree_centigrade = 0x1e,
//% block="ok"
ok = 0x21
}
export enum Switch {
//% block="OFF"
Off = 0,
//% block="ON"
On = 1
}
export enum Ports {
PORT1 = 0,
PORT2 = 1,
PORT3 = 2,
PORT4 = 3,
PORT5 = 4,
PORT6 = 5,
PORT7 = 6
}
//% blockId=button block="button module %pin pressed"
//% subcategory=Sensor group=DigitalIn weight=99 color=#49CEF7
export function Button(pin: DigitalPin): boolean {
pins.setPull(pin, PinPullMode.PullUp)
return pins.digitalReadPin(pin) == 0
}
//% blockId=onButtonEvent block="on button|%pin pressed"
//% subcategory=Sensor group=DigitalIn weight=98 color=#49CEF7
export function onButtonEvent(pin: DigitalPin, handler: () => void): void {
pins.setPull(pin, PinPullMode.PullUp)
pins.onPulsed(pin, PulseValue.Low, handler)
}
//% blockId=Crash block="crash sensor %pin pressed"
//% subcategory=Sensor group=DigitalIn weight=97 color=#49CEF7
export function Crash(pin: DigitalPin): boolean {
return pins.digitalReadPin(pin) == 1
}
//% blockId=Touch block="touch sensor %pin touched"
//% subcategory=Sensor group=DigitalIn weight=96 color=#49CEF7
export function Touch(pin: DigitalPin): boolean {
return pins.digitalReadPin(pin) == 1
}
//% blockId=pir block="PIR sensor %pin human move detected"
//% subcategory=Sensor group=DigitalIn weight=95 color=#49CEF7
export function PIR(pin: DigitalPin): boolean {
return pins.digitalReadPin(pin) == 1
}
//% blockId=hall block="hall sensor %pin magnetic detected"
//% subcategory=Sensor group=DigitalIn weight=94 color=#49CEF7
export function Hall(pin: DigitalPin): boolean {
return pins.digitalReadPin(pin) == 0
}
//% blockId=tracer block="Tracker sensor %pin black line dectected"
//% subcategory=Sensor group=DigitalIn weight=93 color=#49CEF7
export function Tracker(pin: DigitalPin): boolean {
return pins.digitalReadPin(pin) == 1
}
function celsius(pin: DigitalPin): number {
return 32.6;
}
//% blockId=sugarDSTemperature block="ds18b20 sensor %pin get temperature"
//% subcategory=Sensor group=DigitalIn weight=92 color=#49CEF7
export function sugarDSTemperature(pin: DigitalPin): number {
temp = celsius(pin)
while (temp >= 85 || temp <= -300) {
temp = celsius(pin)
basic.pause(100)
}
return Math.round(temp)
}
/**
* signal pin
* @param pin singal pin; eg: DigitalPin.P1
* @param unit desired conversion unit
*/
//% blockId=robotbit_holeultrasonicver block="ultrasonic sensor %pin distance unit %unit value"
//% subcategory=Sensor group=DigitalIn weight=91 color=#49CEF7
export function HoleUltrasonic(pin: DigitalPin, unit: ValueUnit): number {
pins.setPull(pin, PinPullMode.PullNone);
// pins.setPull(pin, PinPullMode.PullDown);
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(10);
pins.digitalWritePin(pin, 0);
pins.setPull(pin, PinPullMode.PullUp);
// read pulse
let d = pins.pulseIn(pin, PulseValue.High, 30000);
let ret = d;
// filter timeout spikes
if (ret == 0 && distanceBuf != 0) {
ret = distanceBuf;
}
distanceBuf = d;
pins.digitalWritePin(pin, 0);
basic.pause(15)
if (parseInt(control.hardwareVersion()) == 2) {
d = ret * 10 / 58;
}
else {
// return Math.floor(ret / 40 + (ret / 800));
d = ret * 15 / 58;
}
switch (unit) {
case ValueUnit.Millimeter: return Math.floor(d)
case ValueUnit.Centimeters: return Math.floor(d / 10)
default: return d;
}
}
function sleep_simulate() {
//microbit can't do 1-millisecond delay,so in this way
for (let index = 0; index < 400; index++) {
}
}
/**
* get 4x4keyboard value
* @param scl Tx pin; eg: DigitalPin.P2
* @param sdo Rx pin; eg: DigitalPin.P12
*/
//% blockId=keyboard_getKey block="4x4 matrix keypad scl %scl sdo %sdo key value"
//% subcategory=Sensor group=DigitalIn weight=90 color=#49CEF7
export function keyboard_getKey(scl: DigitalPin, sdo: DigitalPin): string {
let keyList: number[] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
let keyText: string[] = ["*", "7", "4", "1", "3", "6", "9", "#", "A", "B", "C", "D", "0", "8", "5", "2"];
let i = 0;
pins.digitalWritePin(scl, 1)
basic.pause(1)
i = 0
for (let index = 0; index < keyList.length; index++) {
pins.digitalWritePin(scl, 0)
sleep_simulate()
keyList[i] = pins.digitalReadPin(sdo)
pins.digitalWritePin(scl, 1)
sleep_simulate()
i += 1
}
basic.pause(1)
for (let index = 0; index < keyList.length; index++) {
if (keyList[index] == 0) {
return keyText[index]
}
}
return "None"
}
// //% blockId=flameBool block="flame sensor %pin fire detected"
// //% subcategory=Sensor group=DigitalIn weight=80 color=#49CEF7
// export function FlameDigi(pin: DigitalPin): boolean {
// return pins.digitalReadPin(pin) == 1
// }
let irState: IrState;
let kittenIREventId = 202412
interface IrState {
protocol: 1;
hasNewDatagram: boolean;
bitsReceived: uint8;
addressSectionBits: uint16;
commandSectionBits: uint16;
hiword: uint16;
loword: uint16;
activeCommand: number;
repeatTimeout: number;
onIrDatagram: () => void;
}
const IR_REPEAT = 256;
const IR_INCOMPLETE = 257;
const IR_DATAGRAM = 258;
const REPEAT_TIMEOUT_MS = 120;
export enum IrCmd {
//% block="power"
OFF = 41565,
//% block="menu"
Menu = 25245,
//% block="mute"
Mute = 57885,
//% block="mode"
Mode = 8925,
//% block="+"
Add = 765,
//% block="🔙"
Back = 49725,
//% block="⏪️"
Fb = 57375,
//% block="⏯︎"
Stop = 43095,
//% block="⏩︎"
Ff = 36975,
//% block="0"
Zero = 26775,
//% block="-"
Minus = 39015,
//% block="ok"
OK = 45135,
//% block="1"
One = 12495,
//% block="2"
Tow = 6375,
//% block="3"
Three = 31365,
//% block="4"
Four = 4335,
//% block="5"
Five = 14535,
//% block="6"
Six = 23205,
//% block="7"
Seven = 17085,
//% block="8"
Eight = 19125,
//% block="9"
Nine = 21165,
}
function initIr(pin: DigitalPin) {
initIrState();
enableIrMarkSpaceDetection(pin);
background.schedule(notifyIrEvents, background.Thread.Priority, background.Mode.Repeat, REPEAT_TIMEOUT_MS);
}
//% blockId=initInfraed block="init Infraed %pin"
//% subcategory=Sensor group=DigitalIn weight=71
export function sugarInitInfraed(pin: DigitalPin) {
control.inBackground(() => {
initIr(pin);
});;
}
function appendBitToDatagram(bit: number): number {
irState.bitsReceived += 1;
if (irState.bitsReceived <= 8) {
irState.hiword = (irState.hiword << 1) + bit;
} else if (irState.bitsReceived <= 16) {
irState.hiword = (irState.hiword << 1) + bit;
} else if (irState.bitsReceived <= 32) {
irState.loword = (irState.loword << 1) + bit;
}
if (irState.bitsReceived === 32) {
irState.addressSectionBits = irState.hiword & 0xffff;
irState.commandSectionBits = irState.loword & 0xffff;
serial.writeString(irState.addressSectionBits.toString() + "-" + irState.commandSectionBits.toString())
control.raiseEvent(kittenIREventId, irState.commandSectionBits)
return IR_DATAGRAM;
} else {
return IR_INCOMPLETE;
}
}
function decode(markAndSpace: number): number {
if (markAndSpace < 1600) {
// low bit
return appendBitToDatagram(0);
} else if (markAndSpace < 2700) {
// high bit
return appendBitToDatagram(1);
}
irState.bitsReceived = 0;
if (markAndSpace < 12500) {
// Repeat detected
return IR_REPEAT;
} else if (markAndSpace < 14500) {
// Start detected
return IR_INCOMPLETE;
} else {
return IR_INCOMPLETE;
}
}
function enableIrMarkSpaceDetection(pin: DigitalPin) {
pins.setPull(pin, PinPullMode.PullNone);
let mark = 0;
let space = 0;
pins.onPulsed(pin, PulseValue.Low, () => {
mark = pins.pulseDuration();
});
pins.onPulsed(pin, PulseValue.High, () => {
// LOW
space = pins.pulseDuration();
const status = decode(mark + space);
if (status !== IR_INCOMPLETE) {
handleIrEvent(status);
}
});
}
function handleIrEvent(irEvent: number) {
// Refresh repeat timer
if (irEvent === IR_DATAGRAM || irEvent === IR_REPEAT) {
irState.repeatTimeout = input.runningTime() + REPEAT_TIMEOUT_MS;
}
if (irEvent === IR_DATAGRAM) {
irState.hasNewDatagram = true;
if (irState.onIrDatagram) {
background.schedule(irState.onIrDatagram, background.Thread.UserCallback, background.Mode.Once, 0);
}
const newCommand = irState.commandSectionBits >> 8;
// Process a new command
if (newCommand !== irState.activeCommand) {
irState.activeCommand = newCommand;
}
}
}
function initIrState() {
if (irState) {
return;
}
irState = {
protocol: undefined,
bitsReceived: 0,
hasNewDatagram: false,
addressSectionBits: 0,
commandSectionBits: 0,
hiword: 0, // TODO replace with uint32
loword: 0,
activeCommand: -1,
repeatTimeout: 0,
onIrDatagram: undefined,
};
}
function notifyIrEvents() {
if (irState.activeCommand === -1) {
// skip to save CPU cylces
} else {
const now = input.runningTime();
if (now > irState.repeatTimeout) {
// repeat timed out
irState.bitsReceived = 0;
irState.activeCommand = -1;
}
}
}
/**
* Registers code to run when a specific button on the remote control is pressed.
* @param handler
*/
//% blockId=sugarInfrared block="on remote control |%btn pressed"
//% subcategory=Sensor group=DigitalIn weight=70
//% btn.fieldEditor="gridpicker"
//% btn.fieldOptions.columns=3
export function sugarOnRemoteControlPressed(btn: IrCmd, handler: () => void) {
control.onEvent(kittenIREventId, btn, handler);
}
function ir_rec_to16BitHex(value: number): string {
let hex = "";
for (let pos = 0; pos < 4; pos++) {
let remainder = value % 16;
if (remainder < 10) {
hex = remainder.toString() + hex;
} else {
hex = String.fromCharCode(55 + remainder) + hex;
}
value = Math.idiv(value, 16);
}
return hex;
}
//% blockId=flameAnalog block="flame sensor %pin analog value"
//% subcategory=Sensor group=AnalogIn weight=79 color=#49CEF7
export function FlameAna(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
//% blockId=potential block="angle module %pin analog value"
//% subcategory=Sensor group=AnalogIn weight=78 color=#49CEF7
export function Angle(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
//% blockId=audio block="sound sensor %pin analog value"
//% subcategory=Sensor group=AnalogIn weight=77 color=#49CEF7
export function audio(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
//% blockId=lightlvl block="light sensor %pin analog value"
//% subcategory=Sensor group=AnalogIn weight=76 color=#49CEF7
export function Light(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
//% blockId=soilmoisture block="soilMoisture sensor %pin analog value"
//% subcategory=Sensor group=AnalogIn weight=75 color=#49CEF7
export function SoilMoisture(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
//% blockId=waterlvl block="waterLevel sensor %pin analog value"
//% subcategory=Sensor group=AnalogIn weight=74 color=#49CEF7
export function WaterLevelAna(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
//% blockId=rain block="waterLevel sensor %pin raindrops detected"
//% subcategory=Sensor group="DigitalIn" weight=74 color=#49CEF7
export function WaterLevelDigi(pin: DigitalPin): boolean {
return pins.digitalReadPin(pin) == 1
}
//% blockId=grayscale block="grayscale sensor %pin analog value"
//% subcategory=Sensor group=AnalogIn weight=73 color=#49CEF7
export function grayscale(pin: AnalogPin): number {
return pins.analogReadPin(pin)
}
const VL53L0X_ADDR = 0x5e;
let vl53Inited = false;
//% blockId=tof block="tof distance sensor distance value(0~1200mm)"
//% subcategory=Sensor group=I2C weight=59 color=#49CEF7
export function TOFDistance(): number {
if (!vl53Inited) {
let buf = pins.createBuffer(3)
buf[0] = 1
pins.i2cWriteBuffer(VL53L0X_ADDR, buf)
vl53Inited = true
control.waitMicros(50)
}
pins.i2cWriteNumber(VL53L0X_ADDR, 0x1, NumberFormat.UInt8BE);
return pins.i2cReadNumber(VL53L0X_ADDR, NumberFormat.UInt16LE);
}
const AHT20_ADDR = 0x38
let aht20Inited = false;
function _aht20Ready(): boolean {
let stat = pins.i2cReadNumber(AHT20_ADDR, NumberFormat.UInt8BE);
while (stat & 0x80) {
stat = pins.i2cReadNumber(AHT20_ADDR, NumberFormat.UInt8BE);
basic.pause(100)
}
return true
}
export enum EnvType {
//% block="temperature(℃)"
Temperature = 0,
//% block="humidity(%RH)"
Humidity = 1
}
//% blockId=environment block="ENV.Ⅰ sensor %env value"
//% subcategory=Sensor group=I2C weight=58 color=#49CEF7
export function ENV(env: EnvType): number {
if (!aht20Inited) {
i2cwrite(AHT20_ADDR, 0xba, [])
basic.pause(50)
i2cwrite(AHT20_ADDR, 0xa8, [0, 0])
basic.pause(350)
i2cwrite(AHT20_ADDR, 0xe1, [0x28, 0])
aht20Inited = true;
}
i2cwrite(AHT20_ADDR, 0xac, [0x33, 0])
if (_aht20Ready()) {
const n = pins.i2cReadBuffer(AHT20_ADDR, 6)
const h = ((n[1] << 16) | (n[2] << 8) | (n[3])) >> 4
const humi = Math.floor((h * 0.000095) * 100) / 100
const t = ((n[3] & 0x0f) << 16 | (n[4] << 8) | n[5])
const temp = Math.floor((t * 0.000191 - 50) * 100) / 100
return env === EnvType.Humidity ? humi : temp
}
return 0;
}
//self.i2c address of the device
const HP203B_ADDRESS = 0x76
//ADC_CVT(HP203B_ADC_CVT, 3 - bit OSR, 2 - bit CHNL)
//HP203B Command Set
const HP203B_SOFT_RST = 0x06 //Soft reset the device
const HP203B_ADC_CVT = 0x40 //Perform ADC conversion
const HP203B_READ_PT = 0x10 //Read the temperature and pressure values
const HP203B_READ_AT = 0x11 //Read the temperature and altitude values
const HP203B_READ_P = 0x30 //Read the pressure value only
const HP203B_READ_A = 0x31 //Read the altitude value only
const HP203B_READ_T = 0x32 //Read the temperature value only
const HP203B_ANA_CAL = 0x28 //Re - calibrate the internal analog blocks
const HP203B_READ_REG = 0x80 //Read out the control registers
const HP203B_WRITE_REG = 0xC0 //Write in the control registers
//OSR Configuration
const HP203B_OSR_4096 = 0x00 // Conversion time: 4.1ms
const HP203B_OSR_2048 = 0x04 // Conversion time: 8.2ms
const HP203B_OSR_1024 = 0x08 // Conversion time: 16.4ms
const HP203B_OSR_512 = 0xC0 // Conversion time: 32.8ms
const HP203B_OSR_256 = 0x10 // Conversion time: 65.6ms
const HP203B_OSR_128 = 0x14 // Conversion time: 131.1ms
const HP203B_CH_PRESSTEMP = 0x00 // Sensor Pressure and Temperature Channel
const HP203B_CH_TEMP = 0x02 // Temperature Channel
export enum EnvTypeII {
//% block="pressure(hPa)"
Pressure = 0,
//% block="altitude(m)"
Altitude = 1,
//% block="temp(℃)"
CTemp = 2,
//% block="temp(℉)"
FTemp = 3
}
//% blockId="environment2" block="ENV.II sensor %pin value"
//% subcategory=Sensor group=I2C weight=56 color=#49CEF7
export function ENVII(pin: EnvTypeII): number {
const CNVRSN_CONFIG = Buffer.fromArray([HP203B_ADC_CVT | HP203B_OSR_1024 | HP203B_CH_PRESSTEMP])
pins.i2cWriteBuffer(HP203B_ADDRESS, CNVRSN_CONFIG)
let presData = i2cread(HP203B_ADDRESS, HP203B_READ_P, 3)
let pressure = (((presData[0] & 0x0F) * 65536) + (presData[1] * 256) + presData[2]) / 100.00
let tempData = i2cread(HP203B_ADDRESS, HP203B_READ_T, 3)
let cTemp = (((tempData[0] & 0x0F) * 65536) + (tempData[1] * 256) + tempData[2]) / 100.00
const fTemp = (cTemp * 1.8) + 32
let altData = i2cread(HP203B_ADDRESS, HP203B_READ_A, 3)
let altitude = (((altData[0] & 0x0F) << 16) + (altData[1] << 8) + altData[2]) / 100.00
if (pin === EnvTypeII.Pressure) {
return pressure
} else if (pin === EnvTypeII.Altitude) {
return altitude
} else if (pin === EnvTypeII.CTemp) {
return cTemp
} else {
return fTemp
}
}
export enum JoystickDir {
//% block="pressed"
Pressed = 1,
//% block="left"
Left = 0x10,
//% block="right"
Right = 0x8,
//% block="up"
Up = 0x4,
//% block="down"
Down = 0x2
}
const JOYSTICK_ADDR = 0x5c
//% blockId=joyState block="joystick module %state state triggered"
//% subcategory=Sensor group=I2C weight=55 color=#49CEF7
export function joyState(state: JoystickDir): boolean {
const sta = i2cread(JOYSTICK_ADDR, 1, 1).getNumber(NumberFormat.UInt8BE, 0)
return (sta & state) != 0;
}
export enum DirType {
x = 0,
y = 1
}
//% blockId=joyValue block="joystick module %dir value"
//% subcategory=Sensor group=I2C weight=54 color=#49CEF7
export function joyValue(dir: DirType): number {
const buf = i2cread(JOYSTICK_ADDR, 2, 4)
const valX = Math.round(buf.getNumber(NumberFormat.Int16LE, 0) * 255 / 2048 - 255)
const valY = Math.round(buf.getNumber(NumberFormat.Int16LE, 2) * 255 / 2048 - 255)
return dir === DirType.x ? valX : valY
}
let sugarColorInit = false;
let sugarColor: SugarColor;
//% blockId=colorUpdate block="color sensor update value"
//% subcategory=Sensor group=I2C weight=53 color=#49CEF7
export function colorUpdate(): void {
if (!sugarColorInit) {
sugarColor = new SugarColor()
sugarColorInit = true
}
sugarColor.update()
}
export enum colorType {
//% block="red"
Red = 0,
//% block="green"