summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/assets/text/quotes.txt (renamed from bin/quotes.txt)2
-rw-r--r--bin/display_driver.c2
-rw-r--r--bin/source/hardware_driver.py71
-rw-r--r--bin/source/main.py180
4 files changed, 2 insertions, 253 deletions
diff --git a/bin/quotes.txt b/bin/assets/text/quotes.txt
index a6c9d84..9f778ed 100644
--- a/bin/quotes.txt
+++ b/bin/assets/text/quotes.txt
@@ -27,4 +27,4 @@
27“Keep it simple, stupid (KISS).” 27“Keep it simple, stupid (KISS).”
28“Without testing, code is poetry.” 28“Without testing, code is poetry.”
29“Good software comes from great design.” 29“Good software comes from great design.”
30“The harder you work, the luckier you get.” 30“The harder you work, the luckier you get.” \ No newline at end of file
diff --git a/bin/display_driver.c b/bin/display_driver.c
index 04f2900..adb845a 100644
--- a/bin/display_driver.c
+++ b/bin/display_driver.c
@@ -29,7 +29,7 @@ static void activate(GtkApplication *app, gpointer user_data) {
29 for (int i = 0; i < features_count; i++) { 29 for (int i = 0; i < features_count; i++) {
30 GtkWidget *button = gtk_button_new_with_label(features[i]); 30 GtkWidget *button = gtk_button_new_with_label(features[i]);
31 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0); 31 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
32 gtk_widget_set_size_request(button, 200, 100); 32 gtk_widget_set_size_request(button, 50, 50);
33 } 33 }
34 34
35 // Show all widgets 35 // Show all widgets
diff --git a/bin/source/hardware_driver.py b/bin/source/hardware_driver.py
deleted file mode 100644
index 7d48e69..0000000
--- a/bin/source/hardware_driver.py
+++ /dev/null
@@ -1,71 +0,0 @@
1from smbus import SMBus
2from time import sleep
3
4ALIGN_FUNC = {
5 'left': 'ljust',
6 'right': 'rjust',
7 'center': 'center'}
8CLEAR_DISPLAY = 0x01
9ENABLE_BIT = 0b00000100
10LINES = {
11 1: 0x80,
12 2: 0xC0,
13 3: 0x94,
14 4: 0xD4}
15
16LCD_BACKLIGHT = 0x08
17LCD_NOBACKLIGHT = 0x00
18
19class LCD(object):
20
21 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
22 self.address = address
23 self.bus = SMBus(bus)
24 self.delay = 0.0005
25 self.rows = rows
26 self.width = width
27 self.backlight_status = backlight
28
29 self.write(0x33)
30 self.write(0x32)
31 self.write(0x06)
32 self.write(0x0C)
33 self.write(0x28)
34 self.write(CLEAR_DISPLAY)
35 sleep(self.delay)
36
37 def _write_byte(self, byte):
38 self.bus.write_byte(self.address, byte)
39 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
40 sleep(self.delay)
41 self.bus.write_byte(self.address,(byte & ~ENABLE_BIT))
42 sleep(self.delay)
43
44 def write(self, byte, mode=0):
45 backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
46 self._write_byte(mode | (byte & 0xF0) | backlight_mode)
47 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode)
48
49 def text(self, text, line, align='left'):
50 self.write(LINES.get(line, LINES[1]))
51 text, other_lines = self.get_text_line(text)
52 text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width)
53 for char in text:
54 self.write(ord(char), mode=1)
55 if other_lines and line <= self.rows - 1:
56 self.text(other_lines, line + 1, align=align)
57
58 def backlight(self, turn_on=True):
59 self.backlight_status = turn_on
60 self.write(0)
61
62 def get_text_line(self, text):
63 line_break = self.width
64 if len(text) > self.width:
65 line_break = text[:self.width + 1].rfind(' ')
66 if line_break < 0:
67 line_break = self.width
68 return text[:line_break], text[line_break:].strip()
69
70 def clear(self):
71 self.write(CLEAR_DISPLAY)
diff --git a/bin/source/main.py b/bin/source/main.py
deleted file mode 100644
index 929db57..0000000
--- a/bin/source/main.py
+++ /dev/null
@@ -1,180 +0,0 @@
1import time
2import os
3import speech_recognition as sr
4from gpiozero import CPUTemperature
5import hardware_driver as lcd
6
7# Error messages
8ERROR_BAD_REQUEST = "400 Bad Request"
9ERROR_UNAUTHORIZED = "401 Unauthorized"
10ERROR_NOT_FOUND = "404 Not Found"
11SPEECH_NOT_RECOGNIZED = "404-1 Speech not recognized"
12ERROR_TIMEOUT = "408 Request Timeout"
13
14# Initialize components
15try:
16 lcd_instance = lcd.LCD()
17except Exception as e:
18 print("Error intializing LCD")
19try:
20 cpu_temp = CPUTemperature()
21except Exception as e:
22 print("Error initializing CPU temperature sensor:", e)
23
24try:
25 recognizer = sr.Recognizer()
26except Exception as e:
27 print("Error initialzing voice recognition, its possible the speech recognition module isn't installed")
28
29try:
30 microphone = sr.Microphone()
31except Exception as e:
32 print("Error initialzing the microphone \n check if the sound device package is installed")
33
34# clearing the terminal for a cleaner and program like interaction
35def clear_terminal():
36 os.system("cls" if os.name == "nt" else "clear")
37
38# Features
39def custom_greeting():
40 try:
41 with open("quotes.txt", "r") as file:
42 quotes = [quote.strip() for quote in file.readlines()]
43 except FileNotFoundError:
44 lcd_instance.text("Quotes file missing", 1)
45 return
46
47 for quote in quotes:
48 first_line = quote[:16]
49 second_line = quote[16:32]
50 lcd_instance.text(first_line, 1)
51 lcd_instance.text(second_line, 2)
52 time.sleep(3)
53 lcd_instance.clear()
54
55def pomodoro():
56 try:
57 duration_minutes = int(input("Enter duration in minutes: "))
58 duration_seconds = duration_minutes * 60
59 print("Pomodoro started for", duration_minutes, "minutes")
60 lcd_instance.text("Pomodoro Running", 1)
61 start_count = 0
62 count = 0
63 while duration_seconds > 0:
64 lcd_instance.text(f"Time left: {duration_minutes}:{duration_seconds * 60}", 2)
65 time.sleep(1)
66 duration_seconds -= 1
67 count += 1
68 if count == start_count + 60:
69 start_count = start
70 duration_minutes -= 1
71
72 lcd_instance.text("Time's Up!", 1)
73 time.sleep(3)
74 except ValueError:
75 lcd_instance.text("Invalid input", 1)
76 time.sleep(2)
77
78def system_readings():
79 while True:
80 load = os.getloadavg()[0]
81 temperature = cpu_temp.temperature if cpu_temp else "N/A"
82 lcd_instance.clear()
83 lcd_instance.text(f"CPU Load: {load:.2f}", 1)
84 lcd_instance.text(f"Temp: {temperature}C", 2)
85 time.sleep(5)
86
87def display_uptime():
88 try:
89 with open("/proc/uptime") as f:
90 uptime_seconds = float(f.readline().split()[0])
91 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
92 lcd_instance.text(f"Uptime: {uptime_str}", 1)
93 time.sleep(3)
94 except Exception as e:
95 lcd_instance.text("Error reading uptime", 1)
96 print("Error:", e)
97
98def recognize_speech():
99 lcd_instance.text("Listening...", 1)
100 try:
101 with microphone as source:
102 recognizer.adjust_for_ambient_noise(source)
103 audio = recognizer.listen(source)
104 output = recognizer.recognize_google(audio)
105 lcd_instance.text("Recognized:", 1)
106 lcd_instance.text(output[:16], 2)
107
108 print("Speech recognized:", output)
109 return output
110 except sr.UnknownValueError:
111 lcd_instance.text(SPEECH_NOT_RECOGNIZED, 1)
112 print(SPEECH_NOT_RECOGNIZED)
113 except sr.RequestError as e:
114 lcd_instance.text(ERROR_UNAUTHORIZED, 1)
115 print(ERROR_UNAUTHORIZED, e)
116 except Exception as e:
117 lcd_instance.text("Speech Error", 1)
118 print("Error:", e)
119 return None
120
121def save_notes():
122 print("Type your notes (type 'stop' to exit):")
123 while True:
124 note = input(": ")
125 if note.lower() in ["stop", "exit", "quit"]:
126 break
127 first_line = note[:16]
128 second_line = note[16:32]
129 lcd_instance.text(first_line, 1)
130 lcd_instance.text(second_line, 2)
131 time.sleep(3)
132
133# Command center to execute features
134def command_center():
135 command = recognize_speech().upper()
136 if command:
137 command()
138 else:
139 lcd_instance.text(ERROR_NOT_FOUND, 1)
140 print(ERROR_NOT_FOUND)
141
142# Features dictionary
143FEATURES = {
144 "GREETING": custom_greeting,
145 "READINGS": system_readings,
146 "UPTIME": display_uptime,
147 "SPEECH": recognize_speech,
148 "NOTE": save_notes,
149 "COMMAND": command_center,
150 "POMODORO": pomodoro,
151 }
152
153# Main Menu
154def main():
155 clear_terminal()
156 print("FEATURES:", ", ".join(FEATURES.keys()))
157 while True:
158 user_input = input("Enter command (or 'EXIT' to quit): ").upper()
159 if user_input in ["QUIT", "EXIT"]:
160 destroy()
161 break
162 action = FEATURES.get(user_input)
163 if action:
164 action()
165 else:
166 print(ERROR_NOT_FOUND)
167
168# Clean up on exit
169def destroy():
170 lcd_instance.clear()
171 clear_terminal()
172 print("Goodbye!")
173
174# Entry point
175if __name__ == "__main__":
176 try:
177 main()
178 except KeyboardInterrupt:
179 destroy()
180