diff options
| -rw-r--r-- | source/hardware_driver.py | 71 | ||||
| -rw-r--r-- | source/main.py | 180 |
2 files changed, 251 insertions, 0 deletions
diff --git a/source/hardware_driver.py b/source/hardware_driver.py new file mode 100644 index 0000000..7d48e69 --- /dev/null +++ b/source/hardware_driver.py | |||
| @@ -0,0 +1,71 @@ | |||
| 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/source/main.py b/source/main.py new file mode 100644 index 0000000..929db57 --- /dev/null +++ b/source/main.py | |||
| @@ -0,0 +1,180 @@ | |||
| 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 | |||
