diff options
| author | nasrlol <nsrddyn@gmail.com> | 2024-11-27 03:29:03 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2024-11-27 03:29:03 +0100 |
| commit | 446f3d4742f88b09c85e83e96f6505cd3f6302c5 (patch) | |
| tree | a3e16464ee3cc1f158092fd43806b7686a88c0a3 /source | |
| parent | df6d2c51b15d55a30b8515de637aa7af490b2d95 (diff) | |
switched to using qt
Diffstat (limited to 'source')
| -rw-r--r-- | source/hardware_driver.py | 141 | ||||
| -rw-r--r-- | source/main.py | 180 |
2 files changed, 141 insertions, 180 deletions
diff --git a/source/hardware_driver.py b/source/hardware_driver.py index 7d48e69..671ff4e 100644 --- a/source/hardware_driver.py +++ b/source/hardware_driver.py | |||
| @@ -1,5 +1,9 @@ | |||
| 1 | from smbus import SMBus | 1 | from smbus import SMBus |
| 2 | from time import sleep | 2 | from time import sleep |
| 3 | import time | ||
| 4 | import os | ||
| 5 | import speech_recognition as sr | ||
| 6 | from gpiozero import CPUTemperature | ||
| 3 | 7 | ||
| 4 | ALIGN_FUNC = { | 8 | ALIGN_FUNC = { |
| 5 | 'left': 'ljust', | 9 | 'left': 'ljust', |
| @@ -69,3 +73,140 @@ class LCD(object): | |||
| 69 | 73 | ||
| 70 | def clear(self): | 74 | def clear(self): |
| 71 | self.write(CLEAR_DISPLAY) | 75 | self.write(CLEAR_DISPLAY) |
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | # Error Handling | ||
| 80 | ERROR_BAD_REQUEST = "the request failed, bad request" | ||
| 81 | ERROR_UNAUTHORIZED = "you do not have permission to make that request" | ||
| 82 | ERROR_NOT_FOUND = "the request was not found, try again" | ||
| 83 | SPEECH_NOT_RECOGNIZED = "we couldn't recognize what you said, try again or \n or check your internet connection" | ||
| 84 | ERROR_TIMEOUT = "the request took too long check youre internet connection" | ||
| 85 | |||
| 86 | # Initialize components and error handling for debugging | ||
| 87 | try: | ||
| 88 | lcd_instance = lcd.LCD() | ||
| 89 | except Exception as e: | ||
| 90 | print("Error intializing LCD") | ||
| 91 | try: | ||
| 92 | cpu_temp = CPUTemperature() | ||
| 93 | except Exception as e: | ||
| 94 | print("Error initializing CPU temperature sensor:", e) | ||
| 95 | |||
| 96 | try: | ||
| 97 | recognizer = sr.Recognizer() | ||
| 98 | except Exception as e: | ||
| 99 | print("Error initialzing voice recognition, its possible the speech recognition module isn't installed") | ||
| 100 | |||
| 101 | try: | ||
| 102 | microphone = sr.Microphone() | ||
| 103 | except Exception as e: | ||
| 104 | print("Error initialzing the microphone \n check if the sound device package is installed") | ||
| 105 | |||
| 106 | # clearing the terminal for a cleaner and program like interaction | ||
| 107 | def clear_terminal(): | ||
| 108 | os.system("cls" if os.name == "nt" else "clear") | ||
| 109 | |||
| 110 | # Features | ||
| 111 | def custom_greeting(): | ||
| 112 | try: | ||
| 113 | with open("quotes.txt", "r") as file: | ||
| 114 | quotes = [quote.strip() for quote in file.readlines()] | ||
| 115 | except FileNotFoundError: | ||
| 116 | lcd_instance.text("Quotes file missing", 1) | ||
| 117 | return | ||
| 118 | |||
| 119 | for quote in quotes: | ||
| 120 | first_line = quote[:16] | ||
| 121 | second_line = quote[16:32] | ||
| 122 | lcd_instance.text(first_line, 1) | ||
| 123 | lcd_instance.text(second_line, 2) | ||
| 124 | time.sleep(3) | ||
| 125 | lcd_instance.clear() | ||
| 126 | |||
| 127 | def pomodoro(): | ||
| 128 | try: | ||
| 129 | duration_minutes = int(input("Enter duration in minutes: ")) | ||
| 130 | duration_seconds = duration_minutes * 60 | ||
| 131 | print("Pomodoro started for", duration_minutes, "minutes") | ||
| 132 | lcd_instance.text("Pomodoro Running", 1) | ||
| 133 | start_count = 0 | ||
| 134 | count = 0 | ||
| 135 | while duration_seconds > 0: | ||
| 136 | lcd_instance.text(f"Time left: {duration_minutes}:{duration_seconds * 60}", 2) | ||
| 137 | time.sleep(1) | ||
| 138 | duration_seconds -= 1 | ||
| 139 | count += 1 | ||
| 140 | if count == start_count + 60: | ||
| 141 | start_count = start | ||
| 142 | duration_minutes -= 1 | ||
| 143 | |||
| 144 | lcd_instance.text("Time's Up!", 1) | ||
| 145 | time.sleep(3) | ||
| 146 | except ValueError: | ||
| 147 | lcd_instance.text("Invalid input", 1) | ||
| 148 | time.sleep(2) | ||
| 149 | |||
| 150 | def system_readings(): | ||
| 151 | while True: | ||
| 152 | load = os.getloadavg()[0] | ||
| 153 | temperature = cpu_temp.temperature if cpu_temp else "N/A" | ||
| 154 | lcd_instance.clear() | ||
| 155 | lcd_instance.text(f"CPU Load: {load:.2f}", 1) | ||
| 156 | lcd_instance.text(f"Temp: {temperature}C", 2) | ||
| 157 | time.sleep(5) | ||
| 158 | |||
| 159 | def display_uptime(): | ||
| 160 | try: | ||
| 161 | with open("/proc/uptime") as f: | ||
| 162 | uptime_seconds = float(f.readline().split()[0]) | ||
| 163 | uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) | ||
| 164 | lcd_instance.text(f"Uptime: {uptime_str}", 1) | ||
| 165 | time.sleep(3) | ||
| 166 | except Exception as e: | ||
| 167 | lcd_instance.text("Error reading uptime", 1) | ||
| 168 | print("Error:", e) | ||
| 169 | |||
| 170 | def recognize_speech(): | ||
| 171 | lcd_instance.text("Listening...", 1) | ||
| 172 | try: | ||
| 173 | with microphone as source: | ||
| 174 | recognizer.adjust_for_ambient_noise(source) | ||
| 175 | audio = recognizer.listen(source) | ||
| 176 | output = recognizer.recognize_google(audio) | ||
| 177 | lcd_instance.text("Recognized:", 1) | ||
| 178 | lcd_instance.text(output[:16], 2) | ||
| 179 | |||
| 180 | print("Speech recognized:", output) | ||
| 181 | return output | ||
| 182 | except sr.UnknownValueError: | ||
| 183 | lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1) | ||
| 184 | print(SPEECH_NOT_RECOGNIZED) | ||
| 185 | except sr.RequestError as e: | ||
| 186 | lcd_instance.text(ERROR_UNAUTHORIZED, 1) | ||
| 187 | print(ERROR_UNAUTHORIZED, e) | ||
| 188 | except Exception as e: | ||
| 189 | lcd_instance.text("Speech Error", 1) | ||
| 190 | print("Error:", e) | ||
| 191 | return None | ||
| 192 | |||
| 193 | def save_notes(): | ||
| 194 | print("Type your notes (type 'stop' to exit):") | ||
| 195 | while True: | ||
| 196 | note = input(": ") | ||
| 197 | if note.lower() in ["stop", "exit", "quit"]: | ||
| 198 | break | ||
| 199 | first_line = note[:16] | ||
| 200 | second_line = note[16:32] | ||
| 201 | lcd_instance.text(first_line, 1) | ||
| 202 | lcd_instance.text(second_line, 2) | ||
| 203 | time.sleep(3) | ||
| 204 | |||
| 205 | # Command center to execute features | ||
| 206 | def command_center(): | ||
| 207 | command = recognize_speech().upper() | ||
| 208 | if command: | ||
| 209 | command() | ||
| 210 | else: | ||
| 211 | lcd_instance.text(ERROR_NOT_FOUND, 1) | ||
| 212 | print(ERROR_NOT_FOUND) | ||
diff --git a/source/main.py b/source/main.py deleted file mode 100644 index 929db57..0000000 --- a/source/main.py +++ /dev/null | |||
| @@ -1,180 +0,0 @@ | |||
| 1 | import time | ||
| 2 | import os | ||
| 3 | import speech_recognition as sr | ||
| 4 | from gpiozero import CPUTemperature | ||
| 5 | import hardware_driver as lcd | ||
| 6 | |||
| 7 | # Error messages | ||
| 8 | ERROR_BAD_REQUEST = "400 Bad Request" | ||
| 9 | ERROR_UNAUTHORIZED = "401 Unauthorized" | ||
| 10 | ERROR_NOT_FOUND = "404 Not Found" | ||
| 11 | SPEECH_NOT_RECOGNIZED = "404-1 Speech not recognized" | ||
| 12 | ERROR_TIMEOUT = "408 Request Timeout" | ||
| 13 | |||
| 14 | # Initialize components | ||
| 15 | try: | ||
| 16 | lcd_instance = lcd.LCD() | ||
| 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) | ||
| 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") | ||
| 28 | |||
| 29 | try: | ||
| 30 | microphone = sr.Microphone() | ||
| 31 | except Exception as e: | ||
| 32 | print("Error initialzing the microphone \n check if the sound device package is installed") | ||
| 33 | |||
| 34 | # clearing the terminal for a cleaner and program like interaction | ||
| 35 | def clear_terminal(): | ||
| 36 | os.system("cls" if os.name == "nt" else "clear") | ||
| 37 | |||
| 38 | # Features | ||
| 39 | def custom_greeting(): | ||
| 40 | try: | ||
| 41 | with open("quotes.txt", "r") as file: | ||
| 42 | quotes = [quote.strip() for quote in file.readlines()] | ||
| 43 | except FileNotFoundError: | ||
| 44 | lcd_instance.text("Quotes file missing", 1) | ||
| 45 | return | ||
| 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() | ||
| 54 | |||
| 55 | def pomodoro(): | ||
| 56 | try: | ||
| 57 | duration_minutes = int(input("Enter duration in minutes: ")) | ||
| 58 | duration_seconds = duration_minutes * 60 | ||
| 59 | print("Pomodoro started for", duration_minutes, "minutes") | ||
| 60 | lcd_instance.text("Pomodoro Running", 1) | ||
| 61 | start_count = 0 | ||
| 62 | count = 0 | ||
| 63 | while duration_seconds > 0: | ||
| 64 | lcd_instance.text(f"Time left: {duration_minutes}:{duration_seconds * 60}", 2) | ||
| 65 | time.sleep(1) | ||
| 66 | duration_seconds -= 1 | ||
| 67 | count += 1 | ||
| 68 | if count == start_count + 60: | ||
| 69 | start_count = start | ||
| 70 | duration_minutes -= 1 | ||
| 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) | ||
| 77 | |||
| 78 | def system_readings(): | ||
| 79 | while True: | ||
| 80 | load = os.getloadavg()[0] | ||
| 81 | temperature = cpu_temp.temperature if cpu_temp else "N/A" | ||
| 82 | lcd_instance.clear() | ||
| 83 | lcd_instance.text(f"CPU Load: {load:.2f}", 1) | ||
| 84 | lcd_instance.text(f"Temp: {temperature}C", 2) | ||
| 85 | time.sleep(5) | ||
| 86 | |||
| 87 | def display_uptime(): | ||
| 88 | try: | ||
| 89 | with open("/proc/uptime") as f: | ||
| 90 | uptime_seconds = float(f.readline().split()[0]) | ||
| 91 | uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) | ||
| 92 | lcd_instance.text(f"Uptime: {uptime_str}", 1) | ||
| 93 | time.sleep(3) | ||
| 94 | except Exception as e: | ||
| 95 | lcd_instance.text("Error reading uptime", 1) | ||
| 96 | print("Error:", e) | ||
| 97 | |||
| 98 | def recognize_speech(): | ||
| 99 | lcd_instance.text("Listening...", 1) | ||
| 100 | try: | ||
| 101 | with microphone as source: | ||
| 102 | recognizer.adjust_for_ambient_noise(source) | ||
| 103 | audio = recognizer.listen(source) | ||
| 104 | output = recognizer.recognize_google(audio) | ||
| 105 | lcd_instance.text("Recognized:", 1) | ||
| 106 | lcd_instance.text(output[:16], 2) | ||
| 107 | |||
| 108 | print("Speech recognized:", output) | ||
| 109 | return output | ||
| 110 | except sr.UnknownValueError: | ||
| 111 | lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1) | ||
| 112 | print(SPEECH_NOT_RECOGNIZED) | ||
| 113 | except sr.RequestError as e: | ||
| 114 | lcd_instance.text(ERROR_UNAUTHORIZED, 1) | ||
| 115 | print(ERROR_UNAUTHORIZED, e) | ||
| 116 | except Exception as e: | ||
| 117 | lcd_instance.text("Speech Error", 1) | ||
| 118 | print("Error:", e) | ||
| 119 | return None | ||
| 120 | |||
| 121 | def save_notes(): | ||
| 122 | print("Type your notes (type 'stop' to exit):") | ||
| 123 | while True: | ||
| 124 | note = input(": ") | ||
| 125 | if note.lower() in ["stop", "exit", "quit"]: | ||
| 126 | break | ||
| 127 | first_line = note[:16] | ||
| 128 | second_line = note[16:32] | ||
| 129 | lcd_instance.text(first_line, 1) | ||
| 130 | lcd_instance.text(second_line, 2) | ||
| 131 | time.sleep(3) | ||
| 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) | ||
| 141 | |||
| 142 | # Features dictionary | ||
| 143 | FEATURES = { | ||
| 144 | "GREETING": custom_greeting, | ||
| 145 | "READINGS": system_readings, | ||
| 146 | "UPTIME": display_uptime, | ||
| 147 | "SPEECH": recognize_speech, | ||
| 148 | "NOTE": save_notes, | ||
| 149 | "COMMAND": command_center, | ||
| 150 | "POMODORO": pomodoro, | ||
| 151 | } | ||
| 152 | |||
| 153 | # Main Menu | ||
| 154 | def main(): | ||
| 155 | clear_terminal() | ||
| 156 | print("FEATURES:", ", ".join(FEATURES.keys())) | ||
| 157 | while True: | ||
| 158 | user_input = input("Enter command (or 'EXIT' to quit): ").upper() | ||
| 159 | if user_input in ["QUIT", "EXIT"]: | ||
| 160 | destroy() | ||
| 161 | break | ||
| 162 | action = FEATURES.get(user_input) | ||
| 163 | if action: | ||
| 164 | action() | ||
| 165 | else: | ||
| 166 | print(ERROR_NOT_FOUND) | ||
| 167 | |||
| 168 | # Clean up on exit | ||
| 169 | def destroy(): | ||
| 170 | lcd_instance.clear() | ||
| 171 | clear_terminal() | ||
| 172 | print("Goodbye!") | ||
| 173 | |||
| 174 | # Entry point | ||
| 175 | if __name__ == "__main__": | ||
| 176 | try: | ||
| 177 | main() | ||
| 178 | except KeyboardInterrupt: | ||
| 179 | destroy() | ||
| 180 | |||
