diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-26 23:49:24 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-26 23:49:24 +0100 |
| commit | 4e86a2ee9f5578cbaae6cc7dd52616e3e3751764 (patch) | |
| tree | 9aef587c109accee021553e9e95f26dab11e8920 | |
| parent | fdc9132f0e8fc69d0f500ec07638ac595293083b (diff) | |
rearangef the file managment
| -rw-r--r-- | .DS_Store | bin | 6148 -> 6148 bytes | |||
| -rw-r--r-- | .vscode/settings.json | 22 | ||||
| -rw-r--r-- | assets/text/quotes.txt (renamed from bin/quotes.txt) | 0 | ||||
| -rw-r--r-- | bin/assets/text/quotes.txt | 30 | ||||
| -rw-r--r-- | bin/display_driver.c | 2 | ||||
| -rw-r--r-- | bin/source/hardware_driver.py | 71 | ||||
| -rw-r--r-- | bin/source/main.py | 180 | ||||
| -rw-r--r-- | docs/guide.txt | 13 | ||||
| -rw-r--r-- | templates/api_server.service (renamed from api_server.service) | 0 |
9 files changed, 44 insertions, 274 deletions
| Binary files differ | |||
diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index dd10108..0000000 --- a/.vscode/settings.json +++ /dev/null | |||
| @@ -1,22 +0,0 @@ | |||
| 1 | { | ||
| 2 | "workbench.colorCustomizations": { | ||
| 3 | "activityBar.activeBackground": "#1a1a1a", | ||
| 4 | "activityBar.background": "#1a1a1a", | ||
| 5 | "activityBar.foreground": "#e7e7e7", | ||
| 6 | "activityBar.inactiveForeground": "#e7e7e799", | ||
| 7 | "activityBarBadge.background": "#606020", | ||
| 8 | "activityBarBadge.foreground": "#e7e7e7", | ||
| 9 | "commandCenter.border": "#e7e7e799", | ||
| 10 | "sash.hoverBorder": "#1a1a1a", | ||
| 11 | "statusBar.background": "#000000", | ||
| 12 | "statusBar.foreground": "#e7e7e7", | ||
| 13 | "statusBarItem.hoverBackground": "#1a1a1a", | ||
| 14 | "statusBarItem.remoteBackground": "#000000", | ||
| 15 | "statusBarItem.remoteForeground": "#e7e7e7", | ||
| 16 | "titleBar.activeBackground": "#000000", | ||
| 17 | "titleBar.activeForeground": "#e7e7e7", | ||
| 18 | "titleBar.inactiveBackground": "#00000099", | ||
| 19 | "titleBar.inactiveForeground": "#e7e7e799" | ||
| 20 | }, | ||
| 21 | "peacock.color": "black" | ||
| 22 | } \ No newline at end of file | ||
diff --git a/bin/quotes.txt b/assets/text/quotes.txt index a6c9d84..a6c9d84 100644 --- a/bin/quotes.txt +++ b/assets/text/quotes.txt | |||
diff --git a/bin/assets/text/quotes.txt b/bin/assets/text/quotes.txt new file mode 100644 index 0000000..9f778ed --- /dev/null +++ b/bin/assets/text/quotes.txt | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | “Code is like humor, it’s better if it’s clean.” | ||
| 2 | “Software is a great combination.” | ||
| 3 | “Computers are like a bicycle for the mind.” | ||
| 4 | “Simplicity is the soul of efficiency.” | ||
| 5 | “Good code is its own best documentation.” | ||
| 6 | “In programming, clarity is key.” | ||
| 7 | “Programs must be written for people.” | ||
| 8 | “Computers are only tools.” | ||
| 9 | “The best way to predict the future is to invent it.” | ||
| 10 | “Talk is cheap. Show me the code.” | ||
| 11 | "It’s not a bug, it’s a feature.” | ||
| 12 | “The computer is mightier than the pen.” | ||
| 13 | "Code is the art of telling computers what to do.” | ||
| 14 | "Make it work, make it right, make it fast.” | ||
| 15 | “Every problem can be solved with code.” | ||
| 16 | “The only limit is your imagination.” | ||
| 17 | “Programming is thinking, not typing.” | ||
| 18 | "Design is the soul of programming.” | ||
| 19 | “There’s no place like 127.0.0.1.” | ||
| 20 | “Without algorithms, there’s no solution.” | ||
| 21 | “Compilers turn ideas into machine code.” | ||
| 22 | “A program is only as good as its design.” | ||
| 23 | "Code never lies, comments sometimes do.” | ||
| 24 | “Debugging is twice as hard as writing.” | ||
| 25 | "Computers don’t make mistakes, programmers do.” | ||
| 26 | “Code fast, debug slow, optimize last.” | ||
| 27 | “Keep it simple, stupid (KISS).” | ||
| 28 | “Without testing, code is poetry.” | ||
| 29 | “Good software comes from great design.” | ||
| 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 @@ | |||
| 1 | from smbus import SMBus | ||
| 2 | from time import sleep | ||
| 3 | |||
| 4 | ALIGN_FUNC = { | ||
| 5 | 'left': 'ljust', | ||
| 6 | 'right': 'rjust', | ||
| 7 | 'center': 'center'} | ||
| 8 | CLEAR_DISPLAY = 0x01 | ||
| 9 | ENABLE_BIT = 0b00000100 | ||
| 10 | LINES = { | ||
| 11 | 1: 0x80, | ||
| 12 | 2: 0xC0, | ||
| 13 | 3: 0x94, | ||
| 14 | 4: 0xD4} | ||
| 15 | |||
| 16 | LCD_BACKLIGHT = 0x08 | ||
| 17 | LCD_NOBACKLIGHT = 0x00 | ||
| 18 | |||
| 19 | class 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 @@ | |||
| 1 | import time | ||
| 2 | import os | ||
| 3 | import speech_recognition as sr | ||
| 4 | from gpiozero import CPUTemperature | ||
| 5 | import hardware_driver as lcd | ||
| 6 | |||
| 7 | # Error messages | ||
| 8 | ERROR_BAD_REQUEST = "400 Bad Request" | ||
| 9 | ERROR_UNAUTHORIZED = "401 Unauthorized" | ||
| 10 | ERROR_NOT_FOUND = "404 Not Found" | ||
| 11 | SPEECH_NOT_RECOGNIZED = "404-1 Speech not recognized" | ||
| 12 | ERROR_TIMEOUT = "408 Request Timeout" | ||
| 13 | |||
| 14 | # Initialize components | ||
| 15 | try: | ||
| 16 | lcd_instance = lcd.LCD() | ||
| 17 | except Exception as e: | ||
| 18 | print("Error intializing LCD") | ||
| 19 | try: | ||
| 20 | cpu_temp = CPUTemperature() | ||
| 21 | except Exception as e: | ||
| 22 | print("Error initializing CPU temperature sensor:", e) | ||
| 23 | |||
| 24 | try: | ||
| 25 | recognizer = sr.Recognizer() | ||
| 26 | except Exception as e: | ||
| 27 | print("Error initialzing voice recognition, its possible the speech recognition module isn't installed") | ||
| 28 | |||
| 29 | try: | ||
| 30 | microphone = sr.Microphone() | ||
| 31 | except 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 | ||
| 35 | def clear_terminal(): | ||
| 36 | os.system("cls" if os.name == "nt" else "clear") | ||
| 37 | |||
| 38 | # Features | ||
| 39 | def 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 | |||
| 55 | def 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 | |||
| 78 | def 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 | |||
| 87 | def 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 | |||
| 98 | def 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 | |||
| 121 | def 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 | ||
| 134 | def 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 | ||
| 143 | FEATURES = { | ||
| 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 | ||
| 154 | def 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 | ||
| 169 | def destroy(): | ||
| 170 | lcd_instance.clear() | ||
| 171 | clear_terminal() | ||
| 172 | print("Goodbye!") | ||
| 173 | |||
| 174 | # Entry point | ||
| 175 | if __name__ == "__main__": | ||
| 176 | try: | ||
| 177 | main() | ||
| 178 | except KeyboardInterrupt: | ||
| 179 | destroy() | ||
| 180 | |||
diff --git a/docs/guide.txt b/docs/guide.txt new file mode 100644 index 0000000..b248f49 --- /dev/null +++ b/docs/guide.txt | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | ------------------------------------------------------------- | ||
| 2 | |||
| 3 | I2C CONTROLLER GUIDE | ||
| 4 | |||
| 5 | ------------------------------------------------------------- | ||
| 6 | |||
| 7 | This program is designed to control an LCD1602 using python. It supports | ||
| 8 | a GUI and CLI interface and is at the moment only linux compatible. | ||
| 9 | |||
| 10 | You will need to have a couple of dependency's readdy on your device to use the software. | ||
| 11 | |||
| 12 | * sounddevice | ||
| 13 | * ... | ||
diff --git a/api_server.service b/templates/api_server.service index b69c5e4..b69c5e4 100644 --- a/api_server.service +++ b/templates/api_server.service | |||
