summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornasrlol <nsrddyn@gmail.com>2024-11-13 00:15:05 +0100
committernasrlol <nsrddyn@gmail.com>2024-11-13 00:15:05 +0100
commit0a7689fbf896aa3629f9783c4316a4b453928b6f (patch)
tree5001703353d1f7b0119aecc386585e8d50628ada
parent749e88a3f57320dcbded824dd55c925c520d0158 (diff)
switching back to the previous library
-rw-r--r--bin/kasper_source.py64
1 files 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 @@
1from smbus import SMBus 1from smbus import SMBus
2import gpiozero
3from gpiozero import CPUTemperature 2from gpiozero import CPUTemperature
4import speech_recognition as sr 3import speech_recognition as speech
5import os 4import os
6import time 5import time
7 6
8Initialize components 7# LCD Constants
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# LCD Control Class
21class LCD:
22 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
23 self.address = address
24 self.bus = SMBus(bus)
25 self.width = width
26 self.rows = rows
27 self.backlight_status = backlight
28 self.delay = 0.0005
29 # LCD Initialization
30 for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01):
31 self.write(cmd)
32 time.sleep(self.delay)
33 def write(self, byte, mode=0):
34 """Send a command or character to the LCD."""
35 backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
36 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight)
37 def _write_byte(self, byte):
38 """Write a byte to the I2C bus."""
39 self.bus.write_byte(self.address, byte)
40 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
41 time.sleep(self.delay)
42 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
43 time.sleep(self.delay)
44 def display_text(self, text, line=1, align="left"):
45 """Display text on a specified line with alignment."""
46 self.write(LINES.get(line, LINES[1]))
47 aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
48 for char in aligned_text:
49 self.write(ord(char), mode=1)
50 def clear(self):
51 """Clear the display."""
52 self.write(0x01)
53 def set_backlight(self, turn_on=True):
54 """Toggle backlight on or off."""
55 self.backlight_status = turn_on
56 self.write(0)
57
58# Initialize components
9lcd = LCD() 59lcd = LCD()
10cpu_temp = CPUTemperature() 60cpu_temp = CPUTemperature()
11recognizer = sr.Recognizer() 61recognizer = speech.Recognizer()
12microphone = sr.Microphone() 62microphone = speech.Microphone()
13 63
14 64
15# Display Functions 65# Display Functions
@@ -44,10 +94,10 @@ def recognize_speech():
44 lcd.clear() 94 lcd.clear()
45 lcd.display_text(text, line=1) 95 lcd.display_text(text, line=1)
46 print("Speech recognized:", text) 96 print("Speech recognized:", text)
47 except sr.UnknownValueError: 97 except speech.UnknownValueError:
48 lcd.display_text(ERROR_BAD_REQUEST, line=1) 98 lcd.display_text(ERROR_BAD_REQUEST, line=1)
49 print(ERROR_BAD_REQUEST) 99 print(ERROR_BAD_REQUEST)
50 except sr.RequestError: 100 except s.RequestError:
51 lcd.display_text(ERROR_UNAUTHORIZED, line=1) 101 lcd.display_text(ERROR_UNAUTHORIZED, line=1)
52 print(ERROR_UNAUTHORIZED) 102 print(ERROR_UNAUTHORIZED)
53 103