diff options
| author | nasrlol <nsrddyn@gmail.com> | 2024-11-24 16:13:37 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2024-11-24 16:13:37 +0100 |
| commit | 16d54ef337f510e2dce2158d5cb95e26b29c9c57 (patch) | |
| tree | 497ad37fdcf3f5ee10ee2346f7a59605ebbb5910 | |
| parent | f32ea4fbb4467e9c7d14e785698224ec0f8d073f (diff) | |
fixed the bulk of the code so everyhting is running fine for now but i realised that the libraries i was using arent really all comatibnle with the rpi pico so features like speech transcription wont be working i think unless i can find a work around, because usb microphones arent an option i could maybe get some audio to get outputted using some DAC module or something still dont know, besied that there is still an issue with the pomodoro time display, still figuring out to setup a time count down that counts minutes and seconds
| -rwxr-xr-x | bin/Application | bin | 51400 -> 0 bytes | |||
| -rw-r--r-- | bin/source/hardware_driver.py | 25 | ||||
| -rw-r--r-- | bin/source/main.py | 248 |
3 files changed, 135 insertions, 138 deletions
diff --git a/bin/Application b/bin/Application deleted file mode 100755 index 26fad17..0000000 --- a/bin/Application +++ /dev/null | |||
| Binary files differ | |||
diff --git a/bin/source/hardware_driver.py b/bin/source/hardware_driver.py index 1afb219..7d48e69 100644 --- a/bin/source/hardware_driver.py +++ b/bin/source/hardware_driver.py | |||
| @@ -1,15 +1,21 @@ | |||
| 1 | from smbus2 import SMBus | 1 | from smbus import SMBus |
| 2 | from time import sleep | 2 | from time import sleep |
| 3 | 3 | ||
| 4 | ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} | 4 | ALIGN_FUNC = { |
| 5 | 'left': 'ljust', | ||
| 6 | 'right': 'rjust', | ||
| 7 | 'center': 'center'} | ||
| 5 | CLEAR_DISPLAY = 0x01 | 8 | CLEAR_DISPLAY = 0x01 |
| 6 | ENABLE_BIT = 0b00000100 | 9 | ENABLE_BIT = 0b00000100 |
| 7 | LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} | 10 | LINES = { |
| 11 | 1: 0x80, | ||
| 12 | 2: 0xC0, | ||
| 13 | 3: 0x94, | ||
| 14 | 4: 0xD4} | ||
| 8 | 15 | ||
| 9 | LCD_BACKLIGHT = 0x08 | 16 | LCD_BACKLIGHT = 0x08 |
| 10 | LCD_NOBACKLIGHT = 0x00 | 17 | LCD_NOBACKLIGHT = 0x00 |
| 11 | 18 | ||
| 12 | |||
| 13 | class LCD(object): | 19 | class LCD(object): |
| 14 | 20 | ||
| 15 | def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): | 21 | def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): |
| @@ -32,7 +38,7 @@ class LCD(object): | |||
| 32 | self.bus.write_byte(self.address, byte) | 38 | self.bus.write_byte(self.address, byte) |
| 33 | self.bus.write_byte(self.address, (byte | ENABLE_BIT)) | 39 | self.bus.write_byte(self.address, (byte | ENABLE_BIT)) |
| 34 | sleep(self.delay) | 40 | sleep(self.delay) |
| 35 | self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) | 41 | self.bus.write_byte(self.address,(byte & ~ENABLE_BIT)) |
| 36 | sleep(self.delay) | 42 | sleep(self.delay) |
| 37 | 43 | ||
| 38 | def write(self, byte, mode=0): | 44 | def write(self, byte, mode=0): |
| @@ -40,10 +46,10 @@ class LCD(object): | |||
| 40 | self._write_byte(mode | (byte & 0xF0) | backlight_mode) | 46 | self._write_byte(mode | (byte & 0xF0) | backlight_mode) |
| 41 | self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) | 47 | self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) |
| 42 | 48 | ||
| 43 | def text(self, text, line, align="left"): | 49 | def text(self, text, line, align='left'): |
| 44 | self.write(LINES.get(line, LINES[1])) | 50 | self.write(LINES.get(line, LINES[1])) |
| 45 | text, other_lines = self.get_text_line(text) | 51 | text, other_lines = self.get_text_line(text) |
| 46 | text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) | 52 | text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width) |
| 47 | for char in text: | 53 | for char in text: |
| 48 | self.write(ord(char), mode=1) | 54 | self.write(ord(char), mode=1) |
| 49 | if other_lines and line <= self.rows - 1: | 55 | if other_lines and line <= self.rows - 1: |
| @@ -56,13 +62,10 @@ class LCD(object): | |||
| 56 | def get_text_line(self, text): | 62 | def get_text_line(self, text): |
| 57 | line_break = self.width | 63 | line_break = self.width |
| 58 | if len(text) > self.width: | 64 | if len(text) > self.width: |
| 59 | line_break = text[: self.width + 1].rfind(" ") | 65 | line_break = text[:self.width + 1].rfind(' ') |
| 60 | if line_break < 0: | 66 | if line_break < 0: |
| 61 | line_break = self.width | 67 | line_break = self.width |
| 62 | return text[:line_break], text[line_break:].strip() | 68 | return text[:line_break], text[line_break:].strip() |
| 63 | 69 | ||
| 64 | def clear(self): | 70 | def clear(self): |
| 65 | self.write(CLEAR_DISPLAY) | 71 | self.write(CLEAR_DISPLAY) |
| 66 | |||
| 67 | def read(self): | ||
| 68 | self._read_i2c_block_data | ||
diff --git a/bin/source/main.py b/bin/source/main.py index bbd9dfa..929db57 100644 --- a/bin/source/main.py +++ b/bin/source/main.py | |||
| @@ -1,186 +1,180 @@ | |||
| 1 | import time | 1 | import time |
| 2 | import os | 2 | import os |
| 3 | import speech_recognition as speech | 3 | import speech_recognition as sr |
| 4 | import sounddevice | ||
| 5 | import hardware_driver as lcd | ||
| 6 | from gpiozero import CPUTemperature | 4 | from gpiozero import CPUTemperature |
| 5 | import hardware_driver as lcd | ||
| 7 | 6 | ||
| 7 | # Error messages | ||
| 8 | ERROR_BAD_REQUEST = "400 Bad Request" | 8 | ERROR_BAD_REQUEST = "400 Bad Request" |
| 9 | ERROR_UNAUTHORIZED = "401 Unauthorized" | 9 | ERROR_UNAUTHORIZED = "401 Unauthorized" |
| 10 | ERROR_NOT_FOUND = "404 Not Found" | 10 | ERROR_NOT_FOUND = "404 Not Found" |
| 11 | SPEECH_NOT_RECOGNIZED = "404-1 Speech is not recognized" | 11 | SPEECH_NOT_RECOGNIZED = "404-1 Speech not recognized" |
| 12 | ERROR_TIMEOUT = "408 Request Timeout" | 12 | ERROR_TIMEOUT = "408 Request Timeout" |
| 13 | 13 | ||
| 14 | lcd_instance = lcd.LCD() | 14 | # Initialize components |
| 15 | cpu_temp = CPUTemperature() | 15 | try: |
| 16 | recognizer = speech.Recognizer() | 16 | lcd_instance = lcd.LCD() |
| 17 | microphone = speech.Microphone() | 17 | except Exception as e: |
| 18 | print("Error intializing LCD") | ||
| 19 | try: | ||
| 20 | cpu_temp = CPUTemperature() | ||
| 21 | except Exception as e: | ||
| 22 | print("Error initializing CPU temperature sensor:", e) | ||
| 18 | 23 | ||
| 24 | try: | ||
| 25 | recognizer = sr.Recognizer() | ||
| 26 | except Exception as e: | ||
| 27 | print("Error initialzing voice recognition, its possible the speech recognition module isn't installed") | ||
| 19 | 28 | ||
| 20 | # greeting that starts upon the boot of the device: | 29 | try: |
| 21 | # shows a hello line; shorter than 16 chars | 30 | microphone = sr.Microphone() |
| 22 | # and some small information on the second line | 31 | except Exception as e: |
| 23 | def custom_greeting(): | 32 | print("Error initialzing the microphone \n check if the sound device package is installed") |
| 24 | with open("quotes.txt", "r") as file: | ||
| 25 | quotes = file.readlines() | ||
| 26 | 33 | ||
| 27 | # Strip newline characters and use the quotes | 34 | # clearing the terminal for a cleaner and program like interaction |
| 28 | quotes = [quote.strip() for quote in quotes] | 35 | def clear_terminal(): |
| 36 | os.system("cls" if os.name == "nt" else "clear") | ||
| 29 | 37 | ||
| 30 | # Print the quotes | 38 | # Features |
| 31 | for quote in quotes: | 39 | def custom_greeting(): |
| 32 | print(quote) | 40 | try: |
| 33 | first_line = "" | 41 | with open("quotes.txt", "r") as file: |
| 34 | second_line = "" | 42 | quotes = [quote.strip() for quote in file.readlines()] |
| 35 | count = 0 | 43 | except FileNotFoundError: |
| 36 | for i in quote: | 44 | lcd_instance.text("Quotes file missing", 1) |
| 37 | if count < 16: | 45 | return |
| 38 | first_line += i | ||
| 39 | count += 1 | ||
| 40 | else: | ||
| 41 | second_line += i | ||
| 42 | lcd.text(first_line, 1) | ||
| 43 | lcd.text(second_line, 2) | ||
| 44 | 46 | ||
| 47 | for quote in quotes: | ||
| 48 | first_line = quote[:16] | ||
| 49 | second_line = quote[16:32] | ||
| 50 | lcd_instance.text(first_line, 1) | ||
| 51 | lcd_instance.text(second_line, 2) | ||
| 52 | time.sleep(3) | ||
| 53 | lcd_instance.clear() | ||
| 45 | 54 | ||
| 46 | def pomodoro(): | 55 | def pomodoro(): |
| 47 | time = input("How long do you want to wait? : ") | 56 | try: |
| 48 | print("Okay \nStarting Now...") | 57 | duration_minutes = int(input("Enter duration in minutes: ")) |
| 49 | while time > 0: | 58 | duration_seconds = duration_minutes * 60 |
| 50 | time.sleep(1) | 59 | print("Pomodoro started for", duration_minutes, "minutes") |
| 51 | print(time + "Seconds") | 60 | lcd_instance.text("Pomodoro Running", 1) |
| 52 | lcd.text(time + " Seconds remaining...", 1) | 61 | start_count = 0 |
| 53 | time -= 1 | 62 | count = 0 |
| 54 | 63 | while duration_seconds > 0: | |
| 55 | 64 | lcd_instance.text(f"Time left: {duration_minutes}:{duration_seconds * 60}", 2) | |
| 56 | def weather(): | 65 | time.sleep(1) |
| 57 | pass | 66 | duration_seconds -= 1 |
| 67 | count += 1 | ||
| 68 | if count == start_count + 60: | ||
| 69 | start_count = start | ||
| 70 | duration_minutes -= 1 | ||
| 58 | 71 | ||
| 72 | lcd_instance.text("Time's Up!", 1) | ||
| 73 | time.sleep(3) | ||
| 74 | except ValueError: | ||
| 75 | lcd_instance.text("Invalid input", 1) | ||
| 76 | time.sleep(2) | ||
| 59 | 77 | ||
| 60 | # ram usage, internet speed, | ||
| 61 | def system_readings(): | 78 | def system_readings(): |
| 62 | lcd_instance.clear() | ||
| 63 | while True: | 79 | while True: |
| 64 | load = os.getloadavg()[0] | 80 | load = os.getloadavg()[0] |
| 65 | temperature = cpu_temp.temperature | 81 | temperature = cpu_temp.temperature if cpu_temp else "N/A" |
| 66 | lcd_instance.clear() | 82 | lcd_instance.clear() |
| 67 | lcd_instance.text(f"CPU Load: {load}", 1) | 83 | lcd_instance.text(f"CPU Load: {load:.2f}", 1) |
| 68 | lcd_instance.text(f"Temp: {temperature:.1f}C", 2) | 84 | lcd_instance.text(f"Temp: {temperature}C", 2) |
| 69 | time.sleep(5) | 85 | time.sleep(5) |
| 70 | 86 | ||
| 71 | |||
| 72 | def display_uptime(): | 87 | def display_uptime(): |
| 73 | lcd_instance.clear() | 88 | try: |
| 74 | with open("/proc/uptime") as f: | 89 | with open("/proc/uptime") as f: |
| 75 | uptime_seconds = float(f.readline().split()[0]) | 90 | uptime_seconds = float(f.readline().split()[0]) |
| 76 | uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) | 91 | uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) |
| 77 | lcd_instance.clear() | 92 | lcd_instance.text(f"Uptime: {uptime_str}", 1) |
| 78 | lcd_instance.text(f"Uptime: {uptime_str}", 1, "center") | 93 | time.sleep(3) |
| 79 | 94 | except Exception as e: | |
| 95 | lcd_instance.text("Error reading uptime", 1) | ||
| 96 | print("Error:", e) | ||
| 80 | 97 | ||
| 81 | def recognize_speech(): | 98 | def recognize_speech(): |
| 82 | 99 | lcd_instance.text("Listening...", 1) | |
| 83 | lcd_instance.clear() | ||
| 84 | try: | 100 | try: |
| 85 | with microphone as source: | 101 | with microphone as source: |
| 86 | recognizer.adjust_for_ambient_noise(source) | 102 | recognizer.adjust_for_ambient_noise(source) |
| 87 | print("Listening...") | ||
| 88 | audio = recognizer.listen(source) | 103 | audio = recognizer.listen(source) |
| 89 | text = recognizer.recognize_google(audio) | 104 | output = recognizer.recognize_google(audio) |
| 90 | lcd_instance.clear() | 105 | lcd_instance.text("Recognized:", 1) |
| 91 | lcd_instance.text(text, 1) | 106 | lcd_instance.text(output[:16], 2) |
| 92 | 107 | ||
| 93 | print("Speech recognized:", text) | 108 | print("Speech recognized:", output) |
| 94 | except speech.UnknownValueError: | 109 | return output |
| 95 | lcd_instance.text(ERROR_BAD_REQUEST, 1) | 110 | except sr.UnknownValueError: |
| 96 | print(ERROR_BAD_REQUEST) | 111 | lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1) |
| 97 | except speech.RequestError: | 112 | print(SPEECH_NOT_RECOGNIZED) |
| 113 | except sr.RequestError as e: | ||
| 98 | lcd_instance.text(ERROR_UNAUTHORIZED, 1) | 114 | lcd_instance.text(ERROR_UNAUTHORIZED, 1) |
| 99 | print(ERROR_UNAUTHORIZED) | 115 | print(ERROR_UNAUTHORIZED, e) |
| 100 | 116 | except Exception as e: | |
| 101 | return text | 117 | lcd_instance.text("Speech Error", 1) |
| 102 | 118 | print("Error:", e) | |
| 119 | return None | ||
| 103 | 120 | ||
| 104 | def save_notes(): | 121 | def save_notes(): |
| 105 | print("Type your notes (type 'stop' to exit):") | 122 | print("Type your notes (type 'stop' to exit):") |
| 106 | print("Type line=1 or line=2 to print something to a specific line") | ||
| 107 | while True: | 123 | while True: |
| 108 | line = 1 | 124 | note = input(": ") |
| 109 | output = input(":") | 125 | if note.lower() in ["stop", "exit", "quit"]: |
| 110 | output_length = len(output) | ||
| 111 | if output.lower() in ["stop", "break", "quit", "exit"]: | ||
| 112 | break | 126 | break |
| 113 | if output == "line=1": | 127 | first_line = note[:16] |
| 114 | line = 1 | 128 | second_line = note[16:32] |
| 115 | elif output == "line=2": | 129 | lcd_instance.text(first_line, 1) |
| 116 | line = 2 | 130 | lcd_instance.text(second_line, 2) |
| 117 | 131 | time.sleep(3) | |
| 118 | if output_length < 16: | ||
| 119 | lcd_instance.text(output, line) | ||
| 120 | time.sleep(2) | ||
| 121 | else: | ||
| 122 | output_list = output.split("") | ||
| 123 | first_line = "" | ||
| 124 | second_line = "" | ||
| 125 | for i in output_list: | ||
| 126 | count = 0 | ||
| 127 | if count > 16: | ||
| 128 | first_line += output_list[i] | ||
| 129 | count += 1 | ||
| 130 | else: | ||
| 131 | second_line += output_list[i] | ||
| 132 | lcd.text(first_line, 1) | ||
| 133 | lcd.text(secon_line, 2) | ||
| 134 | |||
| 135 | |||
| 136 | def command_center(commands): | ||
| 137 | # checking if we can reconize commands within the user speech | ||
| 138 | # requires -> | ||
| 139 | # converting the commands to human readable text | ||
| 140 | # no under scars | ||
| 141 | command = recognize_speech() | ||
| 142 | list = [] | ||
| 143 | try: | ||
| 144 | for i in commands: | ||
| 145 | if i == command: | ||
| 146 | print("I think i found what you ment...") | ||
| 147 | command() | ||
| 148 | except: | ||
| 149 | print("ERROR 404 - COMMAND NOT RECOGNIZED") | ||
| 150 | 132 | ||
| 133 | # Command center to execute features | ||
| 134 | def command_center(): | ||
| 135 | command = recognize_speech().upper() | ||
| 136 | if command: | ||
| 137 | command() | ||
| 138 | else: | ||
| 139 | lcd_instance.text(ERROR_NOT_FOUND, 1) | ||
| 140 | print(ERROR_NOT_FOUND) | ||
| 151 | 141 | ||
| 142 | # Features dictionary | ||
| 152 | FEATURES = { | 143 | FEATURES = { |
| 153 | "READINGS": system_readings, | 144 | "GREETING": custom_greeting, |
| 154 | "UPTIME": display_uptime, | 145 | "READINGS": system_readings, |
| 155 | "SPEECH_TRANSCRIBER": recognize_speech, | 146 | "UPTIME": display_uptime, |
| 156 | "NOTES": save_notes, | 147 | "SPEECH": recognize_speech, |
| 157 | "COMMAND CENTER": command_center, | 148 | "NOTE": save_notes, |
| 158 | } | 149 | "COMMAND": command_center, |
| 159 | 150 | "POMODORO": pomodoro, | |
| 151 | } | ||
| 160 | 152 | ||
| 153 | # Main Menu | ||
| 161 | def main(): | 154 | def main(): |
| 162 | lcd_instance.clear() | 155 | clear_terminal() |
| 163 | os.system("cls" if os.name == "nt" else "clear") | ||
| 164 | print("FEATURES:", ", ".join(FEATURES.keys())) | 156 | print("FEATURES:", ", ".join(FEATURES.keys())) |
| 165 | |||
| 166 | while True: | 157 | while True: |
| 167 | user_input = input("Enter command: ").upper() | 158 | user_input = input("Enter command (or 'EXIT' to quit): ").upper() |
| 159 | if user_input in ["QUIT", "EXIT"]: | ||
| 160 | destroy() | ||
| 161 | break | ||
| 168 | action = FEATURES.get(user_input) | 162 | action = FEATURES.get(user_input) |
| 169 | |||
| 170 | if action: | 163 | if action: |
| 171 | action() | 164 | action() |
| 172 | else: | 165 | else: |
| 173 | lcd_instance.text(ERROR_NOT_FOUND, 1) | ||
| 174 | print(ERROR_NOT_FOUND) | 166 | print(ERROR_NOT_FOUND) |
| 175 | 167 | ||
| 176 | 168 | # Clean up on exit | |
| 177 | def destroy(): | 169 | def destroy(): |
| 178 | lcd_instance.clear() | 170 | lcd_instance.clear() |
| 179 | os.system("cls" if os.name == "nt" else "clear") | 171 | clear_terminal() |
| 180 | 172 | print("Goodbye!") | |
| 181 | 173 | ||
| 174 | # Entry point | ||
| 182 | if __name__ == "__main__": | 175 | if __name__ == "__main__": |
| 183 | try: | 176 | try: |
| 184 | main() | 177 | main() |
| 185 | except KeyboardInterrupt: | 178 | except KeyboardInterrupt: |
| 186 | destroy() | 179 | destroy() |
| 180 | |||
