summaryrefslogtreecommitdiff
path: root/source/hardware_driver.py
diff options
context:
space:
mode:
Diffstat (limited to 'source/hardware_driver.py')
-rw-r--r--source/hardware_driver.py141
1 files changed, 141 insertions, 0 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 @@
1from smbus import SMBus 1from smbus import SMBus
2from time import sleep 2from time import sleep
3import time
4import os
5import speech_recognition as sr
6from gpiozero import CPUTemperature
3 7
4ALIGN_FUNC = { 8ALIGN_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
80ERROR_BAD_REQUEST = "the request failed, bad request"
81ERROR_UNAUTHORIZED = "you do not have permission to make that request"
82ERROR_NOT_FOUND = "the request was not found, try again"
83SPEECH_NOT_RECOGNIZED = "we couldn't recognize what you said, try again or \n or check your internet connection"
84ERROR_TIMEOUT = "the request took too long check youre internet connection"
85
86# Initialize components and error handling for debugging
87try:
88 lcd_instance = lcd.LCD()
89except Exception as e:
90 print("Error intializing LCD")
91try:
92 cpu_temp = CPUTemperature()
93except Exception as e:
94 print("Error initializing CPU temperature sensor:", e)
95
96try:
97 recognizer = sr.Recognizer()
98except Exception as e:
99 print("Error initialzing voice recognition, its possible the speech recognition module isn't installed")
100
101try:
102 microphone = sr.Microphone()
103except 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
107def clear_terminal():
108 os.system("cls" if os.name == "nt" else "clear")
109
110# Features
111def 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
127def 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
150def 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
159def 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
170def 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
193def 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
206def 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)