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.py186
1 files changed, 186 insertions, 0 deletions
diff --git a/bin/source/main.py b/bin/source/main.py
new file mode 100644
index 0000000..c29b19f
--- /dev/null
+++ b/bin/source/main.py
@@ -0,0 +1,186 @@
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(second_line, 2)
44
45
46def pomodoro():
47 time = input("How long do you want to wait? : ")
48 print("Okay \nStarting Now...")
49 while time > 0:
50 time.sleep(1)
51 print(time + "Seconds")
52 lcd.text(time + " Seconds remaining...", 1)
53 time -= 1
54
55
56def weather():
57 pass
58
59
60# ram usage, internet speed,
61def system_readings():
62 lcd_instance.clear()
63 while True:
64 load = os.getloadavg()[0]
65 temperature = cpu_temp.temperature
66 lcd_instance.clear()
67 lcd_instance.text(f"CPU Load: {load}", 1)
68 lcd_instance.text(f"Temp: {temperature:.1f}C", 2)
69 time.sleep(5)
70
71
72def display_uptime():
73 lcd_instance.clear()
74 with open("/proc/uptime") as f:
75 uptime_seconds = float(f.readline().split()[0])
76 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
77 lcd_instance.clear()
78 lcd_instance.text(f"Uptime: {uptime_str}", 1, "center")
79
80
81def recognize_speech():
82
83 lcd_instance.clear()
84 try:
85 with microphone as source:
86 recognizer.adjust_for_ambient_noise(source)
87 print("Listening...")
88 audio = recognizer.listen(source)
89 text = recognizer.recognize_google(audio)
90 lcd_instance.clear()
91 lcd_instance.text(text, 1)
92
93 print("Speech recognized:", text)
94 except speech.UnknownValueError:
95 lcd_instance.text(ERROR_BAD_REQUEST, 1)
96 print(ERROR_BAD_REQUEST)
97 except speech.RequestError:
98 lcd_instance.text(ERROR_UNAUTHORIZED, 1)
99 print(ERROR_UNAUTHORIZED)
100
101 return text
102
103
104def save_notes():
105 print("Type your notes (type 'stop' to exit):")
106 print("Type line=1 or line=2 to print something to a specific line")
107 while True:
108 line = 1
109 output = input(":")
110 output_length = len(output)
111 if output.lower() in ["stop", "break", "quit", "exit"]:
112 break
113 if output == "line=1":
114 line = 1
115 elif output == "line=2":
116 line = 2
117
118 if output_length < 16:
119 lcd_instance.text(output, line)
120 time.sleep(2)
121 else:
122 output_list = output.split("")
123 first_line = ""
124 second_line = ""
125 for i in output_list:
126 count = 0
127 if count > 16:
128 first_line += output_list[i]
129 count += 1
130 else:
131 second_line += output_list[i]
132 lcd.text(first_line, 1)
133 lcd.text(secon_line, 2)
134
135
136def command_center(commands):
137 # checking if we can reconize commands within the user speech
138 # requires ->
139 # converting the commands to human readable text
140 # no under scars
141 command = recognize_speech()
142 list = []
143 try:
144 for i in commands:
145 if i == command:
146 print("I think i found what you ment...")
147 command()
148 except:
149 print("ERROR 404 - COMMAND NOT RECOGNIZED")
150
151
152FEATURES = {
153 "READINGS": system_readings,
154 "UPTIME": display_uptime,
155 "SPEECH_TRANSCRIBER": recognize_speech,
156 "NOTES": save_notes,
157 "COMMAND CENTER": command_center,
158}
159
160
161def main():
162 lcd_instance.clear()
163 os.system("cls" if os.name == "nt" else "clear")
164 print("FEATURES:", ", ".join(FEATURES.keys()))
165
166 while True:
167 user_input = input("Enter command: ").upper()
168 action = FEATURES.get(user_input)
169
170 if action:
171 action()
172 else:
173 lcd_instance.text(ERROR_NOT_FOUND, 1)
174 print(ERROR_NOT_FOUND)
175
176
177def destroy():
178 lcd_instance.clear()
179 os.system("cls" if os.name == "nt" else "clear")
180
181
182if __name__ == "__main__":
183 try:
184 main()
185 except KeyboardInterrupt:
186 destroy()