-
Notifications
You must be signed in to change notification settings - Fork 9
/
display.ts
1047 lines (1010 loc) · 36.4 KB
/
display.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
/**
* Functions to PlanetX sensor by ELECFREAKS Co.,Ltd.
*/
//% color=#EA5532 icon="\uf110" block="PlanetX_Display" blockId="PlanetX_Display"
//% groups='["LED", "Digital", "Analog", "IIC Port", "OLED", "8*16 Matrix", "7-Seg 4-Dig LED Nixietube"]'
namespace PlanetX_Display {
////////////////////////////////TM 1637/////////////////
let TM1637_CMD1 = 0x40;
let TM1637_CMD2 = 0xC0;
let TM1637_CMD3 = 0x80;
let _SEGMENTS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71];
/////////////////////OLED///////////////////////////////
let firstoledinit = true
const basicFont: string[] = [
"\x00\x00\x00\x00\x00\x00\x00\x00", // " "
"\x00\x00\x5F\x00\x00\x00\x00\x00", // "!"
"\x00\x00\x07\x00\x07\x00\x00\x00", // """
"\x00\x14\x7F\x14\x7F\x14\x00\x00", // "#"
"\x00\x24\x2A\x7F\x2A\x12\x00\x00", // "$"
"\x00\x23\x13\x08\x64\x62\x00\x00", // "%"
"\x00\x36\x49\x55\x22\x50\x00\x00", // "&"
"\x00\x00\x05\x03\x00\x00\x00\x00", // "'"
"\x00\x1C\x22\x41\x00\x00\x00\x00", // "("
"\x00\x41\x22\x1C\x00\x00\x00\x00", // ")"
"\x00\x08\x2A\x1C\x2A\x08\x00\x00", // "*"
"\x00\x08\x08\x3E\x08\x08\x00\x00", // "+"
"\x00\xA0\x60\x00\x00\x00\x00\x00", // ","
"\x00\x08\x08\x08\x08\x08\x00\x00", // "-"
"\x00\x60\x60\x00\x00\x00\x00\x00", // "."
"\x00\x20\x10\x08\x04\x02\x00\x00", // "/"
"\x00\x3E\x51\x49\x45\x3E\x00\x00", // "0"
"\x00\x00\x42\x7F\x40\x00\x00\x00", // "1"
"\x00\x62\x51\x49\x49\x46\x00\x00", // "2"
"\x00\x22\x41\x49\x49\x36\x00\x00", // "3"
"\x00\x18\x14\x12\x7F\x10\x00\x00", // "4"
"\x00\x27\x45\x45\x45\x39\x00\x00", // "5"
"\x00\x3C\x4A\x49\x49\x30\x00\x00", // "6"
"\x00\x01\x71\x09\x05\x03\x00\x00", // "7"
"\x00\x36\x49\x49\x49\x36\x00\x00", // "8"
"\x00\x06\x49\x49\x29\x1E\x00\x00", // "9"
"\x00\x00\x36\x36\x00\x00\x00\x00", // ":"
"\x00\x00\xAC\x6C\x00\x00\x00\x00", // ";"
"\x00\x08\x14\x22\x41\x00\x00\x00", // "<"
"\x00\x14\x14\x14\x14\x14\x00\x00", // "="
"\x00\x41\x22\x14\x08\x00\x00\x00", // ">"
"\x00\x02\x01\x51\x09\x06\x00\x00", // "?"
"\x00\x32\x49\x79\x41\x3E\x00\x00", // "@"
"\x00\x7E\x09\x09\x09\x7E\x00\x00", // "A"
"\x00\x7F\x49\x49\x49\x36\x00\x00", // "B"
"\x00\x3E\x41\x41\x41\x22\x00\x00", // "C"
"\x00\x7F\x41\x41\x22\x1C\x00\x00", // "D"
"\x00\x7F\x49\x49\x49\x41\x00\x00", // "E"
"\x00\x7F\x09\x09\x09\x01\x00\x00", // "F"
"\x00\x3E\x41\x41\x51\x72\x00\x00", // "G"
"\x00\x7F\x08\x08\x08\x7F\x00\x00", // "H"
"\x00\x41\x7F\x41\x00\x00\x00\x00", // "I"
"\x00\x20\x40\x41\x3F\x01\x00\x00", // "J"
"\x00\x7F\x08\x14\x22\x41\x00\x00", // "K"
"\x00\x7F\x40\x40\x40\x40\x00\x00", // "L"
"\x00\x7F\x02\x0C\x02\x7F\x00\x00", // "M"
"\x00\x7F\x04\x08\x10\x7F\x00\x00", // "N"
"\x00\x3E\x41\x41\x41\x3E\x00\x00", // "O"
"\x00\x7F\x09\x09\x09\x06\x00\x00", // "P"
"\x00\x3E\x41\x51\x21\x5E\x00\x00", // "Q"
"\x00\x7F\x09\x19\x29\x46\x00\x00", // "R"
"\x00\x26\x49\x49\x49\x32\x00\x00", // "S"
"\x00\x01\x01\x7F\x01\x01\x00\x00", // "T"
"\x00\x3F\x40\x40\x40\x3F\x00\x00", // "U"
"\x00\x1F\x20\x40\x20\x1F\x00\x00", // "V"
"\x00\x3F\x40\x38\x40\x3F\x00\x00", // "W"
"\x00\x63\x14\x08\x14\x63\x00\x00", // "X"
"\x00\x03\x04\x78\x04\x03\x00\x00", // "Y"
"\x00\x61\x51\x49\x45\x43\x00\x00", // "Z"
"\x00\x7F\x41\x41\x00\x00\x00\x00", // """
"\x00\x02\x04\x08\x10\x20\x00\x00", // "\"
"\x00\x41\x41\x7F\x00\x00\x00\x00", // """
"\x00\x04\x02\x01\x02\x04\x00\x00", // "^"
"\x00\x80\x80\x80\x80\x80\x00\x00", // "_"
"\x00\x01\x02\x04\x00\x00\x00\x00", // "`"
"\x00\x20\x54\x54\x54\x78\x00\x00", // "a"
"\x00\x7F\x48\x44\x44\x38\x00\x00", // "b"
"\x00\x38\x44\x44\x28\x00\x00\x00", // "c"
"\x00\x38\x44\x44\x48\x7F\x00\x00", // "d"
"\x00\x38\x54\x54\x54\x18\x00\x00", // "e"
"\x00\x08\x7E\x09\x02\x00\x00\x00", // "f"
"\x00\x18\xA4\xA4\xA4\x7C\x00\x00", // "g"
"\x00\x7F\x08\x04\x04\x78\x00\x00", // "h"
"\x00\x00\x7D\x00\x00\x00\x00\x00", // "i"
"\x00\x80\x84\x7D\x00\x00\x00\x00", // "j"
"\x00\x7F\x10\x28\x44\x00\x00\x00", // "k"
"\x00\x41\x7F\x40\x00\x00\x00\x00", // "l"
"\x00\x7C\x04\x18\x04\x78\x00\x00", // "m"
"\x00\x7C\x08\x04\x7C\x00\x00\x00", // "n"
"\x00\x38\x44\x44\x38\x00\x00\x00", // "o"
"\x00\xFC\x24\x24\x18\x00\x00\x00", // "p"
"\x00\x18\x24\x24\xFC\x00\x00\x00", // "q"
"\x00\x00\x7C\x08\x04\x00\x00\x00", // "r"
"\x00\x48\x54\x54\x24\x00\x00\x00", // "s"
"\x00\x04\x7F\x44\x00\x00\x00\x00", // "t"
"\x00\x3C\x40\x40\x7C\x00\x00\x00", // "u"
"\x00\x1C\x20\x40\x20\x1C\x00\x00", // "v"
"\x00\x3C\x40\x30\x40\x3C\x00\x00", // "w"
"\x00\x44\x28\x10\x28\x44\x00\x00", // "x"
"\x00\x1C\xA0\xA0\x7C\x00\x00\x00", // "y"
"\x00\x44\x64\x54\x4C\x44\x00\x00", // "z"
"\x00\x08\x36\x41\x00\x00\x00\x00", // "{"
"\x00\x00\x7F\x00\x00\x00\x00\x00", // "|"
"\x00\x41\x36\x08\x00\x00\x00\x00", // "}"
"\x00\x02\x01\x01\x02\x01\x00\x00" // "~"
];
function oledcmd(c: number) {
pins.i2cWriteNumber(0x3c, c, NumberFormat.UInt16BE);
}
function writeData(n: number) {
let b = n;
if (n < 0) { n = 0 }
if (n > 255) { n = 255 }
pins.i2cWriteNumber(0x3c, 0x4000 + b, NumberFormat.UInt16BE);
}
function writeCustomChar(c: string) {
for (let i = 0; i < 8; i++) {
writeData(c.charCodeAt(i));
}
}
function setText(row: number, column: number) {
let r = row;
let c = column;
if (row < 0) { r = 0 }
if (column < 0) { c = 0 }
if (row > 7) { r = 7 }
if (column > 15) { c = 15 }
oledcmd(0xB0 + r); //set page address
oledcmd(0x00 + (8 * c & 0x0F)); //set column lower address
oledcmd(0x10 + ((8 * c >> 4) & 0x0F)); //set column higher address
}
function putChar(c: string) {
let c1 = c.charCodeAt(0);
writeCustomChar(basicFont[c1 - 32]);
}
function oledinit(): void {
oledcmd(0xAE); // Set display OFF
oledcmd(0xD5); // Set Display Clock Divide Ratio / OSC Frequency 0xD4
oledcmd(0x80); // Display Clock Divide Ratio / OSC Frequency
oledcmd(0xA8); // Set Multiplex Ratio
oledcmd(0x3F); // Multiplex Ratio for 128x64 (64-1)
oledcmd(0xD3); // Set Display Offset
oledcmd(0x00); // Display Offset
oledcmd(0x40); // Set Display Start Line
oledcmd(0x8D); // Set Charge Pump
oledcmd(0x14); // Charge Pump (0x10 External, 0x14 Internal DC/DC)
oledcmd(0xA1); // Set Segment Re-Map
oledcmd(0xC8); // Set Com Output Scan Direction
oledcmd(0xDA); // Set COM Hardware Configuration
oledcmd(0x12); // COM Hardware Configuration
oledcmd(0x81); // Set Contrast
oledcmd(0xCF); // Contrast
oledcmd(0xD9); // Set Pre-Charge Period
oledcmd(0xF1); // Set Pre-Charge Period (0x22 External, 0xF1 Internal)
oledcmd(0xDB); // Set VCOMH Deselect Level
oledcmd(0x40); // VCOMH Deselect Level
oledcmd(0xA4); // Set all pixels OFF
oledcmd(0xA6); // Set display not inverted
oledcmd(0xAF); // Set display On
oledClear();
}
//////////////////////////////////////////////////////////////Matrix
let initializedMatrix = false
const HT16K33_ADDRESS = 0x70
const HT16K33_BLINK_CMD = 0x80
const HT16K33_BLINK_DISPLAYON = 0x01
const HT16K33_CMD_BRIGHTNESS = 0xE0
let matBuf = pins.createBuffer(17)
function matrixInit() {
i2ccmd(HT16K33_ADDRESS, 0x21);// turn on oscillator
i2ccmd(HT16K33_ADDRESS, HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (0 << 1));
i2ccmd(HT16K33_ADDRESS, HT16K33_CMD_BRIGHTNESS | 0xF);
}
function i2ccmd(addr: number, value: number) {
let buf = pins.createBuffer(1)
buf[0] = value
pins.i2cWriteBuffer(addr, buf)
}
function matrixShow() {
matBuf[0] = 0x00;
pins.i2cWriteBuffer(HT16K33_ADDRESS, matBuf);
}
///////////////////////////////enum
export enum DigitalRJPin {
//% block="J1"
J1,
//% block="J2"
J2,
//% block="J3"
J3,
//% block="J4"
J4
}
export enum AnalogRJPin {
//% block="J1"
J1,
//% block="J2"
J2
}
export enum EmojiList {
//% block="😆"
Grinning_Squinting_Face,
//% block="😐"
Neutral_Face,
//% block="😞"
Sad_Face,
//% block="🙂"
Slightly_Smiling_Face,
//% block="😠"
Angry_Face
}
export enum NeoPixelColors {
//% 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
}
/**
* Different modes for RGB or RGB+W NeoPixel strips
*/
export enum NeoPixelMode {
//% block="RGB (GRB format)"
RGB = 0,
//% block="RGB+W"
RGBW = 1,
//% block="RGB (RGB format)"
RGB_RGB = 2
}
///////////////////////////////////////////////////////RJpin_to_pin
function RJpin_to_analog(Rjpin: AnalogRJPin): any {
let pin = AnalogPin.P1
switch (Rjpin) {
case AnalogRJPin.J1:
pin = AnalogPin.P1
break;
case AnalogRJPin.J2:
pin = AnalogPin.P2
break;
}
return pin
}
function RJpin_to_digital(Rjpin: DigitalRJPin): any {
let pin = DigitalPin.P1
switch (Rjpin) {
case DigitalRJPin.J1:
pin = DigitalPin.P8
break;
case DigitalRJPin.J2:
pin = DigitalPin.P12
break;
case DigitalRJPin.J3:
pin = DigitalPin.P14
break;
case DigitalRJPin.J4:
pin = DigitalPin.P16
break;
}
return pin
}
/////////////////////////////User_function//////////////////
/**
* toggle led
*/
//% blockId=LED block="LED %Rjpin toggle to $ledstate || brightness %brightness \\%"
//% Rjpin.fieldEditor="gridpicker" Rjpin.fieldOptions.columns=2
//% brightness.min=0 brightness.max=100
//% ledstate.shadow="toggleOnOff"
//% subcategory=Display group="LED" color=#EA5532
//% expandableArgumentMode="toggle"
export function ledBrightness(Rjpin: DigitalRJPin, ledstate: boolean, brightness: number = 100): void {
let pin = AnalogPin.P1
switch (Rjpin) {
case DigitalRJPin.J1:
pin = AnalogPin.P1
break;
case DigitalRJPin.J2:
pin = AnalogPin.P2
break;
case DigitalRJPin.J3:
pin = AnalogPin.P13
break;
case DigitalRJPin.J4:
pin = AnalogPin.P15
break;
}
if (ledstate) {
pins.analogSetPeriod(pin, 100)
pins.analogWritePin(pin, Math.map(brightness, 0, 100, 0, 1023))
}
else {
pins.analogWritePin(pin, 0)
brightness = 0
}
}
//% subcategory=Display group="8*16 Matrix" color=#00B1ED
//% blockId= matrix_refresh block="Matrix Refresh"
export function matrixRefresh(): void {
if (!initializedMatrix) {
matrixInit();
initializedMatrix = true;
}
matrixShow();
}
//% subcategory=Display group="8*16 Matrix" color=#00B1ED
//% blockId= matrix_clear block="Matrix Clear"
export function matrixClear(): void {
if (!initializedMatrix) {
matrixInit();
initializedMatrix = true;
}
for (let i = 0; i < 16; i++) {
matBuf[i + 1] = 0;
}
matrixShow();
}
//% x.min=0 x.max=15
//% y.min=0 y.max=7
//% blockId= matrix_draw block="Matrix Draw|X %x|Y %y"
//% subcategory=Display group="8*16 Matrix" color=#00B1ED
export function matrixDraw(x: number, y: number): void {
if (!initializedMatrix) {
matrixInit();
initializedMatrix = true;
}
if(x > 15)
{
x = 15
}
if(y > 7)
{
y = 7
}
if(x < 0)
{
x = 0
}
if(y < 0)
{
y = 0
}
x = Math.round(x)
y = Math.round(y)
let idx = y * 2 + Math.idiv(x, 8);
let tmp = matBuf[idx + 1];
tmp |= (1 << (x % 8));
matBuf[idx + 1] = tmp;
matrixShow();
}
//% block="Matrix show emoji %ID" color=#00B1ED
//% subcategory=Display group="8*16 Matrix"
export function matrixEmoji(ID: EmojiList) {
matrixClear();
let point;
switch (ID) {
case 0:
point = [[2, 0], [13, 0],
[3, 1], [12, 1],
[4, 2], [11, 2],
[3, 3], [12, 3],
[2, 4], [5, 4], [6, 4], [7, 4], [8, 4], [9, 4], [10, 4], [13, 4],
[5, 5], [7, 5], [8, 5], [10, 5],
[5, 6], [10, 6],
[6, 7], [7, 7], [8, 7], [9, 7]
];
break;
case 1:
point = [[2, 1], [3, 1], [13, 1], [12, 1],
[2, 2], [3, 2], [13, 2], [12, 2],
[2, 3], [3, 3], [13, 3], [12, 3],
[5, 5], [6, 5], [7, 5], [8, 5], [9, 5], [10, 5],
[5, 6], [6, 6], [7, 6], [8, 6], [9, 6], [10, 6]
];
break;
case 2:
point = [[1, 2], [5, 2], [10, 2], [14, 2],
[1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [10, 3], [11, 3], [12, 3], [13, 3], [14, 3],
[2, 4], [3, 4], [4, 4], [11, 4], [12, 4], [13, 4],
[6, 6], [7, 6], [8, 6], [9, 6],
[5, 7], [10, 7]
];
break;
case 3:
point = [[2, 1], [3, 1], [13, 1], [12, 1],
[2, 2], [3, 2], [13, 2], [12, 2],
[2, 3], [3, 3], [13, 3], [12, 3],
[5, 5], [10, 5],
[6, 6], [7, 6], [8, 6], [9, 6]
];
break;
case 4:
point = [[2, 0], [13, 0],
[3, 1], [12, 1],
[3, 2], [4, 2], [11, 2], [12, 2],
[3, 3], [4, 3], [11, 3], [12, 3],
[6, 6], [7, 6], [8, 6], [9, 6],
[5, 7], [10, 7]
];
break;
}
let index_max = point.length
for (let index = 0; index < index_max; index++) {
matrixDraw(point[index][0], point[index][1])
}
matrixRefresh();
}
//% line.min=1 line.max=8 line.defl=1
//% text.defl="Hello,ELECFREAKS"
//% block="OLED show line %line|text %text"
//% subcategory=Display group="OLED" color=#00B1ED
export function showUserText(line: number, text: string) {
if (firstoledinit) {
oledinit()
firstoledinit = false
}
if(text.length > 16){
text=text.substr(0, 16)
}
line = line - 1
setText(line, 0);
for (let c of text) {
putChar(c);
}
for (let i = text.length; i < 16; i++) {
setText(line, i);
putChar(" ");
}
}
//% line.min=1 line.max=8 line.defl=2
//% n.defl=20200508
//% block="OLED show line %line|number %n"
//% subcategory=Display group="OLED" color=#00B1ED
export function showUserNumber(line: number, n: number) {
if (firstoledinit) {
oledinit()
firstoledinit = false
}
showUserText(line, "" + n)
}
//% block="clear display" color=#00B1ED
//% subcategory=Display group="OLED"
export function oledClear() {
//oledcmd(DISPLAY_OFF); //display off
for (let j = 0; j < 8; j++) {
setText(j, 0);
{
for (let i = 0; i < 16; i++) //clear all columns
{
putChar(' ');
}
}
}
//oledcmd(DISPLAY_ON); //display on
setText(0, 0);
}
/**
* Create a new driver Grove - 4-Digit Display
* @param clkPin value of clk pin number
* @param dataPin value of data pin number
*/
//% blockId=grove_tm1637_create block="connect 4-Digit Display |pin %pin|"
//% subcategory=Display group="7-Seg 4-Dig LED Nixietube" blockSetVariable=display color=#EA5532
export function tm1637Create(Rjpin: DigitalRJPin, intensity: number = 7, count: number = 4): TM1637LEDs {
let display = new TM1637LEDs();
switch (Rjpin) {
case DigitalRJPin.J1:
display.clk = DigitalPin.P1
display.dio = DigitalPin.P8
break;
case DigitalRJPin.J2:
display.clk = DigitalPin.P2
display.dio = DigitalPin.P12
break;
case DigitalRJPin.J3:
display.clk = DigitalPin.P13
display.dio = DigitalPin.P14
break;
case DigitalRJPin.J4:
display.clk = DigitalPin.P15
display.dio = DigitalPin.P16
break;
}
if ((count < 1) || (count > 5)) count = 4;
display.count = count;
display.brightness = intensity;
display.init();
return display;
}
export class TM1637LEDs {
buf: Buffer;
clk: DigitalPin;
dio: DigitalPin;
_ON: number;
brightness: number;
count: number; // number of LEDs
/**
* initial TM1637
*/
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(TM1637_CMD1);
this._stop();
}
/**
* send command3
*/
_write_dsp_ctrl() {
this._start();
this._write_byte(TM1637_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.dio, (b >> i) & 1);
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.clk, 0);
}
pins.digitalWritePin(this.clk, 1);
pins.digitalWritePin(this.clk, 0);
}
_intensity(val: number = 7) {
this._ON = 8;
this.brightness = val - 1;
this._write_data_cmd();
this._write_dsp_ctrl();
}
/**
* set data to TM1637, with given bit
*/
_dat(bit: number, dat: number) {
this._write_data_cmd();
this._start();
this._write_byte(TM1637_CMD2 | (bit % this.count))
this._write_byte(dat);
this._stop();
this._write_dsp_ctrl();
}
/**
* Show a single number from 0 to 9 at a specified digit of Grove - 4-Digit Display
* @param dispData value of number
* @param bitAddr value of bit number
*/
//% blockId=grove_tm1637_display_bit block="%display|show single number|%num|at digit|%bit"
//% subcategory=Display group="7-Seg 4-Dig LED Nixietube" color=#EA5532
//% bit.defl=1 bit.min=0 bit.max=9
showbit(num: number = 5, bit: number = 0) {
bit = Math.map(bit, 4, 1, 0, 3)
this.buf[bit % this.count] = _SEGMENTS[num % 16]
this._dat(bit, _SEGMENTS[num % 16])
}
/**
* Show a 4 digits number on display
* @param dispData value of number
*/
//% blockId=grove_tm1637_display_number block="%display|show number|%num"
//% subcategory=Display group="7-Seg 4-Dig LED Nixietube" color=#EA5532
showNumber(num: number) {
if (num < 0) {
num = -num
this.showbit(Math.idiv(num, 1000) % 10)
this.showbit(num % 10, 1)
this.showbit(Math.idiv(num, 10) % 10, 2)
this.showbit(Math.idiv(num, 100) % 10, 3)
this._dat(0, 0x40) // '-'
}
else {
this.showbit(Math.idiv(num, 1000) % 10)
this.showbit(num % 10, 1)
this.showbit(Math.idiv(num, 10) % 10, 2)
this.showbit(Math.idiv(num, 100) % 10, 3)
}
}
/**
* show or hide dot point.
* @param bit is the position, eg: 1
* @param show is show/hide dp, eg: true
*/
//% blockId="TM1637_showDP" block="%display|DotPoint at %bit|show $show"
//% show.shadow="toggleOnOff"
//% subcategory=Display group="7-Seg 4-Dig LED Nixietube" color=#EA5532
showDP(bit: number = 1, show: boolean = true) {
bit = Math.map(bit, 4, 1, 0, 3)
bit = bit % this.count
if (show) this._dat(bit, this.buf[bit] | 0x80)
else this._dat(bit, this.buf[bit] & 0x7F)
}
/**
* clear LED.
*/
//% blockId="TM1637_clear" block="clear display %display"
//% subcategory=Display group="7-Seg 4-Dig LED Nixietube" color=#EA5532
clear() {
for (let i = 0; i < this.count; i++) {
this._dat(i, 0)
this.buf[i] = 0
}
}
}
//% shim=sendBufferAsm
function sendBuffer(buf: Buffer, pin: DigitalPin) {
}
export class Strip {
buf:Buffer;
pin:DigitalPin;
// TODO: encode as bytes instead of 32bit
brightness:number;
start:number; // start offset in LED strip
_length:number; // number of LEDs
_mode:NeoPixelMode;
_matrixWidth:number; // number of leds in a matrix - if any
/**
* Shows all LEDs to a given color (range 0-255 for r, g, b).
* @param rgb RGB color of the LED
*/
//% blockId="neopixel_set_strip_color" block="%strip|show color %rgb=neopixel_colors"
//% weight=85 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
showColor(rgb: number) {
rgb = rgb >> 0;
this.setAllRGB(rgb);
this.show();
}
/**
* Shows a rainbow pattern on all LEDs.
* @param startHue the start hue value for the rainbow, eg: 1
* @param endHue the end hue value for the rainbow, eg: 360
*/
//% blockId="neopixel_set_strip_rainbow" block="%strip|show rainbow from %startHue|to %endHue"
//% weight=85 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
showRainbow(startHue: number = 1, endHue: number = 360) {
if (this._length <= 0) return;
startHue = startHue >> 0;
endHue = endHue >> 0;
const saturation = 100;
const luminance = 50;
const steps = this._length;
const direction = HueInterpolationDirection.Clockwise;
//hue
const h1 = startHue;
const h2 = endHue;
const hDistCW = ((h2 + 360) - h1) % 360;
const hStepCW = Math.idiv((hDistCW * 100), steps);
const hDistCCW = ((h1 + 360) - h2) % 360;
const hStepCCW = Math.idiv(-(hDistCCW * 100), steps);
let hStep: number;
if (direction === HueInterpolationDirection.Clockwise) {
hStep = hStepCW;
} else if (direction === HueInterpolationDirection.CounterClockwise) {
hStep = hStepCCW;
} else {
hStep = hDistCW < hDistCCW ? hStepCW : hStepCCW;
}
const h1_100 = h1 * 100; //we multiply by 100 so we keep more accurate results while doing interpolation
//sat
const s1 = saturation;
const s2 = saturation;
const sDist = s2 - s1;
const sStep = Math.idiv(sDist, steps);
const s1_100 = s1 * 100;
//lum
const l1 = luminance;
const l2 = luminance;
const lDist = l2 - l1;
const lStep = Math.idiv(lDist, steps);
const l1_100 = l1 * 100
//interpolate
if (steps === 1) {
this.setPixelColor(0, hsl(h1 + hStep, s1 + sStep, l1 + lStep))
} else {
this.setPixelColor(0, hsl(startHue, saturation, luminance));
for (let i = 1; i < steps - 1; i++) {
const h = Math.idiv((h1_100 + i * hStep), 100) + 360;
const s = Math.idiv((s1_100 + i * sStep), 100);
const l = Math.idiv((l1_100 + i * lStep), 100);
this.setPixelColor(i, hsl(h, s, l));
}
this.setPixelColor(steps - 1, hsl(endHue, saturation, luminance));
}
this.show();
}
/**
* Set LED to a given color (range 0-255 for r, g, b).
* You need to call ``show`` to make the changes visible.
* @param pixeloffset position of the NeoPixel in the strip
* @param rgb RGB color of the LED
*/
//% blockId="neopixel_set_pixel_color" block="%strip|set pixel color at %pixeloffset|to %rgb=neopixel_colors"
//% weight=80 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
setPixelColor(pixeloffset: number, rgb: number): void {
this.setPixelRGB(pixeloffset >> 0, rgb >> 0);
}
/**
* Send all the changes to the strip.
*/
//% blockId="neopixel_show" block="%strip|show"
//% weight=79
//% parts="neopixel" subcategory=Neopixel
show() {
sendBuffer(this.buf, this.pin);
}
/**
* Turn off all LEDs.
* You need to call ``show`` to make the changes visible.
*/
//% blockId="neopixel_clear" block="%strip|clear"
//% weight=76 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
clear(): void {
const stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
this.buf.fill(0, this.start * stride, this._length * stride);
}
/**
* Set the brightness of the strip. This flag only applies to future operation.
* @param brightness a measure of LED brightness in 0-255. eg: 255
*/
//% blockId="neopixel_set_brightness" block="%strip|set brightness %brightness"
//% weight=59 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
setBrightness(brightness: number): void {
this.brightness = brightness & 0xff;
}
/**
* Create a range of LEDs.
* @param start offset in the LED strip to start the range
* @param length number of LEDs in the range. eg: 4
*/
//% weight=89 color=#EA5532
//% blockId="neopixel_range" block="%strip|range from %start|with %length|leds"
//% parts="neopixel"
//% blockSetVariable=range subcategory=Neopixel
range(start: number, length: number): Strip {
start = start >> 0;
length = length >> 0;
let strip = new Strip();
strip.buf = this.buf;
strip.pin = this.pin;
strip.brightness = this.brightness;
strip.start = this.start + Math.clamp(0, this._length - 1, start);
strip._length = Math.clamp(0, this._length - (strip.start - this.start), length);
strip._matrixWidth = 0;
strip._mode = this._mode;
return strip;
}
/**
* Shift LEDs forward and clear with zeros.
* You need to call ``show`` to make the changes visible.
* @param offset number of pixels to shift forward, eg: 1
*/
//% blockId="neopixel_shift" block="%strip|shift pixels by %offset"
//% weight=40 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
shift(offset: number = 1): void {
offset = offset >> 0;
const stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
this.buf.shift(-offset * stride, this.start * stride, this._length * stride)
}
/**
* Rotate LEDs forward.
* You need to call ``show`` to make the changes visible.
* @param offset number of pixels to rotate forward, eg: 1
*/
//% blockId="neopixel_rotate" block="%strip|rotate pixels by %offset"
//% weight=39 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
rotate(offset: number = 1): void {
offset = offset >> 0;
const stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
this.buf.rotate(-offset * stride, this.start * stride, this._length * stride)
}
/**
* Set the pin where the neopixel is connected, defaults to P0.
*/
//% weight=10 color=#EA5532
//% parts="neopixel" subcategory=Neopixel
setPin(pin: DigitalPin): void {
this.pin = pin;
pins.digitalWritePin(this.pin, 0);
// don't yield to avoid races on initialization
}
private setBufferRGB(offset: number, red: number, green: number, blue: number): void {
if (this._mode === NeoPixelMode.RGB_RGB) {
this.buf[offset + 0] = red;
this.buf[offset + 1] = green;
} else {
this.buf[offset + 0] = green;
this.buf[offset + 1] = red;
}
this.buf[offset + 2] = blue;
}
private setAllRGB(rgb: number) {
let red = unpackR(rgb);
let green = unpackG(rgb);
let blue = unpackB(rgb);
const br = this.brightness;
if (br < 255) {
red = (red * br) >> 8;
green = (green * br) >> 8;
blue = (blue * br) >> 8;
}
const end = this.start + this._length;
const stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
for (let i = this.start; i < end; ++i) {
this.setBufferRGB(i * stride, red, green, blue)
}
}
private setPixelRGB(pixeloffset: number, rgb: number): void {
if (pixeloffset < 0
|| pixeloffset >= this._length)
return;
let stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
pixeloffset = (pixeloffset + this.start) * stride;
let red = unpackR(rgb);
let green = unpackG(rgb);
let blue = unpackB(rgb);
let br = this.brightness;
if (br < 255) {
red = (red * br) >> 8;
green = (green * br) >> 8;
blue = (blue * br) >> 8;
}
this.setBufferRGB(pixeloffset, red, green, blue)
}
}
/**
* Create a new NeoPixel driver for `numleds` LEDs.
* @param pin the pin where the neopixel is connected.
* @param numleds number of leds in the strip, eg: 24,30,60,64
*/
//% blockId="neopixel_create" block="NeoPixel at pin %Rjpin|with %numleds|leds as %mode"
//% weight=90 color=#EA5532
//% parts="neopixel"
//% trackArgs=0,2
//% blockSetVariable=strip subcategory=Neopixel
export function create(Rjpin: DigitalRJPin, numleds: number, mode: NeoPixelMode): Strip {
let pin = DigitalPin.P1
switch (Rjpin) {
case DigitalRJPin.J1:
pin = DigitalPin.P8
break;
case DigitalRJPin.J2:
pin = DigitalPin.P12
break;
case DigitalRJPin.J3:
pin = DigitalPin.P14
break;
case DigitalRJPin.J4:
pin = DigitalPin.P16
break;
}
let strip = new Strip();
let stride = mode === NeoPixelMode.RGBW ? 4 : 3;
strip.buf = pins.createBuffer(numleds * stride);
strip.start = 0;
strip._length = numleds;
strip._mode = mode;
strip._matrixWidth = 0;
strip.setBrightness(50)
strip.setPin(pin)
return strip;
}
/**
* Converts red, green, blue channels into a RGB color
* @param red value of the red channel between 0 and 255. eg: 255
* @param green value of the green channel between 0 and 255. eg: 255
* @param blue value of the blue channel between 0 and 255. eg: 255
*/
//% weight=1 subcategory=Neopixel color=#EA5532
//% blockId="neopixel_rgb" block="red %red|green %green|blue %blue"
export function rgb(red: number, green: number, blue: number): number {
return packRGB(red, green, blue);
}
/**
* Gets the RGB value of a known color
*/
//% weight=2 subcategory=Neopixel color=#EA5532
//% blockId="neopixel_colors" block="%color"
export function colors(color: NeoPixelColors): number {
return color;
}
function packRGB(a: number, b: number, c: number): number {
return ((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF);
}
function unpackR(rgb: number): number {
let r = (rgb >> 16) & 0xFF;
return r;
}
function unpackG(rgb: number): number {
let g = (rgb >> 8) & 0xFF;
return g;
}
function unpackB(rgb: number): number {
let b = (rgb) & 0xFF;
return b;
}
/**
* Converts a hue saturation luminosity value into a RGB color
* @param h hue from 0 to 360
* @param s saturation from 0 to 99
* @param l luminosity from 0 to 99
*/
//% blockId=neopixelHSL block="hue %h|saturation %s|luminosity %l" subcategory=Neopixel color=#EA5532
export function hsl(h: number, s: number, l: number): number {
h = Math.round(h);
s = Math.round(s);
l = Math.round(l);
h = h % 360;
s = Math.clamp(0, 99, s);
l = Math.clamp(0, 99, l);
let c = Math.idiv((((100 - Math.abs(2 * l - 100)) * s) << 8), 10000); //chroma, [0,255]
let h1 = Math.idiv(h, 60);//[0,6]
let h2 = Math.idiv((h - h1 * 60) * 256, 60);//[0,255]
let temp = Math.abs((((h1 % 2) << 8) + h2) - 256);
let x = (c * (256 - (temp))) >> 8;//[0,255], second largest component of this color
let r$:
number;