forked from sourceperl/rpi.lcd-i2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.py
153 lines (144 loc) · 3.58 KB
/
examples.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
# requires RPi_I2C_driver.py
import RPi_I2C_LCD
from time import *
lcd = RPi_I2C_LCD.LCD()
lcd.set_backlight(True)
# first banner
lcd.set_cursor(row=0)
lcd.message("LCD 4x20 chars I2C")
lcd.set_cursor(row=1)
lcd.message("line 2")
lcd.set_cursor(row=2)
lcd.message("12345678901234567890")
lcd.set_cursor(row=3)
lcd.message("line 4")
sleep(2)
# funny face
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
font_data_1 = [
# 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 (font_data_1)
lcd.load_custom_chars(font_data_1)
# Write first three chars to row 1 directly
lcd.set_cursor(col=8, row=1)
lcd.write_char(0)
lcd.write_char(1)
lcd.write_char(2)
# Write next three chars to row 2 directly
lcd.set_cursor(col=8, row=2)
lcd.write_char(3)
lcd.write_char(4)
lcd.write_char(5)
sleep(4)
# Testing on/off back light
lcd.clear()
lcd.set_backlight(False)
lcd.set_cursor(row=0)
lcd.message("turn off back light")
lcd.set_cursor(row=1)
lcd.message("turn on in 4s")
sleep(4)
lcd.set_backlight(True)
# Testing lines...
lcd.clear()
lcd.set_cursor(row=0)
lcd.message("Testing")
sleep(1)
lcd.set_cursor(row=1, col=2)
lcd.message("Testing")
sleep(1)
lcd.set_cursor(row=2, col=4)
lcd.message("Testing")
sleep(1)
lcd.set_cursor(row=3, col=6)
lcd.message("Testing")
sleep(4)
lcd.clear()
# Now let's define some more custom characters
font_data_2 = [
# 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
lcd.load_custom_chars(font_data_2)
# display two blocks in columns 5 and 6 (i.e. AFTER col. 4) in row 1
# first draw two blocks on 5th column (cols 5 and 6), starts from 0
lcd.home()
lcd.message(chr(255)*2)
# draw progress bar
col = 2
lcd.set_cursor(col, 0)
lcd.write_char(1)
sleep(0.2)
lcd.set_cursor(col, 0)
lcd.write_char(2)
sleep(0.2)
lcd.set_cursor(col, 0)
lcd.write_char(3)
lcd.set_cursor(col, 0)
lcd.write_char(4)
sleep(0.2)
lcd.set_cursor(col, 0)
lcd.write_char(255)
sleep(0.2)
# next column
col += 1
lcd.set_cursor(col, 0)
lcd.write_char(1)
sleep(0.2)
lcd.set_cursor(col, 0)
lcd.write_char(2)
sleep(0.2)
lcd.set_cursor(col, 0)
lcd.write_char(3)
lcd.set_cursor(col, 0)
lcd.write_char(4)
sleep(0.2)
lcd.set_cursor(col, 0)
lcd.write_char(255)
sleep(0.2)
# load again funny face and display it
lcd.load_custom_chars(font_data_1)
lcd.set_cursor(row=1, col=9)
lcd.write_char(0)
lcd.set_cursor(row=1, col=10)
lcd.write_char(1)
lcd.set_cursor(row=1, col=11)
lcd.write_char(2)
lcd.set_cursor(row=2, col=9)
lcd.write_char(3)
lcd.set_cursor(row=2, col=10)
lcd.write_char(4)
lcd.set_cursor(row=2, col=11)
lcd.write_char(5)
sleep(2)
lcd.clear()
lcd.set_backlight(False)