summaryrefslogtreecommitdiff
path: root/bin/source
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2024-11-24 13:04:20 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2024-11-24 13:04:20 +0100
commitd2dea805ee0ec0724a5f2802fed71d20036799bc (patch)
tree920b2c07dc37a497a6b61b9e65eab96a0adae700 /bin/source
parent9f3e2f9df51db83a0fd64e2979f71394ab3cef36 (diff)
parentb78c236641f3f8c1c2041910cc156397fdc6c293 (diff)
Merge remote-tracking branch 'refs/remotes/origin/main'
Diffstat (limited to 'bin/source')
-rw-r--r--bin/source/hardware_driver.py68
-rw-r--r--bin/source/main.py186
2 files changed, 254 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..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()