summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornasrlol <nsrddyn@gmail.com>2024-11-24 13:02:54 +0100
committernasrlol <nsrddyn@gmail.com>2024-11-24 13:02:54 +0100
commitb78c236641f3f8c1c2041910cc156397fdc6c293 (patch)
tree0bb72e1bc3c1fa6a877002b738635183e1f9c2b3
parentdeadf64d1b05985f52cd64fd72232cfdb628b832 (diff)
moving the maiun files ot a source folder
-rw-r--r--bin/source/hardware_driver.py68
-rw-r--r--bin/source/main.py183
2 files changed, 251 insertions, 0 deletions
diff --git a/bin/source/hardware_driver.py b/bin/source/hardware_driver.py
new file mode 100644
index 0000000..1afb219
--- /dev/null
+++ b/bin/source/hardware_driver.py
@@ -0,0 +1,68 @@
1from smbus2 import SMBus
2from time import sleep
3
4ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
5CLEAR_DISPLAY = 0x01
6ENABLE_BIT = 0b00000100
7LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4}
8
9LCD_BACKLIGHT = 0x08
10LCD_NOBACKLIGHT = 0x00
11
12
13class LCD(object):
14
15 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
16 self.address = address
17 self.bus = SMBus(bus)
18 self.delay = 0.0005
19 self.rows = rows
20 self.width = width
21 self.backlight_status = backlight
22
23 self.write(0x33)
24 self.write(0x32)
25 self.write(0x06)
26 self.write(0x0C)
27 self.write(0x28)
28 self.write(CLEAR_DISPLAY)
29 sleep(self.delay)
30
31 def _write_byte(self, byte):
32 self.bus.write_byte(self.address, byte)
33 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
34 sleep(self.delay)
35 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
36 sleep(self.delay)
37
38 def write(self, byte, mode=0):
39 backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
40 self._write_byte(mode | (byte & 0xF0) | backlight_mode)
41 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode)
42
43 def text(self, text, line, align="left"):
44 self.write(LINES.get(line, LINES[1]))
45 text, other_lines = self.get_text_line(text)
46 text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
47 for char in text:
48 self.write(ord(char), mode=1)
49 if other_lines and line <= self.rows - 1:
50 self.text(other_lines, line + 1, align=align)
51
52 def backlight(self, turn_on=True):
53 self.backlight_status = turn_on
54 self.write(0)
55
56 def get_text_line(self, text):
57 line_break = self.width
58 if len(text) > self.width:
59 line_break = text[: self.width + 1].rfind(" ")
60 if line_break < 0:
61 line_break = self.width
62 return text[:line_break], text[line_break:].strip()
63
64 def clear(self):
65 self.write(CLEAR_DISPLAY)
66
67 def read(self):
68 self._read_i2c_block_data
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()