diff options
| author | nasrlol <nsrddyn@gmail.com> | 2024-11-13 00:15:05 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2024-11-13 00:15:05 +0100 |
| commit | 0a7689fbf896aa3629f9783c4316a4b453928b6f (patch) | |
| tree | 5001703353d1f7b0119aecc386585e8d50628ada | |
| parent | 749e88a3f57320dcbded824dd55c925c520d0158 (diff) | |
switching back to the previous library
| -rw-r--r-- | bin/kasper_source.py | 64 |
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 @@ | |||
| 1 | from smbus import SMBus | 1 | from smbus import SMBus |
| 2 | import gpiozero | ||
| 3 | from gpiozero import CPUTemperature | 2 | from gpiozero import CPUTemperature |
| 4 | import speech_recognition as sr | 3 | import speech_recognition as speech |
| 5 | import os | 4 | import os |
| 6 | import time | 5 | import time |
| 7 | 6 | ||
| 8 | Initialize components | 7 | # LCD Constants |
| 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 | # LCD Control Class | ||
| 21 | class 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 | ||
| 9 | lcd = LCD() | 59 | lcd = LCD() |
| 10 | cpu_temp = CPUTemperature() | 60 | cpu_temp = CPUTemperature() |
| 11 | recognizer = sr.Recognizer() | 61 | recognizer = speech.Recognizer() |
| 12 | microphone = sr.Microphone() | 62 | microphone = 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 | ||
