summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2024-12-02 15:06:02 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2024-12-02 15:06:02 +0100
commite7ddeccd096c54a31f1354ff9b6d8be9ebb4e6af (patch)
treec1d3e0a0e87c85cd5d0af071ef3b1667fd2849ae /source
parent9a2f1f4f07dc98f2f9298b49b20381b15deae715 (diff)
made a new features file
Diffstat (limited to 'source')
-rw-r--r--source/__pycache__/hardware_driver.cpython-313.pycbin12187 -> 0 bytes
-rw-r--r--source/features.py162
-rw-r--r--source/hardware_driver.py165
-rw-r--r--source/main.py8
4 files changed, 175 insertions, 160 deletions
diff --git a/source/__pycache__/hardware_driver.cpython-313.pyc b/source/__pycache__/hardware_driver.cpython-313.pyc
deleted file mode 100644
index d6c84ad..0000000
--- a/source/__pycache__/hardware_driver.cpython-313.pyc
+++ /dev/null
Binary files differ
diff --git a/source/features.py b/source/features.py
new file mode 100644
index 0000000..c5e532f
--- /dev/null
+++ b/source/features.py
@@ -0,0 +1,162 @@
1import time
2import os
3import speech_recognition as sr
4from gpiozero import CPUTemperature
5
6# Error Handling
7ERROR_BAD_REQUEST = "the request failed, bad request"
8ERROR_UNAUTHORIZED = "you do not have permission to make that request"
9ERROR_NOT_FOUND = "the request was not found, try again"
10SPEECH_NOT_RECOGNIZED = "we couldn't recognize what you said, try again or \n or check your internet connection"
11ERROR_TIMEOUT = "the request took too long check youre internet connection"
12
13# Initialize components and error handling for debugging
14try:
15 lcd_instance = lcd.LCD()
16except Exception as e:
17 print("Error intializing LCD")
18try:
19 cpu_temp = CPUTemperature()
20except Exception as e:
21 print("Error initializing CPU temperature sensor:", e)
22
23try:
24 recognizer = sr.Recognizer()
25except Exception as e:
26 print(
27 "Error initialzing voice recognition, its possible the speech recognition module isn't installed"
28 )
29
30try:
31 microphone = sr.Microphone()
32except Exception as e:
33 print(
34 "Error initialzing the microphone \n check if the sound device package is installed"
35 )
36
37
38class fe:
39
40 def __init__(self):
41 self.clear_terminal = clear_terminal()
42 self.custom_greeting = custom_greeting()
43 self.joke_of_the_day = joke_of_the_day()
44 self.pomodoro = pomodoro()
45 self.system_readings = system_readings()
46 self.display_uptime = display_uptime()
47 self.recognize_speech = recognize_speech()
48 self.save_notes = save_notes()
49 self.command_center = command_center()
50
51 # clearing the terminal for a cleaner and program like interaction
52 def clear_terminal():
53 os.system("cls" if os.name == "nt" else "clear")
54
55 # Features
56 def custom_greeting():
57 try:
58 with open("quotes.txt", "r") as file:
59 quotes = [quote.strip() for quote in file.readlines()]
60 except FileNotFoundError:
61 lcd_instance.text("Quotes file missing", 1)
62 return
63
64 for quote in quotes:
65 first_line = quote[:16]
66 second_line = quote[16:32]
67 lcd_instance.text(first_line, 1)
68 lcd_instance.text(second_line, 2)
69 time.sleep(3)
70 lcd_instance.clear()
71
72 def joke_of_the_day():
73 pass
74
75 def pomodoro():
76 try:
77 duration_minutes = int(input("Enter duration in minutes: "))
78 duration_seconds = duration_minutes * 60
79 print("Pomodoro started for", duration_minutes, "minutes")
80 lcd_instance.text("Pomodoro Running", 1)
81 start_count = 0
82 count = 0
83 while duration_seconds > 0:
84 lcd_instance.text(
85 f"Time left: {duration_minutes}:{duration_seconds * 60}", 2
86 )
87 time.sleep(1)
88 duration_seconds -= 1
89 count += 1
90 if count == start_count + 60:
91 start_count = start
92 duration_minutes -= 1
93
94 lcd_instance.text("Time's Up!", 1)
95 time.sleep(3)
96 except ValueError:
97 lcd_instance.text("Invalid input", 1)
98 time.sleep(2)
99
100 def system_readings():
101 while True:
102 load = os.getloadavg()[0]
103 temperature = cpu_temp.temperature if cpu_temp else "N/A"
104 lcd_instance.clear()
105 lcd_instance.text(f"CPU Load: {load:.2f}", 1)
106 lcd_instance.text(f"Temp: {temperature}C", 2)
107 time.sleep(5)
108
109 def display_uptime():
110 try:
111 with open("/proc/uptime") as f:
112 uptime_seconds = float(f.readline().split()[0])
113 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
114 lcd_instance.text(f"Uptime: {uptime_str}", 1)
115 time.sleep(3)
116 except Exception as e:
117 lcd_instance.text("Error reading uptime", 1)
118 print("Error:", e)
119
120 def recognize_speech():
121 lcd_instance.text("Listening...", 1)
122 try:
123 with microphone as source:
124 recognizer.adjust_for_ambient_noise(source)
125 audio = recognizer.listen(source)
126 output = recognizer.recognize_google(audio)
127 lcd_instance.text("Recognized:", 1)
128 lcd_instance.text(output[:16], 2)
129
130 print("Speech recognized:", output)
131 return output
132 except sr.UnknownValueError:
133 lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1)
134 print(SPEECH_NOT_RECOGNIZED)
135 except sr.RequestError as e:
136 lcd_instance.text(ERROR_UNAUTHORIZED, 1)
137 print(ERROR_UNAUTHORIZED, e)
138 except Exception as e:
139 lcd_instance.text("Speech Error", 1)
140 print("Error:", e)
141 return None
142
143 def save_notes():
144 print("Type your notes (type 'stop' to exit):")
145 while True:
146 note = input(": ")
147 if note.lower() in ["stop", "exit", "quit"]:
148 break
149 first_line = note[:16]
150 second_line = note[16:32]
151 lcd_instance.text(first_line, 1)
152 lcd_instance.text(second_line, 2)
153 time.sleep(3)
154
155 # Command center to execute features
156 def command_center():
157 command = recognize_speech().upper()
158 if command:
159 command()
160 else:
161 lcd_instance.text(ERROR_NOT_FOUND, 1)
162 print(ERROR_NOT_FOUND)
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)
diff --git a/source/main.py b/source/main.py
index 5bad9d9..81bd07a 100644
--- a/source/main.py
+++ b/source/main.py
@@ -8,11 +8,13 @@ from PySide6.QtWidgets import (
8 QWidget, 8 QWidget,
9) 9)
10from PySide6.QtCore import Qt 10from PySide6.QtCore import Qt
11import hardware_driver as lcd 11import hardware_driver as LCD
12import features as fe
12import sys 13import sys
13 14
15F = fe()
16L = LCD()
14 17
15L = lcd()
16 18
17class mainWindow(QMainWindow): 19class mainWindow(QMainWindow):
18 def __init__(self): 20 def __init__(self):
@@ -38,7 +40,7 @@ class mainWindow(QMainWindow):
38 central_widget.setLayout(layout) 40 central_widget.setLayout(layout)
39 41
40 def clear_screen(): 42 def clear_screen():
41 L.clear() 43 fe.clear()
42 self.label.setText("Cleared the LCD screen", self) 44 self.label.setText("Cleared the LCD screen", self)
43 45
44 46