diff options
| author | nasrlol <nsrddyn@gmail.com> | 2024-11-12 23:38:26 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2024-11-12 23:38:26 +0100 |
| commit | 749e88a3f57320dcbded824dd55c925c520d0158 (patch) | |
| tree | c91531d62f7603625016e14b742a3e98ad906210 | |
| parent | dc9dd804a014c3512da49009eb20298bd377b1ec (diff) | |
switching to gpiozero
| -rw-r--r-- | bin/kasper_gui.py | 7 | ||||
| -rw-r--r-- | bin/kasper_source.py | 64 |
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 @@ | |||
| 1 | import tkinter | 1 | import tkinter |
| 2 | from tkinter import PhotoImage | ||
| 3 | 2 | ||
| 4 | display = tkinter.Tk() | 3 | display = tkinter.Tk() |
| 5 | display.title("Kasper") | 4 | display.title("Kasper") |
| 6 | photo = PhotoImage(file="icon.png") | ||
| 7 | display.iconphoto(False, photo) | ||
| 8 | 5 | ||
| 9 | # making 2 versions IOS AND DESKTOP | 6 | display.mainloop() \ No newline at end of file |
| 10 | # with swiftUI | ||
| 11 | display.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 @@ | |||
| 1 | from smbus2 import SMBus | 1 | from smbus import SMBus |
| 2 | import gpiozero | ||
| 2 | from gpiozero import CPUTemperature | 3 | from gpiozero import CPUTemperature |
| 3 | import speech_recognition as sr | 4 | import speech_recognition as sr |
| 4 | import os | 5 | import os |
| 5 | import time | 6 | import time |
| 6 | 7 | ||
| 7 | # LCD Constants | 8 | Initialize components |
| 8 | LCD_BACKLIGHT = 0x08 | ||
| 9 | LCD_NOBACKLIGHT = 0x00 | ||
| 10 | ENABLE_BIT = 0b00000100 | ||
| 11 | LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} | ||
| 12 | ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} | ||
| 13 | |||
| 14 | # Error Messages | ||
| 15 | ERROR_BAD_REQUEST = "400 Bad Request" | ||
| 16 | ERROR_UNAUTHORIZED = "401 Unauthorized" | ||
| 17 | ERROR_NOT_FOUND = "404 Not Found" | ||
| 18 | ERROR_TIMEOUT = "408 Request Timeout" | ||
| 19 | |||
| 20 | |||
| 21 | # LCD Control Class | ||
| 22 | class 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 | ||
| 67 | lcd = LCD() | 9 | lcd = LCD() |
| 68 | cpu_temp = CPUTemperature() | 10 | cpu_temp = CPUTemperature() |
| 69 | recognizer = sr.Recognizer() | 11 | recognizer = sr.Recognizer() |
