From 749e88a3f57320dcbded824dd55c925c520d0158 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Tue, 12 Nov 2024 23:38:26 +0100 Subject: switching to gpiozero --- bin/kasper_gui.py | 7 +----- 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 @@ import tkinter -from tkinter import PhotoImage display = tkinter.Tk() display.title("Kasper") -photo = PhotoImage(file="icon.png") -display.iconphoto(False, photo) -# making 2 versions IOS AND DESKTOP -# with swiftUI -display.mainloop() +display.mainloop() \ No newline at end of file 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 @@ -from smbus2 import SMBus +from smbus import SMBus +import gpiozero from gpiozero import CPUTemperature import speech_recognition as sr import os import time -# LCD Constants -LCD_BACKLIGHT = 0x08 -LCD_NOBACKLIGHT = 0x00 -ENABLE_BIT = 0b00000100 -LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} -ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} - -# Error Messages -ERROR_BAD_REQUEST = "400 Bad Request" -ERROR_UNAUTHORIZED = "401 Unauthorized" -ERROR_NOT_FOUND = "404 Not Found" -ERROR_TIMEOUT = "408 Request Timeout" - - -# LCD Control Class -class LCD: - def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): - self.address = address - self.bus = SMBus(bus) - self.width = width - self.rows = rows - self.backlight_status = backlight - self.delay = 0.0005 - - # LCD Initialization - for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): - self.write(cmd) - time.sleep(self.delay) - - def write(self, byte, mode=0): - """Send a command or character to the LCD.""" - backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT - self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) - - def _write_byte(self, byte): - """Write a byte to the I2C bus.""" - self.bus.write_byte(self.address, byte) - self.bus.write_byte(self.address, (byte | ENABLE_BIT)) - time.sleep(self.delay) - self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) - time.sleep(self.delay) - - def display_text(self, text, line=1, align="left"): - """Display text on a specified line with alignment.""" - self.write(LINES.get(line, LINES[1])) - aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) - for char in aligned_text: - self.write(ord(char), mode=1) - - def clear(self): - """Clear the display.""" - self.write(0x01) - - def set_backlight(self, turn_on=True): - """Toggle backlight on or off.""" - self.backlight_status = turn_on - self.write(0) - - -# Initialize components +Initialize components lcd = LCD() cpu_temp = CPUTemperature() recognizer = sr.Recognizer() -- cgit v1.2.3-70-g09d2 From 0a7689fbf896aa3629f9783c4316a4b453928b6f Mon Sep 17 00:00:00 2001 From: nasrlol Date: Wed, 13 Nov 2024 00:15:05 +0100 Subject: switching back to the previous library --- bin/kasper_source.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/bin/kasper_source.py b/bin/kasper_source.py index 397c278..bd61942 100644 --- a/bin/kasper_source.py +++ b/bin/kasper_source.py @@ -1,15 +1,65 @@ from smbus import SMBus -import gpiozero from gpiozero import CPUTemperature -import speech_recognition as sr +import speech_recognition as speech import os import time -Initialize components +# LCD Constants +LCD_BACKLIGHT = 0x08 +LCD_NOBACKLIGHT = 0x00 +ENABLE_BIT = 0b00000100 +LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} +ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} + +# Error Messages +ERROR_BAD_REQUEST = "400 Bad Request" +ERROR_UNAUTHORIZED = "401 Unauthorized" +ERROR_NOT_FOUND = "404 Not Found" +ERROR_TIMEOUT = "408 Request Timeout" + +# LCD Control Class +class LCD: + def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): + self.address = address + self.bus = SMBus(bus) + self.width = width + self.rows = rows + self.backlight_status = backlight + self.delay = 0.0005 + # LCD Initialization + for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): + self.write(cmd) + time.sleep(self.delay) + def write(self, byte, mode=0): + """Send a command or character to the LCD.""" + backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT + self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) + def _write_byte(self, byte): + """Write a byte to the I2C bus.""" + self.bus.write_byte(self.address, byte) + self.bus.write_byte(self.address, (byte | ENABLE_BIT)) + time.sleep(self.delay) + self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) + time.sleep(self.delay) + def display_text(self, text, line=1, align="left"): + """Display text on a specified line with alignment.""" + self.write(LINES.get(line, LINES[1])) + aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) + for char in aligned_text: + self.write(ord(char), mode=1) + def clear(self): + """Clear the display.""" + self.write(0x01) + def set_backlight(self, turn_on=True): + """Toggle backlight on or off.""" + self.backlight_status = turn_on + self.write(0) + +# Initialize components lcd = LCD() cpu_temp = CPUTemperature() -recognizer = sr.Recognizer() -microphone = sr.Microphone() +recognizer = speech.Recognizer() +microphone = speech.Microphone() # Display Functions @@ -44,10 +94,10 @@ def recognize_speech(): lcd.clear() lcd.display_text(text, line=1) print("Speech recognized:", text) - except sr.UnknownValueError: + except speech.UnknownValueError: lcd.display_text(ERROR_BAD_REQUEST, line=1) print(ERROR_BAD_REQUEST) - except sr.RequestError: + except s.RequestError: lcd.display_text(ERROR_UNAUTHORIZED, line=1) print(ERROR_UNAUTHORIZED) -- cgit v1.2.3-70-g09d2 From ecd194028a652cc29f300e4a6ca24675b5b8d9ba Mon Sep 17 00:00:00 2001 From: nasrlol Date: Wed, 13 Nov 2024 00:30:41 +0100 Subject: fixed the speechtranscriber bug --- bin/kasper_source.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/kasper_source.py b/bin/kasper_source.py index bd61942..d878f6c 100644 --- a/bin/kasper_source.py +++ b/bin/kasper_source.py @@ -19,6 +19,7 @@ ERROR_TIMEOUT = "408 Request Timeout" # LCD Control Class class LCD: + def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): self.address = address self.bus = SMBus(bus) @@ -26,14 +27,17 @@ class LCD: self.rows = rows self.backlight_status = backlight self.delay = 0.0005 + # LCD Initialization for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): self.write(cmd) time.sleep(self.delay) + def write(self, byte, mode=0): """Send a command or character to the LCD.""" backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) + def _write_byte(self, byte): """Write a byte to the I2C bus.""" self.bus.write_byte(self.address, byte) @@ -41,15 +45,18 @@ class LCD: time.sleep(self.delay) self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) time.sleep(self.delay) + def display_text(self, text, line=1, align="left"): """Display text on a specified line with alignment.""" self.write(LINES.get(line, LINES[1])) aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) for char in aligned_text: self.write(ord(char), mode=1) + def clear(self): """Clear the display.""" self.write(0x01) + def set_backlight(self, turn_on=True): """Toggle backlight on or off.""" self.backlight_status = turn_on @@ -97,7 +104,7 @@ def recognize_speech(): except speech.UnknownValueError: lcd.display_text(ERROR_BAD_REQUEST, line=1) print(ERROR_BAD_REQUEST) - except s.RequestError: + except speech.RequestError: lcd.display_text(ERROR_UNAUTHORIZED, line=1) print(ERROR_UNAUTHORIZED) -- cgit v1.2.3-70-g09d2 From 16c71d8ddc79efff880346c39e918cf07c8abfdf Mon Sep 17 00:00:00 2001 From: nasrlol Date: Wed, 13 Nov 2024 00:33:48 +0100 Subject: clearning the dispplauy with every fuinction hoping it fixes the display --- bin/kasper_source.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bin/kasper_source.py b/bin/kasper_source.py index d878f6c..b7d8060 100644 --- a/bin/kasper_source.py +++ b/bin/kasper_source.py @@ -71,6 +71,8 @@ microphone = speech.Microphone() # Display Functions def display_cpu_info(): + # clearing the display before accessing it + lcd.clear() """Display CPU load and temperature on the LCD.""" while True: load = os.getloadavg()[0] # 1-minute load average @@ -82,6 +84,8 @@ def display_cpu_info(): def display_uptime(): + # clearing the display before accessing it + lcd.clear() """Display system uptime on the LCD.""" with open("/proc/uptime") as f: uptime_seconds = float(f.readline().split()[0]) @@ -91,6 +95,8 @@ def display_uptime(): def recognize_speech(): + # clearing the display before accessing it + lcd.clear() """Capture and transcribe speech input.""" try: with microphone as source: @@ -118,6 +124,8 @@ OPTIONS = { def main(): + # clearing the display before doing anything + lcd.clear() # Main program loop to accept user commands. print("WELCOME TO THE I2C COMMAND LINE CENTER") print("Options:", ", ".join(OPTIONS.keys())) -- cgit v1.2.3-70-g09d2 From 924cde73d031e0019ca364c97f1d3d291fc0b98e Mon Sep 17 00:00:00 2001 From: nasrlol Date: Wed, 13 Nov 2024 00:46:32 +0100 Subject: added a notes function, removed the accesive line statement in the lcd.display setting --- bin/kasper_source.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/bin/kasper_source.py b/bin/kasper_source.py index b7d8060..e1110a1 100644 --- a/bin/kasper_source.py +++ b/bin/kasper_source.py @@ -3,6 +3,7 @@ from gpiozero import CPUTemperature import speech_recognition as speech import os import time +from time import sleep # LCD Constants LCD_BACKLIGHT = 0x08 @@ -78,8 +79,8 @@ def display_cpu_info(): load = os.getloadavg()[0] # 1-minute load average temperature = cpu_temp.temperature lcd.clear() - lcd.display_text(f"CPU Load: {load:.2f}", line=1) - lcd.display_text(f"Temp: {temperature:.1f}C", line=2) + lcd.display_text(f"CPU Load: {load:.2f}", 1) + lcd.display_text(f"Temp: {temperature:.1f}C", 2) time.sleep(5) @@ -91,7 +92,7 @@ def display_uptime(): uptime_seconds = float(f.readline().split()[0]) uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) lcd.clear() - lcd.display_text(f"Uptime: {uptime_str}", line=1) + lcd.display_text(f"Uptime: {uptime_str}", 1) def recognize_speech(): @@ -105,21 +106,29 @@ def recognize_speech(): audio = recognizer.listen(source) text = recognizer.recognize_google(audio) lcd.clear() - lcd.display_text(text, line=1) + lcd.display_text(text, 1) print("Speech recognized:", text) except speech.UnknownValueError: - lcd.display_text(ERROR_BAD_REQUEST, line=1) + lcd.display_text(ERROR_BAD_REQUEST, 1) print(ERROR_BAD_REQUEST) except speech.RequestError: - lcd.display_text(ERROR_UNAUTHORIZED, line=1) + lcd.display_text(ERROR_UNAUTHORIZED, 1) print(ERROR_UNAUTHORIZED) +def notes(): + while True: + OUTPUT = input() + print(OUTPUT) + lcd.display_text() + sleep(2) + # Main Program Options OPTIONS = { "CPU_INFO": display_cpu_info, "UPTIME": display_uptime, "SPEECH_TRANSCRIBER": recognize_speech, + "NOTES": notes, } @@ -137,7 +146,7 @@ def main(): if action: action() else: - lcd.display_text(ERROR_NOT_FOUND, line=1) + lcd.display_text(ERROR_NOT_FOUND, 1) print(ERROR_NOT_FOUND) -- cgit v1.2.3-70-g09d2 From 1b9748da64281bc6fe5a808e744922ecc72fc6ba Mon Sep 17 00:00:00 2001 From: nasrlol Date: Wed, 13 Nov 2024 00:50:55 +0100 Subject: fixed the notes function output bug, only bug left fixeing the display doing funky --- bin/kasper_source.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/kasper_source.py b/bin/kasper_source.py index e1110a1..8067997 100644 --- a/bin/kasper_source.py +++ b/bin/kasper_source.py @@ -119,7 +119,7 @@ def notes(): while True: OUTPUT = input() print(OUTPUT) - lcd.display_text() + lcd.display_text(OUTPUT, 1) sleep(2) -- cgit v1.2.3-70-g09d2