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.py25
1 files changed, 14 insertions, 11 deletions
diff --git a/bin/source/hardware_driver.py b/bin/source/hardware_driver.py
index 1afb219..7d48e69 100644
--- a/bin/source/hardware_driver.py
+++ b/bin/source/hardware_driver.py
@@ -1,15 +1,21 @@
1from smbus2 import SMBus 1from smbus import SMBus
2from time import sleep 2from time import sleep
3 3
4ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} 4ALIGN_FUNC = {
5 'left': 'ljust',
6 'right': 'rjust',
7 'center': 'center'}
5CLEAR_DISPLAY = 0x01 8CLEAR_DISPLAY = 0x01
6ENABLE_BIT = 0b00000100 9ENABLE_BIT = 0b00000100
7LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} 10LINES = {
11 1: 0x80,
12 2: 0xC0,
13 3: 0x94,
14 4: 0xD4}
8 15
9LCD_BACKLIGHT = 0x08 16LCD_BACKLIGHT = 0x08
10LCD_NOBACKLIGHT = 0x00 17LCD_NOBACKLIGHT = 0x00
11 18
12
13class LCD(object): 19class LCD(object):
14 20
15 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): 21 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
@@ -32,7 +38,7 @@ class LCD(object):
32 self.bus.write_byte(self.address, byte) 38 self.bus.write_byte(self.address, byte)
33 self.bus.write_byte(self.address, (byte | ENABLE_BIT)) 39 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
34 sleep(self.delay) 40 sleep(self.delay)
35 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) 41 self.bus.write_byte(self.address,(byte & ~ENABLE_BIT))
36 sleep(self.delay) 42 sleep(self.delay)
37 43
38 def write(self, byte, mode=0): 44 def write(self, byte, mode=0):
@@ -40,10 +46,10 @@ class LCD(object):
40 self._write_byte(mode | (byte & 0xF0) | backlight_mode) 46 self._write_byte(mode | (byte & 0xF0) | backlight_mode)
41 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) 47 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode)
42 48
43 def text(self, text, line, align="left"): 49 def text(self, text, line, align='left'):
44 self.write(LINES.get(line, LINES[1])) 50 self.write(LINES.get(line, LINES[1]))
45 text, other_lines = self.get_text_line(text) 51 text, other_lines = self.get_text_line(text)
46 text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) 52 text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width)
47 for char in text: 53 for char in text:
48 self.write(ord(char), mode=1) 54 self.write(ord(char), mode=1)
49 if other_lines and line <= self.rows - 1: 55 if other_lines and line <= self.rows - 1:
@@ -56,13 +62,10 @@ class LCD(object):
56 def get_text_line(self, text): 62 def get_text_line(self, text):
57 line_break = self.width 63 line_break = self.width
58 if len(text) > self.width: 64 if len(text) > self.width:
59 line_break = text[: self.width + 1].rfind(" ") 65 line_break = text[:self.width + 1].rfind(' ')
60 if line_break < 0: 66 if line_break < 0:
61 line_break = self.width 67 line_break = self.width
62 return text[:line_break], text[line_break:].strip() 68 return text[:line_break], text[line_break:].strip()
63 69
64 def clear(self): 70 def clear(self):
65 self.write(CLEAR_DISPLAY) 71 self.write(CLEAR_DISPLAY)
66
67 def read(self):
68 self._read_i2c_block_data