-
Notifications
You must be signed in to change notification settings - Fork 7
/
glcd12864zw.py
1661 lines (1337 loc) · 66.5 KB
/
glcd12864zw.py
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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
# ==============================================================
# Notes from translation
# ==============================================================
#
# English translation from this translation project:
# https://github.com/SrBrahma/RPi-12864-LCD-ST7920-lib
#
# Most of the translation is crappy. It is based on Google Translator.
# There is still missing some words that I can't find anywhere, maybe typos from author.
#
# Czech words between "-- --" are words that I was unable to translate.
# There are few czech words followed by --[possible english translation]--
#
# Unknow terms:
#
# - mikrorow (microrow? but why that name)
# -
#
# ==============================================================
# Functions Index
# ==============================================================
# Note1: these translations are not very good.
# Note2: the Y position is inverted (like most (all?) displays), so the coordinate (0,0) is the top left.
#
# init()
# Basic GPIO port settings - just run it once at the beginning of the program
#
# initTextMode()
# Switches the display to text mode.
# NOTE: However, look like to use functions that use plot(), you need to initTextMode.
#
# initGraphicMode()
# Switches display to graphic mode.
#
# clearText()
# Deletes the content of the text part of the display (the graphics portion remains unchanged)
#
# clearGraphic(pattern)
# Fills the entire contents of the graphical part of the display by the byte.
# (When 0x00 is deleted, 0xFF will fill it with white dots, other values will
# fill the display with different vertical lines). The text part of the display remains unchanged.
#
# clearDisplay()
# Performs both previous deletions at the same time. After returning the display is switched to text mode.
#
# defineIcon(iconId, iconData)
# Defines one of four custom icons
# iconId = icon identifier number 0 to 3
# iconData = a variable that contains an array: 16x double-byte value
#
# printIcon(iconId, x, y)
# The [x, y] coordinates print one of the four custom icons.
# iconId = Icon identifier number 0 to 3
# x = column 0 to 7
# y = line 0 to 3
#
# ==== Graphic font 8x8 pixels
# printCharGraphicMode(code, x, supers, inversion)
# Displays one printCharGraphicMode with the ASCII code "code" at the "x" (0 to 15) coordinates,
# posY (0 to 63) - the y position to
# When "inverse" = True, a dark printCharGraphicMode appears on a light background.
#
# printStringGraphicMode(text, x, supers, inversion)
# Displays text (multiple characters) at "x" and "supers" (parameters same as "printCharGraphicMode ()").
#
# ==== Inside display font
# printCharTextMode(code, x, y) Displays one printCharGraphicMode in text mode at [x, y].
# Code is in the range 1 to 127 (from 32 to 126 it is a classic ASCII)
# X is from 0 to 15
# Y is a line 0 to 3
#
# printStringTextMode(string, x, y)
# Use large characters to display text on the display. Parameters are the
# same as for "big_set ()" and apply to the first printCharGraphicMode of the text.
#
#
# plot(posX, posY, style)
# At "posX" (0 to 127) and "posY" (0 to 63), it drawns, deletes,
# or inverts one pixel. If style = 0 , point deletion is performed,
# style = 1 is drawn, and style = 2 changes the point status on the display.
#
# memPlot(posX, posY, style)
# The same function as the previous "plot()", but the points do not
# appear directly on the display, only in the temporary storage space.
# This feature allows faster drawing. After use, however, it is necessary to transfer
# the contents of that temporary memory to the display using the "memDump()" function.
#
# memDump()
# Transfer the memory contents to the display after using the memPlot() command.
#
# drawHorizontalLine(posY, fromX, toX, style, use_memPlot = 0)
# Drawing a simple horizontal line at a "supery" distance from the top edge with the
# ability to define the beginning and end of the line (variables "from" to "to").
# The "style" parameter is the same as the " plot() " function.
# All the functions that have the use_memPlot argument, can use the memPlot() instead of plot() (read memPlot() explanation)
#
# drawHorizontalLine2(posY, fromX, toX, pattern)
# Faster drawing of the horizontal line. In this case, the parameters "fromX" and "toX"
# are in the range 0 to 7 (they are sixteen pixels on the display). Therefore,
# the line can begin and end only at the coordinates on which the icons are printed.
# The minimum length of such a line is 16 points. The "pattern" parameter specifies
# the style of the line. Depending on the individual bits of that parameter, you can
# set the line full, dashed, dotted, dashed ...
#
# drawVerticalLine(posX, fromY, toY, pattern, use_memPlot = 0)
# Vertical line at arbitrary coordinates.
# posX = X line coordinates in the range 0 to 127
# fromY, toY = y coordinates of the beginning and end of the line in the range 0 to 63
# The "pattern" parameter is the same as in the previous case.
# All the functions that have the use_memPlot argument, can use the memPlot() instead of plot() (read memPlot() explanation)
#
# loadBMP12864(fileName)
# Load a two-color image from the file into the display.
# Beware of the correct file format!
#
# sendByte(rs, byte) Sends 1 byte to the display.
# The data (1) or the command (0) register is selected using the "rs" parameter.
#
# send2Bytes(rs, byte1, byte2)
# It sends 2 bytes at a time.
# The data (1) or the command (0) register is selected using the "rs" parameter.
# ==== NEW FUNCTIONS BELOW!! ====
# They are not present on the original code. They aren't so complicated, but are handy.
# drawGenericLine(fromX, fromY, toX, toY, style = 1, use_memPlot = 0)
# Draws a line from and to the specified coordinates.
# Based on this code: http://itsaboutcs.blogspot.com.br/2015/04/bresenhams-line-drawing-algorithm.html
# All the functions that have the use_memPlot argument, can use the memPlot() instead of plot() (read memPlot() explanation)
#
# drawCircle(circleCenterX, circleCenterY, radius, startDegree = 0, stopDegree = 360, stepDegree = 1, style = 1, use_memPlot = 0):
# The arguments are self-explaining.
# Increasing stepDegree increases the speed of drawing, but may result in missing pixels.
# All the functions that have the use_memPlot argument, can use the memPlot() instead of plot() (read memPlot() explanation)
#
# drawRadiusLine(fromX, fromY, degree, radius, style = 1, use_memPlot = 0):
# Draws a line like a clock hand, where you enter the initial coordinate, the angle
# in degrees and the radius (the size of the line)
# All the functions that have the use_memPlot argument, can use the memPlot() instead of plot() (read memPlot() explanation)
#
# hideShowDisplay(state)
# Hide all active pixels on display, but doesn't remove them from memory.
# If you hide them (True or any value except 0), and then show them again (False or 0),
# all active pixels will show again, on the same place.
# printString3x5(string, leftX, topY, rotation = 0, use_memPlot = 0):
# Prints a string with the font 3x5 pixels.
# rotation = 0: no change in the text.
# rotation = 1: text is turned 90degrees counter-clockwise (and keeps writing up)
# rotation = 2: text is turned 180degrees counter-clockwise (and keeps writing left)
# rotation = 3: text is turned 270degrees counter-clockwise (and keeps writing down)
# leftX and topY are the "top-left" position of the first char that you want to print.
# All the functions that have the use_memPlot argument, can use the memPlot() instead of plot() (read memPlot() explanation)
# ==============================================================
# Last edit: 15.7.2013 (from the original czech code)
# Display 12864 ZW display (128x64 point) SERIAL:
# Display output (purpouse) - connected to ...
# 1 (GND) - RasPi (GPIO GND - pin 6)
# 2 (+ power supply) - RasPi (GPIO +5V - pin 1)
# 3 VO -
# 4 (RS Data/Command) - +5V (CHIP SELECT - In serial communication)
# 5 (R/W Read/Write) - RasPi (Serial data) - GPIO7 (pin26)
# 6 (E - strobe) - RasPi (serial CLOCK) - GPIO8 (pin24)
# 7 (Data bit0) -
# 8 (Data bit1) -
# 9 (Data bit2) -
# 10 (Data bit3) -
# 11 (Data bit4) -
# 12 (Data bit5) -
# 13 (Data bit6) -
# 14 (Data bit7) -
# 15 (PSB) - GND - Set serial communication
# 16 (NC) -
# 17 (Reset) - RasPi - GPIO25(pin22)
# 18 (Vout) -
# 19 (Podsvet - A) - +5V (Or any LED brightness regulator - about 60mA)
# 20 (Podsvet - K) - RasPi (GPIO GND - pin 6)
import os
import time # Various operations with time (pauses)
import RPi.GPIO as GPIO # It can only be used when attaching the E or RS signal to the GPIO in RasPi
import math # It will only be used in the examples of drawing circles
import random # It is used only in the indices for generating random coordinates
# This 3x5 font wasn't present on the original code.
# Based on this font: https://robey.lag.net/2010/01/23/tiny-monospace-font.html
# Changed some chars as I thought it was better, you should do the same!
# Maybe, for code cleansing, I will move this into a file, and the user must loadFont3x5.
char3x5 = [ # KEY | ASCII(dec)
[[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]], # SPACE 32
[[1,1,1,0,1]], # ! 33
[[1,1,0,0,0], [0,0,0,0,0], [1,1,0,0,0]], # " 34
[[1,1,1,1,1], [0,1,0,1,0], [1,1,1,1,1]], # # 35
[[0,1,0,1,0], [1,1,1,1,1], [1,0,1,0,0]], # $ 36
[[1,0,0,1,0], [0,0,1,0,0], [0,1,0,0,1]], # % 37
[[1,1,1,1,0], [1,1,1,0,1], [0,0,1,1,1]], # & 38
[[1,1,0,0,0]], # ' 39
[[0,1,1,1,0], [1,0,0,0,1]], # ( 40
[[1,0,0,0,1], [0,1,1,1,0]], # ) 41
[[1,0,1,0,0], [0,1,0,0,0], [1,0,1,0,0]], # * 42
[[0,0,1,0,0], [0,1,1,1,0], [0,0,1,0,0]], # + 43
[[0,0,0,0,1], [0,0,0,1,0]], # , 44
[[0,0,1,0,0], [0,0,1,0,0], [0,0,1,0,0]], # - 45
[[0,0,0,0,1]], # . 46
[[0,0,0,1,1], [0,0,1,0,0], [1,1,0,0,0]], # / 47
[[1,1,1,1,1], [1,0,0,0,1], [1,1,1,1,1]], # 0 48
[[1,0,0,0,1], [1,1,1,1,1], [0,0,0,0,1]], # 1 49
[[1,0,1,1,1], [1,0,1,0,1], [1,1,1,0,1]], # 2 50
[[1,0,1,0,1], [1,0,1,0,1], [1,1,1,1,1]], # 3 51
[[1,1,1,0,0], [0,0,1,0,0], [1,1,1,1,1]], # 4 52
[[1,1,1,0,1], [1,0,1,0,1], [1,0,1,1,1]], # 5 53
[[1,1,1,1,1], [1,0,1,0,1], [1,0,1,1,1]], # 6 54
[[1,0,0,0,0], [1,0,0,1,1], [1,1,1,0,0]], # 7 55
[[1,1,1,1,1], [1,0,1,0,1], [1,1,1,1,1]], # 8 56
[[1,1,1,0,1], [1,0,1,0,1], [1,1,1,1,1]], # 9 57
[[0,1,0,1,0]], # : 58
[[0,0,0,0,1], [0,1,0,1,0]], # ; 59
[[0,0,1,0,0], [0,1,0,1,0], [1,0,0,0,1]], # < 60
[[0,1,0,1,0], [0,1,0,1,0], [0,1,0,1,0]], # = 61
[[1,0,0,0,1], [0,1,0,1,0], [1,1,1,1,1]], # > 62
[[1,0,0,0,0], [1,0,1,0,1], [1,1,0,0,0]], # ? 63
[[0,1,1,1,0], [1,0,1,0,1], [0,1,1,0,1]], # @ 64
[[1,1,1,1,1], [1,0,1,0,0], [1,1,1,1,1]], # A 65
[[1,1,1,1,1], [1,0,1,0,1], [0,1,0,1,0]], # B 66
[[0,1,1,1,0], [1,0,0,0,1], [1,0,0,0,1]], # C 67
[[1,1,1,1,1], [1,0,0,0,1], [0,1,1,1,0]], # D 68
[[1,1,1,1,1], [1,0,1,0,1], [1,0,1,0,1]], # E 69
[[1,1,1,1,1], [1,0,1,0,0], [1,0,1,0,0]], # F 70
[[0,1,1,1,0], [1,0,1,0,1], [1,0,1,1,1]], # G 71
[[1,1,1,1,1], [0,0,1,0,0], [1,1,1,1,1]], # H 72
[[1,0,0,0,1], [1,1,1,1,1], [1,0,0,0,1]], # I 73
[[0,0,0,1,0], [0,0,0,0,1], [1,1,1,1,0]], # J 74
[[1,1,1,1,1], [0,0,1,0,0], [1,1,0,1,1]], # K 75
[[1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1]], # L 76
[[1,1,1,1,1], [0,1,1,0,0], [1,1,1,1,1]], # M 77
[[1,1,1,1,1], [0,1,1,1,0], [1,1,1,1,1]], # N 78
[[0,1,1,1,0], [1,0,0,0,1], [0,1,1,1,0]], # O 79
[[1,1,1,1,1], [1,0,1,0,0], [0,1,0,0,0]], # P 80
[[0,1,1,1,0], [1,0,0,1,1], [0,1,1,1,1]], # Q 81
[[1,1,1,1,1], [1,0,1,0,0], [0,1,0,1,1]], # R 82
[[0,1,0,0,1], [1,0,1,0,1], [1,0,0,1,0]], # S 83
[[1,0,0,0,0], [1,1,1,1,1], [1,0,0,0,0]], # T 84
[[1,1,1,1,1], [0,0,0,0,1], [1,1,1,1,1]], # U 85
[[1,1,1,1,0], [0,0,0,0,1], [1,1,1,1,0]], # V 86
[[1,1,1,1,1], [0,0,1,1,0], [1,1,1,1,1]], # W 87
[[1,1,0,1,1], [0,0,1,0,0], [1,1,0,1,1]], # X 88
[[1,1,0,0,0], [0,0,1,1,1], [1,1,0,0,0]], # Y 89
[[1,0,0,1,1], [1,0,1,0,1], [1,1,0,0,1]], # Z 90
[[1,1,1,1,1], [1,0,0,0,1]], # [ 91
[[1,1,0,0,0], [0,0,1,0,0], [0,0,0,1,1]], # \ 92
[[1,0,0,0,1], [1,1,1,1,1]], # ] 93
[[0,1,0,0,0], [1,0,0,0,0], [0,1,0,0,0]], # ^ 94
[[0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1]], # _ 95
[[1,1,0,0,0]], # ' 96
[[0,1,0,1,1], [0,1,1,0,1], [0,0,1,1,1]], # a 97
[[1,1,1,1,1], [0,1,0,0,1], [0,0,1,1,0]], # b 98
[[0,0,1,1,0], [0,1,0,0,1], [0,1,0,0,1]], # c 99
[[0,0,1,1,0], [0,1,0,0,1], [1,1,1,1,1]], # d 100
[[0,0,1,1,0], [0,1,0,1,1], [0,1,1,0,1]], # e 101
[[0,0,1,0,0], [0,1,1,1,1], [1,0,1,0,0]], # f 102
[[0,1,1,0,0], [1,0,1,0,1], [1,1,1,1,0]], # g 103
[[1,1,1,1,1], [0,1,0,0,0], [0,0,1,1,1]], # h 104
[[1,0,1,1,1]], # i 105
[[0,0,0,1,0], [0,0,0,0,1], [1,0,1,1,1]], # j 106
[[1,1,1,1,1], [0,0,1,1,0], [0,1,0,0,1]], # k 107
[[1,0,0,0,1], [1,1,1,1,1], [0,0,0,0,1]], # l 108
[[0,1,1,1,1], [0,1,1,1,0], [0,1,1,1,1]], # m 109
[[0,1,1,1,1], [0,1,0,0,0], [0,1,1,1,1]], # n 110
[[0,0,1,1,0], [0,1,0,0,1], [0,0,1,1,0]], # o 111
[[0,1,1,1,1], [0,1,0,1,0], [0,0,1,0,0]], # p 112
[[0,0,1,0,0], [0,1,0,1,0], [0,1,1,1,1]], # q 113
[[0,0,1,1,1], [0,1,0,0,0], [0,1,1,0,0]], # r 114
[[0,0,1,0,1], [0,1,1,1,1], [0,1,0,1,0]], # s 115
[[0,1,0,0,0], [1,1,1,1,1], [0,1,0,0,1]], # t 116
[[0,1,1,1,1], [0,0,0,0,1], [0,1,1,1,1]], # u 117
[[0,1,1,1,0], [0,0,0,0,1], [0,1,1,1,0]], # v 118
[[0,1,1,1,1], [0,0,1,1,1], [0,1,1,1,1]], # w 119
[[0,1,0,0,1], [0,0,1,1,0], [0,1,0,0,1]], # x 120
[[0,1,1,0,1], [0,0,0,1,1], [0,1,1,1,0]], # y 121
[[0,1,0,1,1], [0,1,1,1,1], [0,1,1,0,1]], # z 122
[[0,0,1,0,0], [1,1,0,1,1], [1,0,0,0,1]], # { 123
[[1,1,1,1,1]], # | 124
[[1,0,0,0,1], [1,1,0,1,1], [0,0,1,0,0]], # ] 125
[[0,0,1,0,0], [0,1,1,0,0], [0,1,0,0,0]], # ~ 126
[[1,0,1,0,1], [0,1,0,1,0], [1,0,1,0,1]]] # ERROR "127" = If the ascii is out of range,
# will print this custom char instead.
# Translation of Czech characters from a font file
cz2={}
cz2[345] = 128 # r s hackem
cz2[237] = 129 # i s carkou
cz2[353] = 130 # s s hackem
cz2[382] = 131 # z s hackem
cz2[357] = 132 # t s hackem
cz2[269] = 133 # c s hackem
cz2[253] = 134 # y s carkou
cz2[367] = 135 # u With a ring
cz2[328] = 136 # n s hackem
cz2[250] = 137 # u s carkou
cz2[283] = 138 # e s hackem
cz2[271] = 139 # d s hackem
cz2[225] = 140 # a s carkou
cz2[233] = 141 # e s carkou
cz2[243] = 142 # o s carkou
cz2[344] = 143 # R s hackem
cz2[205] = 144 # I s carkou
cz2[352] = 145 # S s hackem
cz2[381] = 146 # Z s hackem
cz2[356] = 147 # T s hackem
cz2[268] = 148 # C s hackem
cz2[221] = 149 # Y s carkou
cz2[366] = 150 # U With a ring
cz2[327] = 151 # N s hackem
cz2[218] = 152 # U s carkou
cz2[282] = 153 # E s hackem
cz2[270] = 154 # D s hackem
cz2[193] = 155 # A s carkou
cz2[201] = 156 # E s carkou
cz2[211] = 157 # O s carkou
cz2[228] = 228 # pronounced a
cz2[235] = 235 # pronounced e
cz2[239] = 239 # pronounced i
cz2[246] = 246 # pronounced o
cz2[252] = 252 # pronounced u
cz2[196] = 196 # pronounced A
cz2[214] = 214 # pronounced O
cz2[220] = 220 # pronounced U
cz2[176] = 176 # degree
cz2[177] = 177 # plus minus
cz2[171] = 171 # Double arrow on the left
cz2[166] = 166 # Interrupted vertically
cz2[223] = 223 # beta
# Assign GPIO pin
sData_Pin = 7 # (pin 26 = GPIO7) = DATA
sClk_Pin = 8 # (pin 24 = GPIO8) = CLOCK
reset_Pin = 25 # (pin 22 = GPIO25) = RESET
mapa={} # Memory to store the current status of the displayed pixels on the display
txtmapa={} # Memory to which the current text status on the display is saved
font2={} # The memory in which the font is stored is retrieved from the external file
iconData={} # The variable through which the graphical Icons will be defined
#==============================================================
# Main program
#==============================================================
def main():
init() # Basic HW system setup - port directions on the expander and reset the display
clearDisplay(0) # Complete deletion of the display
#==============================================================
# Starts the default examples
#==============================================================
#- - - - - - - - - Writing text to the display - - - - - - - - - - - - - - - - - - - -
initTextMode() # Switch to text mode
printStringTextMode("Viewing pointer",0,0) # Display the text in the text mode at specified coordinates
printStringTextMode("display",4,1)
printStringTextMode("in text",3,2)
printStringTextMode("mode",5,3)
time.sleep(2)
printStringTextMode("chr(1)...chr(32)", 0 , 1)
charCode = 0
for r in range(2, 4):
for s in range (16):
charCode = charCode + 1
printCharTextMode(charCode, s, r) # printCharGraphicMode display in text mode according to its (ASCII) code
time.sleep(3)
printStringTextMode("chr(33)..chr(64)", 0, 1)
for r in range(2, 4):
for s in range (16):
charCode = charCode + 1
printCharTextMode(charCode, s, r)
time.sleep(3)
clearText() # Delete the text part of the display
time.sleep(1)
#- - - - - - - - - Icons - - - - - - - - - - - - - - - - - - - -
printStringTextMode("Own Icons", 0, 0)
# Definition of a zig-zag form of 4 own icons:
# First User-Defined Icon (Focus Crisis)
iconData[0] = 0b0011111111111100
iconData[1] = 0b0111111111111110
iconData[2] = 0b1110000110000111
iconData[3] = 0b1100000110000011
iconData[4] = 0b1100000110000011
iconData[5] = 0b1100000000000011
iconData[6] = 0b1100000000000011
iconData[7] = 0b1111100110011111
iconData[8] = 0b1111100110011111
iconData[9] = 0b1100000000000011
iconData[10] = 0b1100000000000111
iconData[11] = 0b1100000110000011
iconData[12] = 0b1100000110000011
iconData[13] = 0b1110000110000111
iconData[14] = 0b0111111111111110
iconData[15] = 0b0011111111111100
defineIcon(0, iconData)
# Second user-defined icon (square with crash)
iconData[0] = 0b1111111111111111
iconData[1] = 0b1111111111111111
iconData[2] = 0b1110000000000111
iconData[3] = 0b1101000000001011
iconData[4] = 0b1100100000010011
iconData[5] = 0b1100010000100011
iconData[6] = 0b1100001001000011
iconData[7] = 0b1100000110000011
iconData[8] = 0b1100000110000011
iconData[9] = 0b1100001001000011
iconData[10] = 0b1100010000100011
iconData[11] = 0b1100100000010011
iconData[12] = 0b1101000000001011
iconData[13] = 0b1110000000000111
iconData[14] = 0b1111111111111111
iconData[15] = 0b1111111111111111
defineIcon(1, iconData)
# Third user-defined icon (empty square)
iconData[0] = 0b1111111111111111
iconData[1] = 0b1111111111111111
iconData[2] = 0b1100000000000011
iconData[3] = 0b1100000000000011
iconData[4] = 0b1100000000000011
iconData[5] = 0b1100000000000011
iconData[6] = 0b1100000000000011
iconData[7] = 0b1100000000000011
iconData[8] = 0b1100000000000011
iconData[9] = 0b1100000000000011
iconData[10] = 0b1100000000000011
iconData[11] = 0b1100000000000011
iconData[12] = 0b1100000000000011
iconData[13] = 0b1100000000000011
iconData[14] = 0b1111111111111111
iconData[15] = 0b1111111111111111
defineIcon(2, iconData)
# Fourth user-defined icon (crisis in a circle)
iconData[0] = 0b0000011111100000
iconData[1] = 0b0000100110010000
iconData[2] = 0b0011000110001100
iconData[3] = 0b0010000110000100
iconData[4] = 0b0100000110000010
iconData[5] = 0b1000000110000001
iconData[6] = 0b1000000110000001
iconData[7] = 0b1111111111111111
iconData[8] = 0b1111111111111111
iconData[9] = 0b1000000110000001
iconData[10] = 0b1000000110000001
iconData[11] = 0b0100000110000010
iconData[12] = 0b0010000110000100
iconData[13] = 0b0011000110001100
iconData[14] = 0b0000100110010000
iconData[15] = 0b0000011111100000
defineIcon(3, iconData)
printIcon(1, 0, 1) # Icon c.2 at the top of the second row from the top
for icon in range (7): # The rest of the row is filled with random icons
# When you print the icons for yourself, you do not have to set positions for each
icon = random.randint(0, 3) * 2 # The last parameter is double the number of Icons
send2Bytes(1, 0,icon)
printIcon(2, 0, 2) # Icon c.3 at the top of the third row from above
for icon in range (7): # The rest of the row is filled with random icons
# When you print the icons for yourself, you do not have to set positions for each
icon = random.randint(0, 3) * 2 # The last parameter is the double-pin number Icons
send2Bytes(1, 0, icon)
printIcon(3, 0, 3) # Icon c.4 at the bottom of the bottom row
for icon in range (7): # The rest of the lines are filled with c.4
# When you print the icons for yourself, you do not have to set positions for each
send2Bytes(1, 0, 6) # (6 = icon c.4)
time.sleep(2)
printStringTextMode("Change of definition", 0, 0)
time.sleep(2)
printStringTextMode(" Fourth Icons ", 0 , 0)
time.sleep(2)
# When changing the Icons definition, OK to change the appearance of ALL Icons displayed
# Fourth user-defined icon will now be moved to the shuffle
iconData[0] = 0b1111000011110000
iconData[1] = 0b1111000011110000
iconData[2] = 0b1111000011110000
iconData[3] = 0b1111000011110000
iconData[4] = 0b0000111100001111
iconData[5] = 0b0000111100001111
iconData[6] = 0b0000111100001111
iconData[7] = 0b0000111100001111
iconData[8] = 0b1111000011110000
iconData[9] = 0b1111000011110000
iconData[10] = 0b1111000011110000
iconData[11] = 0b1111000011110000
iconData[12] = 0b0000111100001111
iconData[13] = 0b0000111100001111
iconData[14] = 0b0000111100001111
iconData[15] = 0b0000111100001111
defineIcon(3, iconData)
time.sleep(3)
clearText()
#- - - - - - - - -Cinske charactery - - - - - - - - - - - - - - - -
printStringTextMode(" Like Icons " , 0 , 0)
printStringTextMode(" Are displayed " , 0 , 1)
printStringTextMode(" And Czech chars " , 0 , 2)
time.sleep(3)
clearText()
# Print CINSTINY --["nothing"?]-- with 16x16 point font (in text mode)
# These are just randomly chosen charters (nothing to me --nerika--)
# printCharGraphicMode is selected with the help of the last two functions "send2Bytes()"
# The first of these two numbers must be higher than 127
setIconPos(2, 0) # Setting the first printCharGraphicMode position [0,0] to [7,3]
send2Bytes(1, 200, 150)
send2Bytes(1, 218, 10)
send2Bytes(1, 128, 1)
send2Bytes(1, 211, 200)
setIconPos(4, 2) # nastaveni pozice prvniho characteru [0,0] az [7,3]
send2Bytes(1, 240, 4)
send2Bytes(1, 240, 33)
send2Bytes(1, 240, 222)
# Vertical four-letter writing
setIconPos(0, 0) # printCharGraphicMode position setting
send2Bytes(1, 128, 18)
setIconPos(0, 1) # printCharGraphicMode position setting
send2Bytes(1, 154, 251)
setIconPos(0, 2) # printCharGraphicMode position setting
send2Bytes(1, 197, 37)
setIconPos(0, 3) # printCharGraphicMode position setting
send2Bytes(1, 141, 90)
time.sleep(3)
clearText()
#- - - - - - - - - Text in the graphics mode - - - - - - - - - - - - - - - - - - -
# Print 8x8 dot font (in graphic mode)
initGraphicMode()
printStringGraphicMode("The display is y", 0, 0, False) # Normal graphic on the top row
printStringGraphicMode("operate", 3, 15, False)
printStringGraphicMode("also in graphics", 1, 30, False)
printStringGraphicMode("mode", 5, 45, False)
time.sleep(2)
printStringGraphicMode("graphics", 5, 30, True) # Inversely rewritten text
time.sleep(3)
clearGraphic() # Delete the graphical parts of the display
# In the same mode, y print individual characters according to their code
for code in range(32, 128):
column = (code - 32) % 16
row = int((code - 32)/16) * 11 # The spacing between the lines is 11 points
if (row / 22.0 == int(row / 22)): # Every other row is inverse
invert = True
else:
invert = False
printCharGraphicMode(code, column, row, invert) # printCharGraphicMode subroutine in graphics mode
time.sleep(2)
clearGraphic()
for code in range(128, 256):
column = (code - 128) % 16
row = int((code - 128) / 16) * 8
printCharGraphicMode(code, column, row, False)
time.sleep(2)
clearGraphic()
#- - - - - - - Point printing and car - - - - - - - - - - - - - - - - - -
printStringGraphicMode("In this mode", 0, 2,False) # Normal writing 2 pixels from the top edge
printStringGraphicMode("Is y to draw", 0, 11,False)
printStringGraphicMode("Points and line", 0, 20, False)
# Oramovani --[Window?]-- display with full car
drawHorizontalLine ( 0, 0, 127, 1) # Horizontal line at any position
drawHorizontalLine2( 63, 0, 7, 0b11111111) # Speed mountains. Line in a 16-column raster with mask setting
drawVerticalLine ( 0, 0, 63, 0b11111111) # Vertical line in any position with mask setting
drawVerticalLine (127, 0, 63, 0b11111111)
# Internal small rectangle with a carcass
drawHorizontalLine2(31, 1, 6, 0b11001100)
drawHorizontalLine2(56, 1, 6, 0b11001100)
drawVerticalLine ( 16, 31, 56, 0b11001100)
drawVerticalLine (111, 31, 56, 0b11001100)
# Randomly popping a point into a small rectangle in inverse mode
for i in range(2000):
x= int(random.randint(17, 110))
y= int(random.randint(32, 55))
plot(x, y, 2) # Print an inverse point at the coordinates [x, y]
#- - - - - - - - - - - - Different styles of --horizontalnich-- cars - - - - - - - - - - - - - - - - -
clearGraphic()
drawHorizontalLine2( 0, 2, 5, 0b11111111) # Speed mountains. Line in a 16-column raster with mask setting
drawHorizontalLine2(10, 3, 4, 0b11001100) # Speed mountains. Line in a 16-column raster with mask setting
drawHorizontalLine2(20, 2, 5, 0b11110000) # Speed mountains. Line in a 16-column raster with mask setting
drawHorizontalLine2(30, 1, 6, 0b10101010) # Speed mountains. Line in a 16-column raster with mask setting
drawHorizontalLine2(40, 0, 7, 0b11110101) # Speed mountains. Line in a 16-column raster with mask setting
drawHorizontalLine2(50, 1, 6, 0b01110101) # Speed mountains. Line in a 16-column raster with mask setting
time.sleep(3)
clearGraphic(0xff) # To fill the screen with white ink
#- - - - - - - - - - drawing - - - - - - - - - - - - - - - - - - - -
# Draw a double circle using the first print point on the display (slowly)
for circle in range(0, 6283, 4):
x = int(((math.sin(circle / 1000.0) * 30.0)) + 32)
y = int(((math.cos(circle / 1000.0) * 30.0)) + 32)
plot(x, y, 0)
x = int(((math.sin(circle / 1000.0) * 20.0)) + 32)
y = int(((math.cos(circle / 1000.0) * 20.0)) + 32)
plot(x, y, 0)
# Draw five of the circles using the memo
# And then swipe the memory to the display (fast)
for circle in range(0, 6283, 4):
x = int(((math.sin(circle / 1000.0) * 30.0)) + 96)
y = int(((math.cos(circle / 1000.0) * 30.0)) + 32)
memPlot(x, y, 0)
x = int(((math.sin(circle / 1000.0) * 25.0)) + 96)
y = int(((math.cos(circle / 1000.0) * 25.0)) + 32)
memPlot(x, y, 0)
x = int(((math.sin(circle / 1000.0) * 20.0)) + 96)
y = int(((math.cos(circle / 1000.0) * 20.0)) + 32)
memPlot(x, y, 0)
x = int(((math.sin(circle / 1000.0) * 15.0)) + 96)
y = int(((math.cos(circle / 1000.0) * 15.0)) + 32)
memPlot(x, y, 0)
x = int(((math.sin(circle / 1000.0) * 10.0)) + 96)
y = int(((math.cos(circle / 1000.0) * 10.0)) + 32)
memPlot(x, y, 0)
memDump() # Spraying data from memory to display
time.sleep(2)
clearGraphic()
#- - - - - - - - - - Overwrite text and graphic mode - - - - - - - - - -
printStringGraphicMode(" Graphic ", 0, 0, False)
printStringGraphicMode(" And text mode ", 0, 10, False)
printStringGraphicMode(" Y is used ", 0, 20, False)
printStringGraphicMode(" In particular ", 0, 30, False)
time.sleep(2)
clearGraphic()
initTextMode() # Switch the display to text mode
printStringTextMode("Big writing", 2, 0)
printStringTextMode(" In text ", 3, 1)
printStringTextMode(" mode ", 5, 2)
printIcon(1, 0, 2)
printIcon(1, 7, 2)
time.sleep(2)
initGraphicMode() # Switch display to graphic mode
drawHorizontalLine2(53, 0, 7, 0b10011001)
printStringGraphicMode("Graphic row", 1, 56, False)
# When combining text and graphic mode, the common area on the XOR displays
# Example: 4 points thick sikma line over the entire screen
for x in range(128):
plot(x ,x/2 , 1)
plot(x,(x/2) + 1, 1)
plot(x,(x/2) + 2, 1)
plot(x,(x/2) + 3, 1)
time.sleep(2)
printStringGraphicMode("Delete graphics", 0, 56, True)
time.sleep(2)
clearGraphic() # The graphic is deleted separately, so the original text remains
printStringGraphicMode("The text remains", 0, 56, True)
time.sleep(2)
printStringGraphicMode("Make a line ", 0, 56, True)
for x in range(128):
plot(127-x ,x/2 , 1)
plot(127-x,(x/2) + 1, 1)
plot(127-x,(x/2) + 2, 1)
plot(127-x,(x/2) + 3, 1)
time.sleep(2)
printStringGraphicMode(" Delete text ", 0, 56, True)
time.sleep(1)
clearText() # The display will be deleted separately
initGraphicMode()
memDump() # graphic se ale v tom pripade musi obnovit z pameti
time.sleep(1)
printStringGraphicMode("Graphic remains", 0, 56, True)
time.sleep(2)
clearGraphic()
#- - - - - - - - - nahrani obrazku - - - - - - - - - - - - - - - - - -
printStringGraphicMode(" display " ,0, 0,False) #
printStringGraphicMode(" file " ,0, 10,False) #
printStringGraphicMode(" with a image " ,0, 20,False) #
time.sleep(2)
loadBMP12864("./pokladnik.bmp") # Upload bitmaps of 128x64 pixels on the display
time.sleep(4)
# Fill the display with vertical lines (pattern = 0b10101010)
clearDisplay(0b10101010) # The clearDisplay () function remains in the text mode
initGraphicMode() # So you have to switch it to the graphical mode before the next graphic function
printStringGraphicMode("END OF EXAXMPLES :)" , 2 , 25 , True)
exit(0)
#==============================================================
# All subroutines are:
#==============================================================
#==============================================================
# One of the 4 defined 16 x 16 pixel icons at position [x, y]
# X is in the range 0 to 7 (translated to 0, 16, 32, 48, 64, 80, 96, 112)
# Y ranges from 0 to 3 (translated to 0, 16, 32, 48)
def printIcon(iconId, x, y):
shift = x
if (y == 1): shift = shift + 16
if (y == 2): shift = shift + 8
if (y == 3): shift = shift + 24
sendByte(0, 0b10000000 + shift) # Address Counter to the required position
send2Bytes(1, 0, iconId * 2)
#==============================================================
# Draw a horizontal line point by point
def drawHorizontalLine(posY, fromX = 0, toX=127, style = 1, use_memPlot = 0):
if use_memPlot:
customPlot = memPlot
else:
customPlot = plot
for posX in range(fromX, toX + 1):
customPlot(posX, posY, style)
#==============================================================
# Draw horizontal line from the edge to the edge after the bytes
def drawHorizontalLine2(posY = 0, fromByte = 0, toByte = 5, pattern = 0b11111111):
shift = fromByte
if (posY >= 32):
posY = posY - 32
shift = shift + 8
send2Bytes( 0, 0b10000000 + posY, 0b10000000 + shift )
for r in range(toByte - fromByte + 1):
send2Bytes(1, pattern, pattern)
mapa[shift + r, posY, 0] = pattern
mapa[shift + r, posY, 1] = pattern
#==============================================================
# Draw a vertical line using bit masks
# NOTICE: if given a pattern != 255 and if style == 2, style = 1.
def drawVerticalLine(posX, fromY = 0, toY = 63, style = 1, pattern = 255, use_memPlot = 0):
if use_memPlot:
customPlot = memPlot
else:
customPlot = plot
bitSelector = 0 # The bit position in the pattern
pixelStyle = style # Gives it the style value, as plot uses the pixelStyle var.
for posY in range(fromY , toY + 1):
if (pattern != 255):
bitValue = pattern & (0b10000000 >> (bitSelector % 8)) # According to the given pattern, selects individual bits
# Which will either be displayed or deleted on the display
if (bitValue == 0):
pixelStyle = 0
else:
pixelStyle = 1
if (style == 0): # inverts the pixelStyle if the style is == 0 (delete)
pixelStyle = not pixelStyle
bitSelector = bitSelector + 1
customPlot(posX, posY, pixelStyle)
#==============================================================
# Displaying one printCharGraphicMode from the 8x8 point font
# bytePosX = x printCharGraphicMode position (0 to 15, translated to multiples of 8 up to 128), posY = 0 to 63
def printCharGraphicMode(ascii8bitCode, bytePosX, posY , invert = False):
# Control of lightning parameters and their possible override at the limit
if (ascii8bitCode < 32): ascii8bitCode = 32
if (ascii8bitCode > 255): ascii8bitCode = 255
if (bytePosX < 0): bytePosX = 0
if (bytePosX > 15): bytePosX = 15
if (posY > 63): posY = 63
if (posY < 0): posY = 0
ascii8bitCode = ascii8bitCode - 32 # In the font file, the space with the 32 code is defined as the first printCharGraphicMode
posX = bytePosX * 8
# The font of the printCharGraphicMode from the font will be gradually transferred to the byte byte display in 8 steps (top down)
for adr_font in range(ascii8bitCode * 8, (ascii8bitCode * 8) + 8):
# Calculate the horizontal and vertical addresses in the display memory
horiz = posX / 16
dis_adr_y = posY
if (dis_adr_y >= 32):
dis_adr_y = dis_adr_y - 32
horiz = horiz + 8
minibit = posX % 16 # The position of the bit to work with, in the double-bin
send2Bytes(0, 0b10000000 + dis_adr_y, 0b10000000 + horiz) # Setting the graphics address
originalLeftByte = mapa[horiz,dis_adr_y, 0] # To find out the current status of the two-byte on the display
originalRightByte = mapa[horiz,dis_adr_y, 1]
if(minibit < 8): # When the minibit is <8, change only the left byte from the double byte
if (invert == False): # Normal text (write everything that is below the text)
leftByte = font2[adr_font]
if (invert == True): # Inverse text (write everything that is below the text)
leftByte = ~font2[adr_font]
rightByte = originalRightByte # right byte z dvojbyteu bude beze zmeny
else: # When the minibit >= 8, change only the right byte from the double-bin
if (invert == False): # Normal text (write everything that is below the text)
rightByte = font2[adr_font]
if (invert == True): # Inverse text (write everything that is below the text)
rightByte = ~font2[adr_font]
leftByte = originalLeftByte # The left byte of the two-bit will be unchanged
send2Bytes(1, leftByte, rightByte) # Prepress the two bytes to enter the address in the display
mapa[horiz, dis_adr_y, 0] = leftByte # The same value remembers the variable map []
mapa[horiz, dis_adr_y, 1] = rightByte
posY = posY + 1 # Change the current micro-bars by one
#==============================================================
# Displaying several characters behind the 8x8 font
# bytePosX = the position of the first printCharGraphicMode in the text is in column 0 to 15; posY = 0 to 63 (upper margin of the printCharGraphicMode)
def printStringGraphicMode(string, bytePosX, posY, invert = False):
if (isinstance(string, unicode) == False): # If the string is not in unicode, then transfer it
string = unicode(string, "utf-8") # Convert string from UTF-8 to unicode
# All the string scroll printCharGraphicMode after printCharGraphicMode and print
for letter in range (len(string)):
if (bytePosX < 16): # Screen overlay
if (ord(string[letter:letter + 1]) > 127): # As for the ASCII printCharGraphicMode, proceed as described in the table above
try: # If there is no special code defined in the table, ...
printCharGraphicMode(cz2[ord(string[letter:letter + 1])], bytePosX, posY, invert)
except: # ... the program will terminate the error of the non-existent index of the variable cz2 [].
printCharGraphicMode(164, bytePosX, posY, invert) # In this case, replace the undefined printCharGraphicMode printCharGraphicMode "wheel with teckama"
else:
printCharGraphicMode(ord(string[letter:letter + 1]), bytePosX, posY, invert) # ASCII charactery tisknout normalne
bytePosX = bytePosX + 1
#==============================================================
# Load 8x8 point font from file to list "font2 []"
def loadTextFont(fileName):
# Here are two ways to get the font.txt file path. I will leave them there
# for future referencies to myself. You can take them as lessons :)
#
# asbolute path relative to the program caller' path
# fullPathFileName = os.path.join(os.path.dirname(os.path.realpath('__file__')), fileName)
# absolute path relative to this library path (https://stackoverflow.com/a/25612797)
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
fullPathFileName = os.path.join(script_dir, fileName)
# print (fullPathFileName)
fontfile = open(fullPathFileName, "r")
adresafontu = 0
for row in fontfile:
rozlozeno = row.split(",") # Saturation of individual bytes from one line ...
for byte in range(8): # 8 byte on one row in a file
font2[adresafontu] = int(rozlozeno[byte][-4:], 0) # ... and save everyone on the list
adresafontu = adresafontu + 1
fontfile.close()
#==============================================================
# Subroutine for string display obrim font (8x16 point)
# Font definition is a part of the ROM in the display - therefore not Czech characters
# bytePosX = Initial column where the string will begin to print [0 to 16]
# Row is in the range [0 to 3]
def printStringTextMode(string, column, row):
if (len(string) + column > 16): # If the line is longer than 16 characters,
string = string[0:16 - column] # ... so the end is cut off
setTextCursorPos(column, row) # The start position of the text is sent to the display
for printCharGraphicMode in range(len(string)):
sendByte(1, ord(string[printCharGraphicMode:printCharGraphicMode+1])) # Characters from the text are gradually streaked into the display
pomtext = txtmapa[row][:column + printCharGraphicMode] + string[printCharGraphicMode:printCharGraphicMode + 1] + txtmapa[row][column+printCharGraphicMode+1:]
txtmapa[row] = pomtext # Memory for text mode
#==============================================================
# Single printCharGraphicMode display in text mode
def printCharTextMode(code, bytePosX, row):
setTextCursorPos(bytePosX, row) # The start position of the text is sent to the display
sendByte(1, code) # The printCharGraphicMode code is sent to the display
pomtext = txtmapa[row][:bytePosX] + chr(code) + txtmapa[row][bytePosX + 1:]
txtmapa[row] = pomtext # Memory for text mode
#==============================================================
# Print position setting for text mode (for characters 8x16 point)
# Column (0 to 15) and line (0 to 3)
def setTextCursorPos(column , row):
shift = column
if (row == 1): shift = column + 32
if (row == 2): shift = column + 16
if (row == 3): shift = column + 48
sendByte( 0, 0b10000000 + int(shift / 2)) # Address Counter na pozadovanou pozici
# In the case of --lichen-- columns, the printCharGraphicMode must be filled in with the printCharGraphicMode on the display before the new printout