summaryrefslogtreecommitdiff
path: root/bin/source/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/source/main.py')
-rw-r--r--bin/source/main.py183
1 files changed, 183 insertions, 0 deletions
diff --git a/bin/source/main.py b/bin/source/main.py
new file mode 100644
index 0000000..bf556ca
--- /dev/null
+++ b/bin/source/main.py
@@ -0,0 +1,183 @@
1import time
2import os
3import speech_recognition as speech
4import sounddevice
5import bin.hardware_driver as lcd
6from gpiozero import CPUTemperature
7
8ERROR_BAD_REQUEST = "400 Bad Request"
9ERROR_UNAUTHORIZED = "401 Unauthorized"
10ERROR_NOT_FOUND = "404 Not Found"
11SPEECH_NOT_RECOGNIZED = "404-1 Speech is not recognized"
12ERROR_TIMEOUT = "408 Request Timeout"
13
14lcd_instance = lcd.LCD()
15cpu_temp = CPUTemperature()
16recognizer = speech.Recognizer()
17microphone = speech.Microphone()
18
19
20# greeting that starts upon the boot of the device:
21# shows a hello line; shorter than 16 chars
22# and some small information on the second line
23def custom_greeting():
24 with open('quotes.txt', 'r') as file:
25 quotes = file.readlines()
26
27 # Strip newline characters and use the quotes
28 quotes = [quote.strip() for quote in quotes]
29
30 # Print the quotes
31 for quote in quotes:
32 print(quote)
33 first_line = ""
34 second_line = ""
35 count = 0
36 for i in quote:
37 if count < 16:
38 first_line += i
39 count += 1
40 else:
41 second_line += i
42 lcd.text(first_line,1)
43 lcd.text(secon_line,2)
44def pomodoro():
45 time = input("How long do you want to wait? : ")
46 print("Okay \nStarting Now...")
47 while time > 0:
48 time.sleep(1)
49 print(time + "Seconds")
50 lcd.text(time + " Seconds remaining...", 1)
51 time -= 1
52
53
54def weather():
55 pass
56
57
58# ram usage, internet speed,
59def system_readings():
60 lcd_instance.clear()
61 while True:
62 load = os.getloadavg()[0]
63 temperature = cpu_temp.temperature
64 lcd_instance.clear()
65 lcd_instance.text(f"CPU Load: {load}", 1)
66 lcd_instance.text(f"Temp: {temperature:.1f}C", 2)
67 time.sleep(5)
68
69
70def display_uptime():
71 lcd_instance.clear()
72 with open("/proc/uptime") as f:
73 uptime_seconds = float(f.readline().split()[0])
74 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
75 lcd_instance.clear()
76 lcd_instance.text(f"Uptime: {uptime_str}", 1, "center")
77
78
79def recognize_speech():
80
81 lcd_instance.clear()
82 try:
83 with microphone as source:
84 recognizer.adjust_for_ambient_noise(source)
85 print("Listening...")
86 audio = recognizer.listen(source)
87 text = recognizer.recognize_google(audio)
88 lcd_instance.clear()
89 lcd_instance.text(text, 1)
90
91 print("Speech recognized:", text)
92 except speech.UnknownValueError:
93 lcd_instance.text(ERROR_BAD_REQUEST, 1)
94 print(ERROR_BAD_REQUEST)
95 except speech.RequestError:
96 lcd_instance.text(ERROR_UNAUTHORIZED, 1)
97 print(ERROR_UNAUTHORIZED)
98
99 return text
100
101
102def save_notes():
103 print("Type your notes (type 'stop' to exit):")
104 print("Type line=1 or line=2 to print something to a specific line")
105 while True:
106 line = 1
107 output = input(":")
108 output_length = len(output)
109 if output.lower() in ["stop", "break", "quit", "exit"]:
110 break
111 if output == "line=1":
112 line = 1
113 elif output == "line=2":
114 line = 2
115
116 if output_length < 16:
117 lcd_instance.text(output, line)
118 time.sleep(2)
119 else:
120 output_list = output.split("")
121 first_line = ""
122 second_line = ""
123 for i in output_list:
124 count = 0
125 if count > 16:
126 first_line += output_list[i]
127 count += 1
128 else:
129 second_line += output_list[i]
130 lcd.text(first_line,1)
131 lcd.text(secon_line,2)
132
133def command_center(commands):
134 # checking if we can reconize commands within the user speech
135 # requires ->
136 # converting the commands to human readable text
137 # no under scars
138 command = recognize_speech()
139 list = []
140 try:
141 for i in commands:
142 if i == command:
143 print("I think i found what you ment...")
144 command()
145 except:
146 print("ERROR 404 - COMMAND NOT RECOGNIZED")
147
148
149FEATURES = {
150 "READINGS": system_readings,
151 "UPTIME": display_uptime,
152 "SPEECH_TRANSCRIBER": recognize_speech,
153 "NOTES": save_notes,
154 "COMMAND CENTER": command_center,
155 }
156
157
158def main():
159 lcd_instance.clear()
160 os.system("cls" if os.name == "nt" else "clear")
161 print("FEATURES:", ", ".join(FEATURES.keys()))
162
163 while True:
164 user_input = input("Enter command: ").upper()
165 action = FEATURES.get(user_input)
166
167 if action:
168 action()
169 else:
170 lcd_instance.text(ERROR_NOT_FOUND, 1)
171 print(ERROR_NOT_FOUND)
172
173
174def destroy():
175 lcd_instance.clear()
176 os.system("cls" if os.name == "nt" else "clear")
177
178
179if __name__ == "__main__":
180 try:
181 main()
182 except KeyboardInterrupt:
183 destroy()