summaryrefslogtreecommitdiff
path: root/bin/source/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/source/main.py')
-rw-r--r--bin/source/main.py248
1 files changed, 121 insertions, 127 deletions
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 @@
1import time 1import time
2import os 2import os
3import speech_recognition as speech 3import speech_recognition as sr
4import sounddevice
5import hardware_driver as lcd
6from gpiozero import CPUTemperature 4from gpiozero import CPUTemperature
5import hardware_driver as lcd
7 6
7# Error messages
8ERROR_BAD_REQUEST = "400 Bad Request" 8ERROR_BAD_REQUEST = "400 Bad Request"
9ERROR_UNAUTHORIZED = "401 Unauthorized" 9ERROR_UNAUTHORIZED = "401 Unauthorized"
10ERROR_NOT_FOUND = "404 Not Found" 10ERROR_NOT_FOUND = "404 Not Found"
11SPEECH_NOT_RECOGNIZED = "404-1 Speech is not recognized" 11SPEECH_NOT_RECOGNIZED = "404-1 Speech not recognized"
12ERROR_TIMEOUT = "408 Request Timeout" 12ERROR_TIMEOUT = "408 Request Timeout"
13 13
14lcd_instance = lcd.LCD() 14# Initialize components
15cpu_temp = CPUTemperature() 15try:
16recognizer = speech.Recognizer() 16 lcd_instance = lcd.LCD()
17microphone = speech.Microphone() 17except Exception as e:
18 print("Error intializing LCD")
19try:
20 cpu_temp = CPUTemperature()
21except Exception as e:
22 print("Error initializing CPU temperature sensor:", e)
18 23
24try:
25 recognizer = sr.Recognizer()
26except 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: 29try:
21# shows a hello line; shorter than 16 chars 30 microphone = sr.Microphone()
22# and some small information on the second line 31except Exception as e:
23def 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] 35def 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: 39def 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
46def pomodoro(): 55def 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)
56def 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,
61def system_readings(): 78def 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
72def display_uptime(): 87def 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
81def recognize_speech(): 98def 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
104def save_notes(): 121def 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
136def 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
134def 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
152FEATURES = { 143FEATURES = {
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
161def main(): 154def 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
177def destroy(): 169def 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
182if __name__ == "__main__": 175if __name__ == "__main__":
183 try: 176 try:
184 main() 177 main()
185 except KeyboardInterrupt: 178 except KeyboardInterrupt:
186 destroy() 179 destroy()
180