summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.vscode/settings.json5
-rw-r--r--I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstatebin37489 -> 37459 bytes
-rw-r--r--bin/i2c.py66
3 files changed, 47 insertions, 24 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..66f540e
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,5 @@
+{
+ "python.testing.pytestArgs": ["I2C"],
+ "python.testing.unittestEnabled": false,
+ "python.testing.pytestEnabled": true
+}
diff --git a/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate b/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate
index 07340b1..6b96e8d 100644
--- a/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate
+++ b/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate
Binary files differ
diff --git a/bin/i2c.py b/bin/i2c.py
index a79d9d1..75a3509 100644
--- a/bin/i2c.py
+++ b/bin/i2c.py
@@ -1,47 +1,65 @@
-import smbus2 as SMBus
-import time
+from smbus import SMBus
+from time import sleep
+
+ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
+CLEAR_DISPLAY = 0x01
+ENABLE_BIT = 0b00000100
+LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4}
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00
-ENABLE_BIT = 0b00000100
-LINES = {1: 0x80, 2: 0xC0}
-ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
-class LCD:
+class LCD(object):
def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
self.address = address
self.bus = SMBus(bus)
- self.width = width
+ self.delay = 0.0005
self.rows = rows
+ self.width = width
self.backlight_status = backlight
- self.delay = 0.0005
- for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01):
- self.write(cmd)
- time.sleep(self.delay)
-
- def write(self, byte, mode=0):
- backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
- self._write_byte(mode | ((byte << 4) & 0xF0) | backlight)
+ self.write(0x33)
+ self.write(0x32)
+ self.write(0x06)
+ self.write(0x0C)
+ self.write(0x28)
+ self.write(CLEAR_DISPLAY)
+ sleep(self.delay)
def _write_byte(self, byte):
self.bus.write_byte(self.address, byte)
self.bus.write_byte(self.address, (byte | ENABLE_BIT))
- time.sleep(self.delay)
+ sleep(self.delay)
self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
- time.sleep(self.delay)
+ sleep(self.delay)
- def display_text(self, text, line=1, align="left"):
+ def write(self, byte, mode=0):
+ backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
+ self._write_byte(mode | (byte & 0xF0) | backlight_mode)
+ self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode)
+
+ def text(self, text, line, align="left"):
self.write(LINES.get(line, LINES[1]))
- aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
- for char in aligned_text:
+ text, other_lines = self.get_text_line(text)
+ text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
+ for char in text:
self.write(ord(char), mode=1)
+ if other_lines and line <= self.rows - 1:
+ self.text(other_lines, line + 1, align=align)
- def clear(self):
- self.write(0x01)
-
- def set_backlight(self, turn_on=True):
+ def backlight(self, turn_on=True):
self.backlight_status = turn_on
self.write(0)
+
+ def get_text_line(self, text):
+ line_break = self.width
+ if len(text) > self.width:
+ line_break = text[: self.width + 1].rfind(" ")
+ if line_break < 0:
+ line_break = self.width
+ return text[:line_break], text[line_break:].strip()
+
+ def clear(self):
+ self.write(CLEAR_DISPLAY)