summaryrefslogtreecommitdiff
path: root/source/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'source/main.py')
-rw-r--r--source/main.py180
1 files changed, 180 insertions, 0 deletions
diff --git a/source/main.py b/source/main.py
new file mode 100644
index 0000000..929db57
--- /dev/null
+++ b/source/main.py
@@ -0,0 +1,180 @@
1import time
2import os
3import speech_recognition as sr
4from gpiozero import CPUTemperature
5import hardware_driver as lcd
6
7# Error messages
8ERROR_BAD_REQUEST = "400 Bad Request"
9ERROR_UNAUTHORIZED = "401 Unauthorized"
10ERROR_NOT_FOUND = "404 Not Found"
11SPEECH_NOT_RECOGNIZED = "404-1 Speech not recognized"
12ERROR_TIMEOUT = "408 Request Timeout"
13
14# Initialize components
15try:
16 lcd_instance = lcd.LCD()
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)
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")
28
29try:
30 microphone = sr.Microphone()
31except 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
35def clear_terminal():
36 os.system("cls" if os.name == "nt" else "clear")
37
38# Features
39def 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
55def 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
78def 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
87def 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
98def 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
121def 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
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)
141
142# Features dictionary
143FEATURES = {
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
154def 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
169def destroy():
170 lcd_instance.clear()
171 clear_terminal()
172 print("Goodbye!")
173
174# Entry point
175if __name__ == "__main__":
176 try:
177 main()
178 except KeyboardInterrupt:
179 destroy()
180