summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornasrlol <nsrddyn@gmail.com>2024-11-12 23:38:26 +0100
committernasrlol <nsrddyn@gmail.com>2024-11-12 23:38:26 +0100
commit749e88a3f57320dcbded824dd55c925c520d0158 (patch)
treec91531d62f7603625016e14b742a3e98ad906210
parentdc9dd804a014c3512da49009eb20298bd377b1ec (diff)
switching to gpiozero
-rw-r--r--bin/kasper_gui.py7
-rw-r--r--bin/kasper_source.py64
2 files changed, 4 insertions, 67 deletions
diff --git a/bin/kasper_gui.py b/bin/kasper_gui.py
index d94784d..e5edc6a 100644
--- a/bin/kasper_gui.py
+++ b/bin/kasper_gui.py
@@ -1,11 +1,6 @@
1import tkinter 1import tkinter
2from tkinter import PhotoImage
3 2
4display = tkinter.Tk() 3display = tkinter.Tk()
5display.title("Kasper") 4display.title("Kasper")
6photo = PhotoImage(file="icon.png")
7display.iconphoto(False, photo)
8 5
9# making 2 versions IOS AND DESKTOP 6display.mainloop() \ No newline at end of file
10# with swiftUI
11display.mainloop()
diff --git a/bin/kasper_source.py b/bin/kasper_source.py
index f4f7947..397c278 100644
--- a/bin/kasper_source.py
+++ b/bin/kasper_source.py
@@ -1,69 +1,11 @@
1from smbus2 import SMBus 1from smbus import SMBus
2import gpiozero
2from gpiozero import CPUTemperature 3from gpiozero import CPUTemperature
3import speech_recognition as sr 4import speech_recognition as sr
4import os 5import os
5import time 6import time
6 7
7# LCD Constants 8Initialize components
8LCD_BACKLIGHT = 0x08
9LCD_NOBACKLIGHT = 0x00
10ENABLE_BIT = 0b00000100
11LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4}
12ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
13
14# Error Messages
15ERROR_BAD_REQUEST = "400 Bad Request"
16ERROR_UNAUTHORIZED = "401 Unauthorized"
17ERROR_NOT_FOUND = "404 Not Found"
18ERROR_TIMEOUT = "408 Request Timeout"
19
20
21# LCD Control Class
22class LCD:
23 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
24 self.address = address
25 self.bus = SMBus(bus)
26 self.width = width
27 self.rows = rows
28 self.backlight_status = backlight
29 self.delay = 0.0005
30
31 # LCD Initialization
32 for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01):
33 self.write(cmd)
34 time.sleep(self.delay)
35
36 def write(self, byte, mode=0):
37 """Send a command or character to the LCD."""
38 backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
39 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight)
40
41 def _write_byte(self, byte):
42 """Write a byte to the I2C bus."""
43 self.bus.write_byte(self.address, byte)
44 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
45 time.sleep(self.delay)
46 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
47 time.sleep(self.delay)
48
49 def display_text(self, text, line=1, align="left"):
50 """Display text on a specified line with alignment."""
51 self.write(LINES.get(line, LINES[1]))
52 aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
53 for char in aligned_text:
54 self.write(ord(char), mode=1)
55
56 def clear(self):
57 """Clear the display."""
58 self.write(0x01)
59
60 def set_backlight(self, turn_on=True):
61 """Toggle backlight on or off."""
62 self.backlight_status = turn_on
63 self.write(0)
64
65
66# Initialize components
67lcd = LCD() 9lcd = LCD()
68cpu_temp = CPUTemperature() 10cpu_temp = CPUTemperature()
69recognizer = sr.Recognizer() 11recognizer = sr.Recognizer()