summaryrefslogtreecommitdiff
path: root/bin/i2c.py
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2024-11-14 19:31:03 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2024-11-14 19:31:03 +0100
commitfe441f69a4632e5245588574923ef8dc467eced5 (patch)
treec9975c3b706a4c810ba975c2884c748d7255c770 /bin/i2c.py
parent9ebcbfc83a64b8a27d0725dbd12f6a8fdb042934 (diff)
tried implementing ollama
Diffstat (limited to 'bin/i2c.py')
-rw-r--r--bin/i2c.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/bin/i2c.py b/bin/i2c.py
new file mode 100644
index 0000000..a79d9d1
--- /dev/null
+++ b/bin/i2c.py
@@ -0,0 +1,47 @@
1import smbus2 as SMBus
2import time
3
4LCD_BACKLIGHT = 0x08
5LCD_NOBACKLIGHT = 0x00
6ENABLE_BIT = 0b00000100
7LINES = {1: 0x80, 2: 0xC0}
8ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
9
10
11class LCD:
12
13 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
14 self.address = address
15 self.bus = SMBus(bus)
16 self.width = width
17 self.rows = rows
18 self.backlight_status = backlight
19 self.delay = 0.0005
20
21 for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01):
22 self.write(cmd)
23 time.sleep(self.delay)
24
25 def write(self, byte, mode=0):
26 backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
27 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight)
28
29 def _write_byte(self, byte):
30 self.bus.write_byte(self.address, byte)
31 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
32 time.sleep(self.delay)
33 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
34 time.sleep(self.delay)
35
36 def display_text(self, text, line=1, align="left"):
37 self.write(LINES.get(line, LINES[1]))
38 aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
39 for char in aligned_text:
40 self.write(ord(char), mode=1)
41
42 def clear(self):
43 self.write(0x01)
44
45 def set_backlight(self, turn_on=True):
46 self.backlight_status = turn_on
47 self.write(0)