summaryrefslogtreecommitdiff
path: root/bin/source/hardware_driver.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/source/hardware_driver.py')
-rw-r--r--bin/source/hardware_driver.py71
1 files changed, 0 insertions, 71 deletions
diff --git a/bin/source/hardware_driver.py b/bin/source/hardware_driver.py
deleted file mode 100644
index 7d48e69..0000000
--- a/bin/source/hardware_driver.py
+++ /dev/null
@@ -1,71 +0,0 @@
1from smbus import SMBus
2from time import sleep
3
4ALIGN_FUNC = {
5 'left': 'ljust',
6 'right': 'rjust',
7 'center': 'center'}
8CLEAR_DISPLAY = 0x01
9ENABLE_BIT = 0b00000100
10LINES = {
11 1: 0x80,
12 2: 0xC0,
13 3: 0x94,
14 4: 0xD4}
15
16LCD_BACKLIGHT = 0x08
17LCD_NOBACKLIGHT = 0x00
18
19class LCD(object):
20
21 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
22 self.address = address
23 self.bus = SMBus(bus)
24 self.delay = 0.0005
25 self.rows = rows
26 self.width = width
27 self.backlight_status = backlight
28
29 self.write(0x33)
30 self.write(0x32)
31 self.write(0x06)
32 self.write(0x0C)
33 self.write(0x28)
34 self.write(CLEAR_DISPLAY)
35 sleep(self.delay)
36
37 def _write_byte(self, byte):
38 self.bus.write_byte(self.address, byte)
39 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
40 sleep(self.delay)
41 self.bus.write_byte(self.address,(byte & ~ENABLE_BIT))
42 sleep(self.delay)
43
44 def write(self, byte, mode=0):
45 backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
46 self._write_byte(mode | (byte & 0xF0) | backlight_mode)
47 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode)
48
49 def text(self, text, line, align='left'):
50 self.write(LINES.get(line, LINES[1]))
51 text, other_lines = self.get_text_line(text)
52 text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width)
53 for char in text:
54 self.write(ord(char), mode=1)
55 if other_lines and line <= self.rows - 1:
56 self.text(other_lines, line + 1, align=align)
57
58 def backlight(self, turn_on=True):
59 self.backlight_status = turn_on
60 self.write(0)
61
62 def get_text_line(self, text):
63 line_break = self.width
64 if len(text) > self.width:
65 line_break = text[:self.width + 1].rfind(' ')
66 if line_break < 0:
67 line_break = self.width
68 return text[:line_break], text[line_break:].strip()
69
70 def clear(self):
71 self.write(CLEAR_DISPLAY)