-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patholedplus.ts
More file actions
1104 lines (1026 loc) · 39.2 KB
/
oledplus.ts
File metadata and controls
1104 lines (1026 loc) · 39.2 KB
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
/**
* Based Kitronik VIEW 128x64 Display blocks
* LCD chip SSD1306
**/
//% weight=10 color=#e342f5 icon="\uf26c" block="Hackbit OLED+" blockId="HackbitOLEDPlus"
//% groups='["Control", "Show", "Draw", "Delete"]'
namespace hackbit_VIEW128x64 {
let font: number[] = [];
font[0] = 0x0022d422;
font[1] = 0x0022d422;
font[2] = 0x0022d422;
font[3] = 0x0022d422;
font[4] = 0x0022d422;
font[5] = 0x0022d422;
font[6] = 0x0022d422;
font[7] = 0x0022d422;
font[8] = 0x0022d422;
font[9] = 0x0022d422;
font[10] = 0x0022d422;
font[11] = 0x0022d422;
font[12] = 0x0022d422;
font[13] = 0x0022d422;
font[14] = 0x0022d422;
font[15] = 0x0022d422;
font[16] = 0x0022d422;
font[17] = 0x0022d422;
font[18] = 0x0022d422;
font[19] = 0x0022d422;
font[20] = 0x0022d422;
font[21] = 0x0022d422;
font[22] = 0x0022d422;
font[23] = 0x0022d422;
font[24] = 0x0022d422;
font[25] = 0x0022d422;
font[26] = 0x0022d422;
font[27] = 0x0022d422;
font[28] = 0x0022d422;
font[29] = 0x0022d422;
font[30] = 0x0022d422;
font[31] = 0x0022d422;
font[32] = 0x00000000;
font[33] = 0x000002e0;
font[34] = 0x00018060;
font[35] = 0x00afabea;
font[36] = 0x00aed6ea;
font[37] = 0x01991133;
font[38] = 0x010556aa;
font[39] = 0x00000060;
font[40] = 0x000045c0;
font[41] = 0x00003a20;
font[42] = 0x00051140;
font[43] = 0x00023880;
font[44] = 0x00002200;
font[45] = 0x00021080;
font[46] = 0x00000100;
font[47] = 0x00111110;
font[48] = 0x0007462e;
font[49] = 0x00087e40;
font[50] = 0x000956b9;
font[51] = 0x0005d629;
font[52] = 0x008fa54c;
font[53] = 0x009ad6b7;
font[54] = 0x008ada88;
font[55] = 0x00119531;
font[56] = 0x00aad6aa;
font[57] = 0x0022b6a2;
font[58] = 0x00000140;
font[59] = 0x00002a00;
font[60] = 0x0008a880;
font[61] = 0x00052940;
font[62] = 0x00022a20;
font[63] = 0x0022d422;
font[64] = 0x00e4d62e;
font[65] = 0x000f14be;
font[66] = 0x000556bf;
font[67] = 0x0008c62e;
font[68] = 0x0007463f;
font[69] = 0x0008d6bf;
font[70] = 0x000094bf;
font[71] = 0x00cac62e;
font[72] = 0x000f909f;
font[73] = 0x000047f1;
font[74] = 0x0017c629;
font[75] = 0x0008a89f;
font[76] = 0x0008421f;
font[77] = 0x01f1105f;
font[78] = 0x01f4105f;
font[79] = 0x0007462e;
font[80] = 0x000114bf;
font[81] = 0x000b6526;
font[82] = 0x010514bf;
font[83] = 0x0004d6b2;
font[84] = 0x0010fc21;
font[85] = 0x0007c20f;
font[86] = 0x00744107;
font[87] = 0x01f4111f;
font[88] = 0x000d909b;
font[89] = 0x00117041;
font[90] = 0x0008ceb9;
font[91] = 0x0008c7e0;
font[92] = 0x01041041;
font[93] = 0x000fc620;
font[94] = 0x00010440;
font[95] = 0x01084210;
font[96] = 0x00000820;
font[97] = 0x010f4a4c;
font[98] = 0x0004529f;
font[99] = 0x00094a4c;
font[100] = 0x000fd288;
font[101] = 0x000956ae;
font[102] = 0x000097c4;
font[103] = 0x0007d6a2;
font[104] = 0x000c109f;
font[105] = 0x000003a0;
font[106] = 0x0006c200;
font[107] = 0x0008289f;
font[108] = 0x000841e0;
font[109] = 0x01e1105e;
font[110] = 0x000e085e;
font[111] = 0x00064a4c;
font[112] = 0x0002295e;
font[113] = 0x000f2944;
font[114] = 0x0001085c;
font[115] = 0x00012a90;
font[116] = 0x010a51e0;
font[117] = 0x010f420e;
font[118] = 0x00644106;
font[119] = 0x01e8221e;
font[120] = 0x00093192;
font[121] = 0x00222292;
font[122] = 0x00095b52;
font[123] = 0x0008fc80;
font[124] = 0x000003e0;
font[125] = 0x000013f1;
font[126] = 0x00841080;
font[127] = 0x0022d422;
/**
* Select the alignment of text
*/
export enum ShowAlign {
//% block="Left"
//% block.loc.pt-BR="Esquerda"
Left,
//% block="Centre"
//% block.loc.pt-BR="Centro"
Centre,
//% block="Right"
//% block.loc.pt-BR="Direita"
Right
}
/**
* Selecting direction of drawing line
*/
export enum LineDirectionSelection {
//% block="horizontal"
//% block.loc.pt-BR="horizontal"
horiztonal,
//% block="vertical"
//% block.loc.pt-BR="vertical"
vertical
}
//Screen buffers for sending data to the display
let screenBuf = pins.createBuffer(1025);
let ackBuf = pins.createBuffer(2);
let writeOneByteBuf = pins.createBuffer(2);
let writeTwoByteBuf = pins.createBuffer(3);
let writeThreeByteBuf = pins.createBuffer(4);
let initalised = 0
//Constants for Display
let NUMBER_OF_CHAR_PER_LINE = 27
//default address for the display
let DISPLAY_ADDR_1 = 60
let DISPLAY_ADDR_2 = 10
let displayAddress = DISPLAY_ADDR_1;
//text alignment
let displayShowAlign = ShowAlign.Left
//plot variables
let plotArray: number[] = []
let plottingEnable = 0
let plotData = 0;
let graphYMin = 0
let graphYMax = 100
let graphRange = 100
let GRAPH_Y_MIN_LOCATION = 63
let GRAPH_Y_MAX_LOCATION = 20
let previousYPlot = 0
//function write one byte of data to the display
function writeOneByte(regValue: number) {
writeOneByteBuf[0] = 0;
writeOneByteBuf[1] = regValue;
pins.i2cWriteBuffer(displayAddress, writeOneByteBuf);
}
//function write two byte of data to the display
function writeTwoByte(regValue1: number, regValue2: number) {
writeTwoByteBuf[0] = 0;
writeTwoByteBuf[1] = regValue1;
writeTwoByteBuf[2] = regValue2;
pins.i2cWriteBuffer(displayAddress, writeTwoByteBuf);
}
//function write three byte of data to the display
function writeThreeByte(regValue1: number, regValue2: number, regValue3: number) {
writeThreeByteBuf[0] = 0;
writeThreeByteBuf[1] = regValue1;
writeThreeByteBuf[2] = regValue2;
writeThreeByteBuf[3] = regValue3;
pins.i2cWriteBuffer(displayAddress, writeThreeByteBuf);
}
function set_pos(col: number = 0, page: number = 0) {
writeOneByte(0xb0 | page) // page number
writeOneByte(0x00 | (col % 16)) // lower start column address
writeOneByte(0x10 | (col >> 4)) // upper start column address
}
// clear bit
function clearBit(d: number, b: number): number {
if (d & (1 << b))
d -= (1 << b)
return d
}
// sorts the value and return the correct address
function setScreenAddr(selection: number): number {
let addr = 0
if (selection == 1) {
addr = DISPLAY_ADDR_1
}
else if (selection == 2) {
addr = DISPLAY_ADDR_2
}
else {
addr = DISPLAY_ADDR_1
}
return addr
}
/**
* Setup of display ready for using
* @param screen is the selection of which screen to initialise
*/
export function initDisplay(screen?: number): void {
displayAddress = setScreenAddr(screen)
//load the ackBuffer to check is there is a display there before starting initalising of ths display
ackBuf[0] = 0
ackBuf[1] = 0xAF
let ack = pins.i2cWriteBuffer(displayAddress, ackBuf)
if (ack == -1010) { //ifvalue return back is -1010, there is no display and show error message
basic.showString("ERROR - no display")
}
else { //start initalising of the display
writeOneByte(0xAE) // SSD1306_DISPLAYOFF
writeOneByte(0xA4) // SSD1306_DISPLAYALLON_RESUME
writeTwoByte(0xD5, 0xF0) // SSD1306_SETDISPLAYCLOCKDIV
writeTwoByte(0xA8, 0x3F) // SSD1306_SETMULTIPLEX
writeTwoByte(0xD3, 0x00) // SSD1306_SETDISPLAYOFFSET
writeOneByte(0 | 0x0) // line #SSD1306_SETSTARTLINE
writeTwoByte(0x8D, 0x14) // SSD1306_CHARGEPUMP
writeTwoByte(0x20, 0x00) // SSD1306_MEMORYMODE
writeThreeByte(0x21, 0, 127) // SSD1306_COLUMNADDR
writeThreeByte(0x22, 0, 63) // SSD1306_PAGEADDR
writeOneByte(0xa0 | 0x1) // SSD1306_SEGREMAP
writeOneByte(0xc8) // SSD1306_COMSCANDEC
writeTwoByte(0xDA, 0x12) // SSD1306_SETCOMPINS
writeTwoByte(0x81, 0xCF) // SSD1306_SETCONTRAST
writeTwoByte(0xd9, 0xF1) // SSD1306_SETPRECHARGE
writeTwoByte(0xDB, 0x40) // SSD1306_SETVCOMDETECT
writeOneByte(0xA6) // SSD1306_NORMALDISPLAY
writeTwoByte(0xD6, 0) // zoom is set to off
writeOneByte(0xAF) // SSD1306_DISPLAYON
initalised = 1
clear()
}
}
let quer = hackbit_VIEW128x64.matrix22x6(`
. . # # # # # # # # # # # # # # # # # # . .
. # # # # # # # # # # # # # # # # # # # # .
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
. # # # # # # # # # # # # # # # # # # # # .
. . # # # # # # # # # # # # # # # # # # . .
`)
let hoch = hackbit_VIEW128x64.matrix6x24(`
. . # # . .
. # # # # .
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
# # # # # #
. # # # # .
. . # # . .
`)
//% blockId=zeigeZahl block="show 7-segment number %num"
//% block.loc.pt-BR="mostrar número %num (7-Segmentos)"
//% group="Show"
//% weight=91
export function zeigeZahl(num: number) {
let zahlAlsText = ""
screenBuf.fill(0) //Puffer löschen
screenBuf[0] = 0x40
set_pos() // auf Start setzen
if (num < 10000 && num >= 0) {
zahlAlsText = convertToText(num)
for (let Index = 0; Index <= zahlAlsText.length - 1; Index++) {
hackbit_VIEW128x64.drawnum(parseFloat(zahlAlsText.substr(Index, 1)), Index + 4 - zahlAlsText.length)
}
} else { //Errormessage
hackbit_VIEW128x64.drawsegment("A", 0)
hackbit_VIEW128x64.drawsegment("D", 0)
hackbit_VIEW128x64.drawsegment("E", 0)
hackbit_VIEW128x64.drawsegment("F", 0)
hackbit_VIEW128x64.drawsegment("G", 0)
hackbit_VIEW128x64.drawsegment("E", 32)
hackbit_VIEW128x64.drawsegment("G", 32)
hackbit_VIEW128x64.drawsegment("E", 64)
hackbit_VIEW128x64.drawsegment("G", 64)
}
hackbit_VIEW128x64.refresh() //Puffer anzeigen
}
//% blockId=drawSegment block="paint segment %seg at position %pos"
//% block.loc.pt-BR="escrever texto %seg na posição %pos"
//% subcategory=advanced
//% group="Show"
export function drawsegment(seg: string, pos: number) {
switch (seg) {
case "A": {
hackbit_VIEW128x64.writeImageOLED(quer, pos + 4, 0)
break
}
case "F": {
hackbit_VIEW128x64.writeImageOLED(hoch, pos, 4)
break
}
case "E": {
hackbit_VIEW128x64.writeImageOLED(hoch, pos, 29)
break
}
case "G": {
hackbit_VIEW128x64.writeImageOLED(quer, pos + 4, 25)
break
}
case "B": {
hackbit_VIEW128x64.writeImageOLED(hoch, pos + 24, 4)
break
}
case "C": {
hackbit_VIEW128x64.writeImageOLED(hoch, pos + 24, 29)
break
}
case "D": {
hackbit_VIEW128x64.writeImageOLED(quer, pos + 4, 50)
break
}
}
}
//% blockId=drawnum block="paint number %num at position %pos"
//% block.loc.pt-BR="escrever número %num na posição %pos"
//% subcategory=advanced
//% group="Show"
export function drawnum(num: number, pos: number) {
switch (pos) {
case 0: {
pos = 0
break
}
case 1: {
pos = 32
break
}
case 2: {
pos = 64
break
}
case 3: {
pos = 96
break
}
}
switch (num) {
case 0: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
hackbit_VIEW128x64.drawsegment("D", pos)
hackbit_VIEW128x64.drawsegment("E", pos)
hackbit_VIEW128x64.drawsegment("F", pos)
break
}
case 1: {
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
break
}
case 2: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("G", pos)
hackbit_VIEW128x64.drawsegment("E", pos)
hackbit_VIEW128x64.drawsegment("D", pos)
break
}
case 3: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
hackbit_VIEW128x64.drawsegment("D", pos)
hackbit_VIEW128x64.drawsegment("G", pos)
break
}
case 4: {
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
hackbit_VIEW128x64.drawsegment("F", pos)
hackbit_VIEW128x64.drawsegment("G", pos)
break
}
case 5: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
hackbit_VIEW128x64.drawsegment("D", pos)
hackbit_VIEW128x64.drawsegment("F", pos)
hackbit_VIEW128x64.drawsegment("G", pos)
break
}
case 6: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
hackbit_VIEW128x64.drawsegment("D", pos)
hackbit_VIEW128x64.drawsegment("E", pos)
hackbit_VIEW128x64.drawsegment("F", pos)
hackbit_VIEW128x64.drawsegment("G", pos)
break
}
case 7: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
break
}
case 8: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
hackbit_VIEW128x64.drawsegment("D", pos)
hackbit_VIEW128x64.drawsegment("E", pos)
hackbit_VIEW128x64.drawsegment("F", pos)
hackbit_VIEW128x64.drawsegment("G", pos)
break
}
case 9: {
hackbit_VIEW128x64.drawsegment("A", pos)
hackbit_VIEW128x64.drawsegment("B", pos)
hackbit_VIEW128x64.drawsegment("C", pos)
hackbit_VIEW128x64.drawsegment("D", pos)
hackbit_VIEW128x64.drawsegment("F", pos)
hackbit_VIEW128x64.drawsegment("G", pos)
break
}
}
}
//% blockId=OLEDstring block="%s"
//% group=Show weight=60
export function OLEDstring(s: string): string {
return s;
}
//% blockId=OLEDnumber block="%n"
//% group=Show
export function OLEDnumber(n: number): number {
return n;
}
//% block="image 16x16"
//% block.loc.pt-BR="imagem 16x16"
//% imageLiteral=1
//% imageLiteralColumns=16
//% imageLiteralRows=16
//% shim=images::createImage
//% weight=50
//% subcategory=paint
export function matrix16x16(i: string): Image {
const im = <Image><any>i;
return im
}
//% block="image 8x8"
//% block.loc.pt-BR="imagem 8x8"
//% imageLiteral=1
//% imageLiteralColumns=8
//% imageLiteralRows=8
//% shim=images::createImage
//% weight=55
//% subcategory=paint
export function matrix8x8(i: string): Image {
const im = <Image><any>i;
return im
}
//% imageLiteral=1
//% imageLiteralColumns=6
//% imageLiteralRows=24
//% shim=images::createImage
export function matrix6x24(i: string): Image {
const im = <Image><any>i;
return im
}
//% imageLiteral=1
//% imageLiteralColumns=22
//% imageLiteralRows=6
//% shim=images::createImage
//% weight=90
export function matrix22x6(i: string): Image {
const im = <Image><any>i;
return im
}
//% help=images/icon-image
//% blockId=cimage block="icon %i"
//% block.loc.pt-BR="ícone %i"
//% i.fieldEditor="imagedropdown"
//% i.fieldOptions.columns="5"
//% i.fieldOptions.width="380"
//% i.fieldOptions.maxRows=4
//% weight=59
//% subcategory=paint
export function iImage(i: IconNames): Image {
return images.iconImage(i)
}
/**
* Writes the matrix into the buffer
* Use "refresh" to show matrix on display
*/
//% block="write matrix %im|x %xpos|y %ypos"
//% block.loc.pt-BR="desenhar matriz %im|x %xpos|y %ypos"
//% weight=65
//% subcategory=paint
export function writeImageOLED(im: Image, xpos: number, ypos: number) {
for (let y = 0; y <= im.height() - 1; y++) {
for (let x = 0; x <= im.width() - 1; x++) {
if ((im.pixel(x, y) ? 1 : 0)) {
setPixelbuffer(x + xpos, y + ypos)
}
}
}
}
/**
* Writes the matrix double sized into the buffer
* Use "refresh" to show matrix on display
*/
//% block="write matrix double %im|x %xpos|y %ypos"
//% block.loc.pt-BR="plotar matriz dupla %im|x %xpos|y %ypos"
//% weight=60
//% subcategory=paint
export function writeImageDouble(im: Image, xpos: number, ypos: number) {
let x2 = 0;
let y2 = 0;
for (let y = 0; y <= im.height() - 1; y++) {
y2 = ypos + y + 1
for (let x = 0; x <= im.width() - 1; x++) {
if ((im.pixel(x, y) ? 1 : 0)) {
x2 = xpos + x + 1
setPixelbuffer(2 * x2 - 1, 2 * y2 - 1)
setPixelbuffer(2 * x2 - 1, 2 * y2)
setPixelbuffer(2 * x2, 2 * y2 - 1)
setPixelbuffer(2 * x2, 2 * y2)
}
}
}
}
/**
* Using the X Y co-ordinates, it is possible to turn on a selected pixel in the buffer. Use refresh to show buffer on display
* @param x is X alis, eg: 0
* @param y is Y alis, eg: 0
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_set_pixelbuffer" block="write pixel in buffer at x %x|y %y"
//% block.loc.pt-BR="escrever pixel no buffer em x %x|y %y"
//% group="Show"
//% weight=70 blockGap=8
//% x.min=0, x.max=127
//% y.min=0, y.max=63
//% inlineInputMode=inline
//% subcategory=advanced
export function setPixelbuffer(x: number, y: number, screen?: 1) {
displayAddress = setScreenAddr(screen)
if (initalised == 0) {
initDisplay()
}
let page = y >> 3
let shift_page = y % 8 //calculate the page to write to
let ind = x + page * 128 + 1 //calculate which register in the page to write to.
let screenPixel = (screenBuf[ind] | (1 << shift_page)) //set the screen data byte
screenBuf[ind] = screenPixel //store data in screen buffer
}
/**
* Using the X Y co-ordinates, it is possible to turn on a selected pixel on the display
* @param x is X alis, eg: 0
* @param y is Y alis, eg: 0
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_set_pixel" block="show pixel at x %x|y %y"
//% block.loc.pt-BR="desenhar pixel em x %x|y %y"
//% group="Draw"
//% weight=70 blockGap=8
//% x.min=0, x.max=127
//% y.min=0, y.max=63
//% inlineInputMode=inline
export function setPixel(x: number, y: number, screen?: 1) {
displayAddress = setScreenAddr(screen)
if (initalised == 0) {
initDisplay()
}
let page = y >> 3
let shift_page = y % 8 //calculate the page to write to
let ind = x + page * 128 + 1 //calculate which register in the page to write to.
let screenPixel = (screenBuf[ind] | (1 << shift_page)) //set the screen data byte
screenBuf[ind] = screenPixel //store data in screen buffer
set_pos(x, page) //set the position on the screen to write at
writeOneByteBuf[0] = 0x40 //load buffer with command
writeOneByteBuf[1] = screenPixel //load buffer with byte
pins.i2cWriteBuffer(displayAddress, writeOneByteBuf) //send data to screen
}
/**
* Using the X Y co-ordinates, it is possible to turn off a selected pixel on the display
* @param x is X alis, eg: 0
* @param y is Y alis, eg: 0
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_clear_pixel" block="clear pixel at x %x|y %y"
//% block.loc.pt-BR="limpar pixel em x %x|y %y"
//% group="Delete"
//% weight=70 blockGap=8
//% x.min=0, x.max=127
//% y.min=0, y.max=63
//% inlineInputMode=inline
export function clearPixel(x: number, y: number, screen?: 1) {
displayAddress = setScreenAddr(screen)
if (initalised == 0) {
initDisplay(1)
}
let page = y >> 3
let shift_page = y % 8 //calculate the page to write to
let ind = x + page * 128 + 1 //calculate which register in the page to write to.
let screenPixel = clearBit(screenBuf[ind], shift_page) //clear the screen data byte
screenBuf[ind] = screenPixel //store data in screen buffer
set_pos(x, page) //set the position on the screen to write at
writeOneByteBuf[0] = 0x40 //load buffer with command
writeOneByteBuf[1] = screenPixel //load buffer with byte
pins.i2cWriteBuffer(displayAddress, writeOneByteBuf) //send data to screen
}
// function should be updated, soon
/**
* show will allow any number, string or variable to be displayed onto the screen.
* Block is expandable to set line and alignment
* @param displayShowAlign is the alignment of the text, this can be left, centre or right
* @param line is line the text to be started on, eg: 1
* @param inputData is the text will be show
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_show" block="show %s|| on line %line| with alignment: %displayShowAlign"
//% block.loc.pt-BR="mostrar %s|| na linha %line| alinhamento: %displayShowAlign"
//% weight=80 blockGap=8
//% group="Show"
//% expandableArgumentMode="enable"
//% inlineInputMode=inline
//% line.min=1 line.max=8
export function show(inputData: any, line?: number, displayShowAlign?: ShowAlign, screen?: 1) {
let y = 0
let x = 0
let inputString = convertToText(inputData)
displayAddress = setScreenAddr(screen)
if (initalised == 0) {
initDisplay(1)
}
if (!displayShowAlign) {//if variable y has not been used, default to y position of 0
displayShowAlign = ShowAlign.Left
}
//if variable y has not been used, default to y position of 0
if (!line) {
y = 0
}
else {
y = (line - 1)
}
//sort text into lines
let lengthOfText = inputString.length
let textLoop = 0
let wordArray: string[] = []
let wordLengthArray: number[] = []
let stringArray: string[] = []
let numberOfWords = 0
let numberOfStrings = 0
let startOfNewWord = 0
let charLength = 0
let word = 0
let createString = ""
//for loop takes the input string and splits into single words by checking for spaces
for (textLoop = 0; textLoop <= inputString.length; textLoop++) {
if (inputString.charAt(textLoop) == " ") {
let splitStr = inputString.substr(startOfNewWord, (textLoop - (startOfNewWord - 1)))
wordArray[numberOfWords] = splitStr
wordLengthArray[numberOfWords] = splitStr.length
numberOfWords += 1
startOfNewWord = textLoop + 1
}
else if (textLoop == inputString.length) {
let splitStr = inputString.substr(startOfNewWord, (textLoop - (startOfNewWord - 1)))
wordArray[numberOfWords] = splitStr + " "
wordLengthArray[numberOfWords] = splitStr.length + 1
numberOfWords += 1
}
}
textLoop = 0
let screenLine = 0
//check the length of words added to string fits on the single line of LCD, if it doesnt start a new line
for (textLoop = 0; textLoop <= numberOfWords; textLoop++) {
if (textLoop == numberOfWords) {
stringArray[numberOfStrings] = createString
numberOfStrings += 1
}
else if ((screenLine + wordLengthArray[textLoop]) <= NUMBER_OF_CHAR_PER_LINE) { //check the current string length plus the next word legnth will fit on the LCD line
createString = createString + wordArray[textLoop] //if it does, add it to the string
screenLine = createString.length //increase the displayed string length to check ready for next word
}
else {
stringArray[numberOfStrings] = createString //save the strings to be displayed on the LCD
numberOfStrings += 1 //add the total number of lines to be displayed created
createString = wordArray[textLoop] //start with next word
screenLine = wordLengthArray[textLoop] //start with the next word length
}
}
let col = 0
let charDisplayBytes = 0
let ind = 0
//add for loop for lines
for (let textLine = 0; textLine <= (numberOfStrings - 1); textLine++) {
inputString = stringArray[textLine]
if (inputString.length < NUMBER_OF_CHAR_PER_LINE) {
while (inputString.length < NUMBER_OF_CHAR_PER_LINE) { //Loop will add white spaces on side of string depending on which alignment
if (displayShowAlign == ShowAlign.Left) {
inputString = inputString + " "
}
else if (displayShowAlign == ShowAlign.Centre) {
if (inputString.length % 2 == 0) {
inputString = " " + inputString + " "
}
else {
inputString = inputString + " "
}
}
else if (displayShowAlign == ShowAlign.Right) {
inputString = " " + inputString
}
}
}
for (let charOfString = 0; charOfString < inputString.length; charOfString++) {
charDisplayBytes = font[inputString.charCodeAt(charOfString)]
for (let i = 0; i < 5; i++) { //for loop will take byte font array and load it into the correct register, the shift to the next byte to load into the next location
col = 0
for (let j = 0; j < 5; j++) {
if (charDisplayBytes & (1 << (5 * i + j)))
col |= (1 << (j + 1))
}
ind = (x + charOfString) * 5 + y * 128 + i + 1
screenBuf[ind] = col
}
}
set_pos(x * 5, y) //set the start position to write to
let ind0 = x * 5 + y * 128
let buf = screenBuf.slice(ind0, ind + 1)
buf[0] = 0x40
pins.i2cWriteBuffer(displayAddress, buf) //send data to the screen
y += 1
}
}
/**
* draw a line using the x and y co-ordinates as a starting point to a given length
* @param lineDirection is the selection of either horizontal line, vertical line or diaganol
* @param x is start position on the X axis, eg: 0
* @param y is start position on the Y axis, eg: 0
* @param len is the length of line, length is the number of pixels, eg: 10
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_draw_line" block="draw a %lineDirection | line with length of %len starting at x %x|y %y"
//% block.loc.pt-BR="desenhar linha %lineDirection | tamanho: %len iniciando em x %x|y %y"
//% weight=72 blockGap=8
//% group="Draw"
//% x.min=0, x.max=127
//% y.min=0, y.max=63
//% len.min=1, len.max=127
//% inlineInputMode=inline
export function drawLine(lineDirection: LineDirectionSelection, len: number, x: number, y: number, screen?: 1) {
if (lineDirection == LineDirectionSelection.horiztonal) {
for (let i = x; i < (x + len); i++) //loop to set the pixel in the horizontal line
setPixel(i, y, screen)
}
else if (lineDirection == LineDirectionSelection.vertical) {
if (len >= 64) { //as length could be max on the x axis, this checks if a vertical lines is draw, max the value to the max of the y axis
len = 63
}
for (let i = y; i < (y + len); i++) //loop to set the pixel in the vertical line
setPixel(x, i, screen)
}
}
/**
* Draw a rectangle using the X and Y coordinates as a starting point, then the width and height can be enter as the number of pixels
* @param width is width of the rectangle, eg: 60
* @param height is height of the rectangle, eg: 30
* @param x is the start position on the X axis, eg: 0
* @param y is the start position on the Y axis, eg: 0
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_draw_rect" block="draw a rectangle %width|wide %height|high from position x %x|y %y"
//% block.loc.pt-BR="desenhar retângulo %width| por %height| na posição x %x|y %y"
//% weight=71 blockGap=8
//% group="Draw"
//% inlineInputMode=inline
//% width.min=1 width.max=127
//% height.min=1 height.max=63
//% x.min=0 x.max=127
//% y.min=0 y.max=63
export function drawRect(width: number, height: number, x: number, y: number, screen?: 1) {
if (!x) //if variable x has not been used, default to x position of 0
x = 0
if (!y) //if variable y has not been used, default to y position of 0
y = 0
//draw the line of each side of the rectangle
drawLine(LineDirectionSelection.horiztonal, width, x, y, screen)
drawLine(LineDirectionSelection.horiztonal, width, x, y + height, screen)
drawLine(LineDirectionSelection.vertical, height, x, y, screen)
drawLine(LineDirectionSelection.vertical, height, x + width, y, screen)
}
/**
* clear screen
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_clear" block="clear display"
//% block.loc.pt-BR="limpar display"
//% group="Delete"
//% weight=63 blockGap=8
export function clear(screen?: number) {
displayAddress = setScreenAddr(screen)
if (initalised == 0) {
initDisplay(1)
}
screenBuf.fill(0) //fill the screenBuf with 0
screenBuf[0] = 0x40
set_pos() //set position to the start of the screen
pins.i2cWriteBuffer(displayAddress, screenBuf) //write clear buffer to the screen
}
/**
* clears the screenbuffer. Refresh display to see changes!
*/
//% blockId="VIEW128x64_clearbuffer" block="clear buffer"
//% block.loc.pt-BR="limpar buffer"
//% group="Delete"
//% subcategory=advanced
//% weight=63 blockGap=8
export function clearbuffer() {
screenBuf.fill(0) //fill the screenBuf with 0
screenBuf[0] = 0x40
set_pos() //set position to the start of the screen
}
/**
* Turn display on and off. The information on the screen will be kept when display when turning on and off
* @param output is the boolean output of the pin, either ON or OFF
* @param screen is screen selection when using multiple screens
*/
//% blockId=VIEW128x64_display_on_off_control
//% block="turn %displayOutput=on_off_toggle| display"
//% block.loc.pt-BR="girar display %displayOutput=on_off_toggle"
//% group="Control"
//% subcategory=advanced
//% expandableArgumentMode="toggle"
//% weight=80 blockGap=8
export function controlDisplayOnOff(displayOutput: boolean, screen?: 1) {
displayAddress = setScreenAddr(screen)
if (initalised == 0) {
initDisplay(1)
}
if (displayOutput == true) {
writeOneByte(0xAF) //turn display output on
}
else {
writeOneByte(0xAE) //turn display output off
}
}
/**
* Render a boolean as an on/off toggle
*/
//% blockId=on_off_toggle
//% block="$on"
//% on.shadow="toggleOnOff"
//% blockHidden=true
export function onOff(on: boolean): boolean {
return on;
}
//////////////////////////////////////
//
// Plotting blocks
//
//////////////////////////////////////
/**
* Plot Request start or stops the plotting of the graph onto the display
* @plotVariable is the variable that the user requires to be recorded on a graph onto the display
* @param screen is screen selection when using multiple screens
*/
//% blockId="VIEW128x64_plot_request"
//% group="Draw"
//% block="plot %plotVariable| onto display"
//% block.loc.pt-BR="plotar %plotVariable| no display"
//% weight=100 blockGap=8
export function plot(plotVariable: number, screen?: 1) {
displayAddress = setScreenAddr(screen)
if (initalised == 0) {
initDisplay(1)
}
let plotLength = plotArray.length
if (plotLength == 127) { //if the length of the array has reach max number of pixels, shift the array and remove the oldest
plotArray.shift()
}
//round the variable to use as ints rather than floats
plotVariable = Math.round(plotVariable)
//place on the end of the array
plotArray.push(plotVariable)
//if the varibale exceeds the scale of the Y axis, update the in or max limits
if (plotVariable > graphYMax)
graphYMax = plotVariable
if (plotVariable < graphYMin)