diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-15 21:33:10 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-15 21:33:10 +0100 |
| commit | 5588a6c6b4d944e0342d5bd3ea54c3dda6aec4d4 (patch) | |
| tree | d65e4b1d90b31cecde8037a3289b4cb6ac3631d1 | |
| parent | 69a64546ae06b0377be8c26f2842e3d92df53778 (diff) | |
updated the code for the i2c display library using the rpi gpio library from github, going to make changes to it later
| -rw-r--r-- | .vscode/settings.json | 5 | ||||
| -rw-r--r-- | I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate | bin | 37489 -> 37459 bytes | |||
| -rw-r--r-- | bin/i2c.py | 66 |
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 @@ | |||
| 1 | { | ||
| 2 | "python.testing.pytestArgs": ["I2C"], | ||
| 3 | "python.testing.unittestEnabled": false, | ||
| 4 | "python.testing.pytestEnabled": true | ||
| 5 | } | ||
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 | |||
| @@ -1,47 +1,65 @@ | |||
| 1 | import smbus2 as SMBus | 1 | from smbus import SMBus |
| 2 | import time | 2 | from time import sleep |
| 3 | |||
| 4 | ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} | ||
| 5 | CLEAR_DISPLAY = 0x01 | ||
| 6 | ENABLE_BIT = 0b00000100 | ||
| 7 | LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} | ||
| 3 | 8 | ||
| 4 | LCD_BACKLIGHT = 0x08 | 9 | LCD_BACKLIGHT = 0x08 |
| 5 | LCD_NOBACKLIGHT = 0x00 | 10 | LCD_NOBACKLIGHT = 0x00 |
| 6 | ENABLE_BIT = 0b00000100 | ||
| 7 | LINES = {1: 0x80, 2: 0xC0} | ||
| 8 | ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} | ||
| 9 | 11 | ||
| 10 | 12 | ||
| 11 | class LCD: | 13 | class LCD(object): |
| 12 | 14 | ||
| 13 | def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): | 15 | def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): |
| 14 | self.address = address | 16 | self.address = address |
| 15 | self.bus = SMBus(bus) | 17 | self.bus = SMBus(bus) |
| 16 | self.width = width | 18 | self.delay = 0.0005 |
| 17 | self.rows = rows | 19 | self.rows = rows |
| 20 | self.width = width | ||
| 18 | self.backlight_status = backlight | 21 | self.backlight_status = backlight |
| 19 | self.delay = 0.0005 | ||
| 20 | 22 | ||
| 21 | for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): | 23 | self.write(0x33) |
| 22 | self.write(cmd) | 24 | self.write(0x32) |
| 23 | time.sleep(self.delay) | 25 | self.write(0x06) |
| 24 | 26 | self.write(0x0C) | |
| 25 | def write(self, byte, mode=0): | 27 | self.write(0x28) |
| 26 | backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT | 28 | self.write(CLEAR_DISPLAY) |
| 27 | self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) | 29 | sleep(self.delay) |
| 28 | 30 | ||
| 29 | def _write_byte(self, byte): | 31 | def _write_byte(self, byte): |
| 30 | self.bus.write_byte(self.address, byte) | 32 | self.bus.write_byte(self.address, byte) |
| 31 | self.bus.write_byte(self.address, (byte | ENABLE_BIT)) | 33 | self.bus.write_byte(self.address, (byte | ENABLE_BIT)) |
| 32 | time.sleep(self.delay) | 34 | sleep(self.delay) |
| 33 | self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) | 35 | self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) |
| 34 | time.sleep(self.delay) | 36 | sleep(self.delay) |
| 35 | 37 | ||
| 36 | def display_text(self, text, line=1, align="left"): | 38 | def write(self, byte, mode=0): |
| 39 | backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT | ||
| 40 | self._write_byte(mode | (byte & 0xF0) | backlight_mode) | ||
| 41 | self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) | ||
| 42 | |||
| 43 | def text(self, text, line, align="left"): | ||
| 37 | self.write(LINES.get(line, LINES[1])) | 44 | self.write(LINES.get(line, LINES[1])) |
| 38 | aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) | 45 | text, other_lines = self.get_text_line(text) |
| 39 | for char in aligned_text: | 46 | text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) |
| 47 | for char in text: | ||
| 40 | self.write(ord(char), mode=1) | 48 | self.write(ord(char), mode=1) |
| 49 | if other_lines and line <= self.rows - 1: | ||
| 50 | self.text(other_lines, line + 1, align=align) | ||
| 41 | 51 | ||
| 42 | def clear(self): | 52 | def backlight(self, turn_on=True): |
| 43 | self.write(0x01) | ||
| 44 | |||
| 45 | def set_backlight(self, turn_on=True): | ||
| 46 | self.backlight_status = turn_on | 53 | self.backlight_status = turn_on |
| 47 | self.write(0) | 54 | self.write(0) |
| 55 | |||
| 56 | def get_text_line(self, text): | ||
| 57 | line_break = self.width | ||
| 58 | if len(text) > self.width: | ||
| 59 | line_break = text[: self.width + 1].rfind(" ") | ||
| 60 | if line_break < 0: | ||
| 61 | line_break = self.width | ||
| 62 | return text[:line_break], text[line_break:].strip() | ||
| 63 | |||
| 64 | def clear(self): | ||
| 65 | self.write(CLEAR_DISPLAY) | ||
