summaryrefslogtreecommitdiff
path: root/source/features.py
diff options
context:
space:
mode:
Diffstat (limited to 'source/features.py')
-rw-r--r--source/features.py162
1 files changed, 162 insertions, 0 deletions
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)