diff options
| author | nasrlol <nsrddyn@gmail.com> | 2024-12-02 22:39:04 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2024-12-02 22:39:04 +0100 |
| commit | 7877a18dd41e133c2aa3ad5dbea193e8aaa2175c (patch) | |
| tree | 021fec0cf0938e2de459da5d320614ae76f9102c /source | |
| parent | 2924fb2486c9c1a66d8b04bf4b5748db83293db3 (diff) | |
fixed issues 1 - 2 - 3 - 4 - 5 - 6 - 7, they still need to be tested
Diffstat (limited to 'source')
| -rw-r--r-- | source/features.py | 150 |
1 files changed, 95 insertions, 55 deletions
diff --git a/source/features.py b/source/features.py index c5a8811..a19ec7a 100644 --- a/source/features.py +++ b/source/features.py | |||
| @@ -3,6 +3,9 @@ import os | |||
| 3 | import speech_recognition as sr | 3 | import speech_recognition as sr |
| 4 | from gpiozero import CPUTemperature | 4 | from gpiozero import CPUTemperature |
| 5 | 5 | ||
| 6 | # some functions need to be put on a different thread but we are keeping that project for another time | ||
| 7 | # import threading | ||
| 8 | |||
| 6 | # Error Handling | 9 | # Error Handling |
| 7 | ERROR_BAD_REQUEST = "the request failed, bad request" | 10 | ERROR_BAD_REQUEST = "the request failed, bad request" |
| 8 | ERROR_UNAUTHORIZED = "you do not have permission to make that request" | 11 | ERROR_UNAUTHORIZED = "you do not have permission to make that request" |
| @@ -10,49 +13,58 @@ ERROR_NOT_FOUND = "the request was not found, try again" | |||
| 10 | SPEECH_NOT_RECOGNIZED = "we couldn't recognize what you said, try again or \n or check your internet connection" | 13 | SPEECH_NOT_RECOGNIZED = "we couldn't recognize what you said, try again or \n or check your internet connection" |
| 11 | ERROR_TIMEOUT = "the request took too long check youre internet connection" | 14 | ERROR_TIMEOUT = "the request took too long check youre internet connection" |
| 12 | 15 | ||
| 16 | |||
| 13 | # Initialize components and error handling for debugging | 17 | # Initialize components and error handling for debugging |
| 18 | # try initializingthe lcd | ||
| 19 | # if initializations fail they should disale theyre function | ||
| 14 | try: | 20 | try: |
| 15 | lcd_instance = lcd.LCD() | 21 | lcd_instance = lcd.LCD() |
| 16 | except Exception as e: | 22 | except Exception as e: |
| 17 | print("Error intializing LCD") | 23 | print("Error intializing LCD \n exiting the application...") |
| 24 | exit() | ||
| 25 | |||
| 26 | # try initializing the temperature readings | ||
| 18 | try: | 27 | try: |
| 19 | cpu_temp = CPUTemperature() | 28 | cpu_temp = CPUTemperature() |
| 29 | temperature_available = 1 | ||
| 20 | except Exception as e: | 30 | except Exception as e: |
| 21 | print("Error initializing CPU temperature sensor:", e) | 31 | print("Error initializing CPU temperature sensor:", e) |
| 32 | temperature_available = 0 | ||
| 22 | 33 | ||
| 34 | # try initializing the google speech recognizer | ||
| 23 | try: | 35 | try: |
| 24 | recognizer = sr.Recognizer() | 36 | recognizer = sr.Recognizer() |
| 37 | recognizer_available = 1 | ||
| 25 | except Exception as e: | 38 | except Exception as e: |
| 26 | print( | 39 | print( |
| 27 | "Error initialzing voice recognition, its possible the speech recognition module isn't installed" | 40 | "Error initialzing voice recognition, its possible the speech recognition module isn't installed" |
| 28 | ) | 41 | ) |
| 42 | recognizer_available = 0 | ||
| 29 | 43 | ||
| 44 | # try initializing the microphone | ||
| 30 | try: | 45 | try: |
| 31 | microphone = sr.Microphone() | 46 | microphone = sr.Microphone() |
| 47 | microphone_available = 1 | ||
| 32 | except Exception as e: | 48 | except Exception as e: |
| 33 | print( | 49 | print( |
| 34 | "Error initialzing the microphone \n check if the sound device package is installed" | 50 | "Error initialzing the microphone \n check if the sound device package is installed" |
| 35 | ) | 51 | ) |
| 52 | microphone_available = 0 | ||
| 36 | 53 | ||
| 37 | 54 | ||
| 38 | class fe: | 55 | class fe: |
| 39 | 56 | ||
| 40 | def __init__(self): | 57 | def __init__(self): |
| 41 | self.clear_terminal = clear_terminal() | 58 | pass |
| 42 | self.custom_greeting = custom_greeting() | ||
| 43 | self.pomodoro = pomodoro() | ||
| 44 | self.system_readings = system_readings() | ||
| 45 | self.display_uptime = display_uptime() | ||
| 46 | self.recognize_speech = recognize_speech() | ||
| 47 | self.save_notes = save_notes() | ||
| 48 | self.command_center = command_center() | ||
| 49 | 59 | ||
| 50 | # clearing the terminal for a cleaner and program like interaction | 60 | # clearing the terminal for a cleaner and program like interaction |
| 51 | def clear_terminal(self): | 61 | def clear_terminal_lcd(self): |
| 62 | lcd.instance.clear() | ||
| 52 | os.system("cls" if os.name == "nt" else "clear") | 63 | os.system("cls" if os.name == "nt" else "clear") |
| 53 | 64 | ||
| 54 | # Features | 65 | # Features |
| 55 | def custom_greeting(self): | 66 | def custom_greeting(self): |
| 67 | self.clear_terminal_lcd() | ||
| 56 | try: | 68 | try: |
| 57 | with open("quotes.txt", "r") as file: | 69 | with open("quotes.txt", "r") as file: |
| 58 | quotes = [quote.strip() for quote in file.readlines()] | 70 | quotes = [quote.strip() for quote in file.readlines()] |
| @@ -68,42 +80,53 @@ class fe: | |||
| 68 | time.sleep(3) | 80 | time.sleep(3) |
| 69 | lcd_instance.clear() | 81 | lcd_instance.clear() |
| 70 | 82 | ||
| 71 | |||
| 72 | def pomodoro(self): | 83 | def pomodoro(self): |
| 84 | |||
| 85 | self.clear_terminal_lcd() | ||
| 73 | try: | 86 | try: |
| 74 | duration_minutes = int(input("Enter duration in minutes: ")) | 87 | duration_minutes = int(input("Enter duration in minutes: ")) |
| 75 | duration_seconds = duration_minutes * 60 | 88 | duration_seconds = duration_minutes * 60 |
| 89 | |||
| 76 | print("Pomodoro started for", duration_minutes, "minutes") | 90 | print("Pomodoro started for", duration_minutes, "minutes") |
| 77 | lcd_instance.text("Pomodoro Running", 1) | 91 | |
| 78 | start_count = 0 | 92 | time_passed_seconds = 0 |
| 79 | count = 0 | 93 | time_passed_minutes = 0 |
| 94 | |||
| 80 | while duration_seconds > 0: | 95 | while duration_seconds > 0: |
| 81 | lcd_instance.text( | 96 | if duration_minutes != 0: |
| 82 | f"Time left: {duration_minutes}:{duration_seconds * 60}", 2 | 97 | duration_minutes -= 1 |
| 83 | ) | 98 | time_passed_seconds += 1 |
| 84 | time.sleep(1) | ||
| 85 | duration_seconds -= 1 | 99 | duration_seconds -= 1 |
| 86 | count += 1 | 100 | if time_passed_seconds == 60: |
| 87 | if count == start_count + 60: | ||
| 88 | start_count = start | ||
| 89 | duration_minutes -= 1 | 101 | duration_minutes -= 1 |
| 102 | time_passed_seconds == 0 | ||
| 103 | print(f"\ryou have {duration_minutes}:{duration_seconds} left", end="") | ||
| 104 | lcd_instance.text( | ||
| 105 | f"\ryou have {duration_minutes}:{duration_seconds} left", 1 | ||
| 106 | ) | ||
| 107 | sleep(1) | ||
| 90 | 108 | ||
| 91 | lcd_instance.text("Time's Up!", 1) | ||
| 92 | time.sleep(3) | ||
| 93 | except ValueError: | 109 | except ValueError: |
| 94 | lcd_instance.text("Invalid input", 1) | 110 | lcd_instance.text("Invalid input", 1) |
| 95 | time.sleep(2) | 111 | time.sleep(2) |
| 96 | 112 | ||
| 97 | def system_readings(self): | 113 | def temperature(self, temperature_available): |
| 98 | while True: | 114 | |
| 99 | load = os.getloadavg()[0] | 115 | self.clear_terminal_lcd() |
| 100 | temperature = cpu_temp.temperature if cpu_temp else "N/A" | 116 | |
| 101 | lcd_instance.clear() | 117 | if temperature_available == 0: |
| 102 | lcd_instance.text(f"CPU Load: {load:.2f}", 1) | 118 | print(ERROR_NOT_FOUND) |
| 103 | lcd_instance.text(f"Temp: {temperature}C", 2) | 119 | else: |
| 104 | time.sleep(5) | 120 | while True: |
| 121 | load = os.getloadavg()[0] | ||
| 122 | temperature = cpu_temp.temperature if cpu_temp else "N/A" | ||
| 123 | lcd_instance.clear() | ||
| 124 | lcd_instance.text(f"CPU Load: {load:.2f}", 1) | ||
| 125 | lcd_instance.text(f"Temp: {temperature}C", 2) | ||
| 126 | time.sleep(5) | ||
| 105 | 127 | ||
| 106 | def display_uptime(self): | 128 | def display_uptime(self): |
| 129 | self.clear_terminal_lcd() | ||
| 107 | try: | 130 | try: |
| 108 | with open("/proc/uptime") as f: | 131 | with open("/proc/uptime") as f: |
| 109 | uptime_seconds = float(f.readline().split()[0]) | 132 | uptime_seconds = float(f.readline().split()[0]) |
| @@ -114,30 +137,36 @@ class fe: | |||
| 114 | lcd_instance.text("Error reading uptime", 1) | 137 | lcd_instance.text("Error reading uptime", 1) |
| 115 | print("Error:", e) | 138 | print("Error:", e) |
| 116 | 139 | ||
| 117 | def recognize_speech(self): | 140 | def recognize_speech(self, recognizer_available): |
| 118 | lcd_instance.text("Listening...", 1) | 141 | self.clear_terminal_lcd() |
| 119 | try: | 142 | if recognizer_available == 0 or microphone_available == 0: |
| 120 | with microphone as source: | 143 | print(ERROR_NOT_FOUND) |
| 121 | recognizer.adjust_for_ambient_noise(source) | 144 | else: |
| 122 | audio = recognizer.listen(source) | ||
| 123 | output = recognizer.recognize_google(audio) | ||
| 124 | lcd_instance.text("Recognized:", 1) | ||
| 125 | lcd_instance.text(output[:16], 2) | ||
| 126 | 145 | ||
| 127 | print("Speech recognized:", output) | 146 | lcd_instance.text("Listening...", 1) |
| 128 | return output | 147 | try: |
| 129 | except sr.UnknownValueError: | 148 | with microphone as source: |
| 130 | lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1) | 149 | recognizer.adjust_for_ambient_noise(source) |
| 131 | print(SPEECH_NOT_RECOGNIZED) | 150 | audio = recognizer.listen(source) |
| 132 | except sr.RequestError as e: | 151 | output = recognizer.recognize_google(audio) |
| 133 | lcd_instance.text(ERROR_UNAUTHORIZED, 1) | 152 | lcd_instance.text("Recognized:", 1) |
| 134 | print(ERROR_UNAUTHORIZED, e) | 153 | lcd_instance.text(output[:16], 2) |
| 135 | except Exception as e: | 154 | |
| 136 | lcd_instance.text("Speech Error", 1) | 155 | print("Speech recognized:", output) |
| 137 | print("Error:", e) | 156 | return output |
| 138 | return None | 157 | except sr.UnknownValueError: |
| 158 | lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1) | ||
| 159 | print(SPEECH_NOT_RECOGNIZED) | ||
| 160 | except sr.RequestError as e: | ||
| 161 | lcd_instance.text(ERROR_UNAUTHORIZED, 1) | ||
| 162 | print(ERROR_UNAUTHORIZED, e) | ||
| 163 | except Exception as e: | ||
| 164 | lcd_instance.text("Speech Error", 1) | ||
| 165 | print("Error:", e) | ||
| 166 | return None | ||
| 139 | 167 | ||
| 140 | def save_notes(self): | 168 | def save_notes(self): |
| 169 | self.clear_terminal_lcd() | ||
| 141 | print("Type your notes (type 'stop' to exit):") | 170 | print("Type your notes (type 'stop' to exit):") |
| 142 | while True: | 171 | while True: |
| 143 | note = input(": ") | 172 | note = input(": ") |
| @@ -151,9 +180,20 @@ class fe: | |||
| 151 | 180 | ||
| 152 | # Command center to execute features | 181 | # Command center to execute features |
| 153 | def command_center(self): | 182 | def command_center(self): |
| 154 | command = recognize_speech().upper() | 183 | self.clear_terminal_lcd() |
| 155 | if command: | 184 | command = self.recognize_speech().lower() |
| 156 | self.command_center() | 185 | if command == "greeting" or command == "greetings": |
| 186 | self.custom_greeting() | ||
| 187 | elif command == "uptime": | ||
| 188 | self.display_uptime() | ||
| 189 | elif command == "pomodoro" or command == "pomodoro": | ||
| 190 | self.pomodoro() | ||
| 191 | elif command == "speech" or "transcribe": | ||
| 192 | self.recognize_speech() | ||
| 193 | elif command == "save notes" or command == notes: | ||
| 194 | self.save_notes() | ||
| 195 | elif command == "temperature": | ||
| 196 | self.temperature() | ||
| 157 | else: | 197 | else: |
| 158 | lcd_instance.text(ERROR_NOT_FOUND, 1) | 198 | lcd_instance.text(ERROR_NOT_FOUND, 1) |
| 159 | print(ERROR_NOT_FOUND) | 199 | print(ERROR_NOT_FOUND) |
