-
Notifications
You must be signed in to change notification settings - Fork 0
/
HD44780Display.py
88 lines (73 loc) · 1.96 KB
/
HD44780Display.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
import time
class HD44780Display:
def __init__(self, write_pin_fn, pin_mode_fn, rs, en, d4, d5, d6, d7, light):
self.write_pin = write_pin_fn
self.pin_mode = pin_mode_fn
self.rs = rs
self.en = en
self.d4 = d4
self.d5 = d5
self.d6 = d6
self.d7 = d7
self.light = light
self.pin_mode(rs, 1)
self.pin_mode(en, 1)
self.pin_mode(d4, 1)
self.pin_mode(d5, 1)
self.pin_mode(d6, 1)
self.pin_mode(d7, 1)
self.pin_mode(light, 1)
self.delay = 0.0005
def begin(self):
# Perform initialization sequence
self._write_bits(0x3)
self._write_bits(0x3)
self._write_bits(0x3)
self._write_bits(0x2)
self._write_bits(0x0)
self._write_bits(0x6)
self._write_bits(0x0)
self._write_bits(0xC)
self._write_bits(0x2)
self._write_bits(0x8)
self._write_bits(0x0)
self._write_bits(0x1)
def backlight(self, mode):
self.write_pin(self.light, mode)
def write(self, msg):
for char in msg:
char_code = ord(char)
top_half = (char_code & 0xF0) >> 4
bottom_half = char_code & 0x0F
self._write_bits(top_half, 1)
self._write_bits(bottom_half, 1)
def clear(self):
self._write_bits(0x0)
self._write_bits(0x1)
def set_line(self, line):
if line == 1:
self._write_bits(0x8)
self._write_bits(0x0)
elif line == 2:
self._write_bits(0xC)
self._write_bits(0x0)
else:
raise Exception("Invalid line")
def _toggle_enable(self):
time.sleep(self.delay)
self.write_pin(self.en, 1)
time.sleep(self.delay)
self.write_pin(self.en, 0)
time.sleep(self.delay)
def _write_bits(self, bits, mode=0):
# Split integer into bits
print(bits)
d7, d6, d5, d4 = map(int, "{0:04b}".format(bits))
self.write_pin(self.rs, mode)
time.sleep(self.delay)
self.write_pin(self.d4, d4)
self.write_pin(self.d5, d5)
self.write_pin(self.d6, d6)
self.write_pin(self.d7, d7)
print [d4, d5, d6, d7]
self._toggle_enable()