-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlcd.py
140 lines (114 loc) · 3.68 KB
/
lcd.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
# requires RPi_I2C_driver.py
# Python3 version
import RPi_I2C_driver
from time import *
mylcd = RPi_I2C_driver.lcd()
# test 2
mylcd.lcd_display_string("RPi I2C test", 1)
mylcd.lcd_display_string(" Custom chars", 2)
sleep(2) # 2 sec delay
mylcd.lcd_clear()
# let's define a custom icon, consisting of 6 individual characters
# 3 chars in the first row and 3 chars in the second row
fontdata1 = [
# Char 0 - Upper-left
[ 0x00, 0x00, 0x03, 0x04, 0x08, 0x19, 0x11, 0x10 ],
# Char 1 - Upper-middle
[ 0x00, 0x1F, 0x00, 0x00, 0x00, 0x11, 0x11, 0x00 ],
# Char 2 - Upper-right
[ 0x00, 0x00, 0x18, 0x04, 0x02, 0x13, 0x11, 0x01 ],
# Char 3 - Lower-left
[ 0x12, 0x13, 0x1b, 0x09, 0x04, 0x03, 0x00, 0x00 ],
# Char 4 - Lower-middle
[ 0x00, 0x11, 0x1f, 0x1f, 0x0e, 0x00, 0x1F, 0x00 ],
# Char 5 - Lower-right
[ 0x09, 0x19, 0x1b, 0x12, 0x04, 0x18, 0x00, 0x00 ],
# Char 6 - my test
[ 0x1f,0x0,0x4,0xe,0x0,0x1f,0x1f,0x1f],
]
# Load logo chars (fontdata1)
mylcd.lcd_load_custom_chars(fontdata1)
# Write first three chars to row 1 directly
mylcd.lcd_write(0x80)
mylcd.lcd_write_char(0)
mylcd.lcd_write_char(1)
mylcd.lcd_write_char(2)
# Write next three chars to row 2 directly
mylcd.lcd_write(0xC0)
mylcd.lcd_write_char(3)
mylcd.lcd_write_char(4)
mylcd.lcd_write_char(5)
sleep(2)
mylcd.lcd_clear()
mylcd.lcd_display_string_pos("Testing",1,1) # row 1, column 1
sleep(1)
mylcd.lcd_display_string_pos("Testing",2,3) # row 2, column 3
sleep(1)
mylcd.lcd_display_string_pos("Testing",3,4) # row 3, column 3
sleep(1)
mylcd.lcd_display_string_pos("Testing",4,5) # row 4, column 3
sleep(1)
mylcd.lcd_clear()
# Now let's define some more custom characters
fontdata2 = [
# Char 0 - left arrow
[ 0x1,0x3,0x7,0xf,0xf,0x7,0x3,0x1 ],
# Char 1 - left one bar
[ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 ],
# Char 2 - left two bars
[ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 ],
# Char 3 - left 3 bars
[ 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c ],
# Char 4 - left 4 bars
[ 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e ],
# Char 5 - left start
[ 0x0,0x1,0x3,0x7,0xf,0x1f,0x1f,0x1f ],
# Char 6 -
# [ ],
]
# Load logo chars from the second set
mylcd.lcd_load_custom_chars(fontdata2)
block = chr(255) # block character, built-in
# display two blocks in columns 5 and 6 (i.e. AFTER pos. 4) in row 1
# first draw two blocks on 5th column (cols 5 and 6), starts from 0
mylcd.lcd_display_string_pos(block * 2,1,4)
#
pauza = 0.2 # define duration of sleep(x)
#
# now draw cust. chars starting from col. 7 (pos. 6)
pos = 6
mylcd.lcd_display_string_pos(chr(1),1,6)
sleep(pauza)
mylcd.lcd_display_string_pos(chr(2),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(chr(3),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(chr(4),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(block,1,pos)
sleep(pauza)
# and another one, same as above, 1 char-space to the right
pos = pos +1 # increase column by one
mylcd.lcd_display_string_pos(chr(1),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(chr(2),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(chr(3),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(chr(4),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(block,1,pos)
sleep(pauza)
#
# now again load first set of custom chars - smiley
mylcd.lcd_load_custom_chars(fontdata1)
mylcd.lcd_display_string_pos(chr(0),1,9)
mylcd.lcd_display_string_pos(chr(1),1,10)
mylcd.lcd_display_string_pos(chr(2),1,11)
mylcd.lcd_display_string_pos(chr(3),2,9)
mylcd.lcd_display_string_pos(chr(4),2,10)
mylcd.lcd_display_string_pos(chr(5),2,11)
sleep(2)
mylcd.lcd_clear()
sleep(1)
mylcd.backlight(0)