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.py165
1 files changed, 8 insertions, 157 deletions
diff --git a/source/hardware_driver.py b/source/hardware_driver.py
index 5879462..85f0adc 100644
--- a/source/hardware_driver.py
+++ b/source/hardware_driver.py
@@ -1,25 +1,16 @@
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
7 3
8ALIGN_FUNC = { 4
9 'left': 'ljust', 5ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
10 'right': 'rjust',
11 'center': 'center'}
12CLEAR_DISPLAY = 0x01 6CLEAR_DISPLAY = 0x01
13ENABLE_BIT = 0b00000100 7ENABLE_BIT = 0b00000100
14LINES = { 8LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4}
15 1: 0x80,
16 2: 0xC0,
17 3: 0x94,
18 4: 0xD4}
19 9
20LCD_BACKLIGHT = 0x08 10LCD_BACKLIGHT = 0x08
21LCD_NOBACKLIGHT = 0x00 11LCD_NOBACKLIGHT = 0x00
22 12
13
23class LCD(object): 14class LCD(object):
24 15
25 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): 16 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
@@ -42,7 +33,7 @@ class LCD(object):
42 self.bus.write_byte(self.address, byte) 33 self.bus.write_byte(self.address, byte)
43 self.bus.write_byte(self.address, (byte | ENABLE_BIT)) 34 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
44 sleep(self.delay) 35 sleep(self.delay)
45 self.bus.write_byte(self.address,(byte & ~ENABLE_BIT)) 36 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
46 sleep(self.delay) 37 sleep(self.delay)
47 38
48 def write(self, byte, mode=0): 39 def write(self, byte, mode=0):
@@ -50,10 +41,10 @@ class LCD(object):
50 self._write_byte(mode | (byte & 0xF0) | backlight_mode) 41 self._write_byte(mode | (byte & 0xF0) | backlight_mode)
51 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) 42 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode)
52 43
53 def text(self, text, line, align='left'): 44 def text(self, text, line, align="left"):
54 self.write(LINES.get(line, LINES[1])) 45 self.write(LINES.get(line, LINES[1]))
55 text, other_lines = self.get_text_line(text) 46 text, other_lines = self.get_text_line(text)
56 text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width) 47 text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
57 for char in text: 48 for char in text:
58 self.write(ord(char), mode=1) 49 self.write(ord(char), mode=1)
59 if other_lines and line <= self.rows - 1: 50 if other_lines and line <= self.rows - 1:
@@ -66,150 +57,10 @@ class LCD(object):
66 def get_text_line(self, text): 57 def get_text_line(self, text):
67 line_break = self.width 58 line_break = self.width
68 if len(text) > self.width: 59 if len(text) > self.width:
69 line_break = text[:self.width + 1].rfind(' ') 60 line_break = text[: self.width + 1].rfind(" ")
70 if line_break < 0: 61 if line_break < 0:
71 line_break = self.width 62 line_break = self.width
72 return text[:line_break], text[line_break:].strip() 63 return text[:line_break], text[line_break:].strip()
73 64
74 def clear(self): 65 def clear(self):
75 self.write(CLEAR_DISPLAY) 66 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 joke_of_the_day():
128 pass
129
130def pomodoro():
131 try:
132 duration_minutes = int(input("Enter duration in minutes: "))
133 duration_seconds = duration_minutes * 60
134 print("Pomodoro started for", duration_minutes, "minutes")
135 lcd_instance.text("Pomodoro Running", 1)
136 start_count = 0
137 count = 0
138 while duration_seconds > 0:
139 lcd_instance.text(f"Time left: {duration_minutes}:{duration_seconds * 60}", 2)
140 time.sleep(1)
141 duration_seconds -= 1
142 count += 1
143 if count == start_count + 60:
144 start_count = start
145 duration_minutes -= 1
146
147 lcd_instance.text("Time's Up!", 1)
148 time.sleep(3)
149 except ValueError:
150 lcd_instance.text("Invalid input", 1)
151 time.sleep(2)
152
153def system_readings():
154 while True:
155 load = os.getloadavg()[0]
156 temperature = cpu_temp.temperature if cpu_temp else "N/A"
157 lcd_instance.clear()
158 lcd_instance.text(f"CPU Load: {load:.2f}", 1)
159 lcd_instance.text(f"Temp: {temperature}C", 2)
160 time.sleep(5)
161
162def display_uptime():
163 try:
164 with open("/proc/uptime") as f:
165 uptime_seconds = float(f.readline().split()[0])
166 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
167 lcd_instance.text(f"Uptime: {uptime_str}", 1)
168 time.sleep(3)
169 except Exception as e:
170 lcd_instance.text("Error reading uptime", 1)
171 print("Error:", e)
172
173def recognize_speech():
174 lcd_instance.text("Listening...", 1)
175 try:
176 with microphone as source:
177 recognizer.adjust_for_ambient_noise(source)
178 audio = recognizer.listen(source)
179 output = recognizer.recognize_google(audio)
180 lcd_instance.text("Recognized:", 1)
181 lcd_instance.text(output[:16], 2)
182
183 print("Speech recognized:", output)
184 return output
185 except sr.UnknownValueError:
186 lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1)
187 print(SPEECH_NOT_RECOGNIZED)
188 except sr.RequestError as e:
189 lcd_instance.text(ERROR_UNAUTHORIZED, 1)
190 print(ERROR_UNAUTHORIZED, e)
191 except Exception as e:
192 lcd_instance.text("Speech Error", 1)
193 print("Error:", e)
194 return None
195
196def save_notes():
197 print("Type your notes (type 'stop' to exit):")
198 while True:
199 note = input(": ")
200 if note.lower() in ["stop", "exit", "quit"]:
201 break
202 first_line = note[:16]
203 second_line = note[16:32]
204 lcd_instance.text(first_line, 1)
205 lcd_instance.text(second_line, 2)
206 time.sleep(3)
207
208# Command center to execute features
209def command_center():
210 command = recognize_speech().upper()
211 if command:
212 command()
213 else:
214 lcd_instance.text(ERROR_NOT_FOUND, 1)
215 print(ERROR_NOT_FOUND)