From deadf64d1b05985f52cd64fd72232cfdb628b832 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Sun, 24 Nov 2024 12:59:50 +0100 Subject: moved the source files to a seperate folder --- bin/hardware_driver.py | 68 ------------------ bin/main.py | 183 ------------------------------------------------- 2 files changed, 251 deletions(-) delete mode 100644 bin/hardware_driver.py delete mode 100644 bin/main.py (limited to 'bin') diff --git a/bin/hardware_driver.py b/bin/hardware_driver.py deleted file mode 100644 index 1afb219..0000000 --- a/bin/hardware_driver.py +++ /dev/null @@ -1,68 +0,0 @@ -from smbus2 import SMBus -from time import sleep - -ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} -CLEAR_DISPLAY = 0x01 -ENABLE_BIT = 0b00000100 -LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} - -LCD_BACKLIGHT = 0x08 -LCD_NOBACKLIGHT = 0x00 - - -class LCD(object): - - def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): - self.address = address - self.bus = SMBus(bus) - self.delay = 0.0005 - self.rows = rows - self.width = width - self.backlight_status = backlight - - self.write(0x33) - self.write(0x32) - self.write(0x06) - self.write(0x0C) - self.write(0x28) - self.write(CLEAR_DISPLAY) - sleep(self.delay) - - def _write_byte(self, byte): - self.bus.write_byte(self.address, byte) - self.bus.write_byte(self.address, (byte | ENABLE_BIT)) - sleep(self.delay) - self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) - sleep(self.delay) - - def write(self, byte, mode=0): - backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT - self._write_byte(mode | (byte & 0xF0) | backlight_mode) - self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) - - def text(self, text, line, align="left"): - self.write(LINES.get(line, LINES[1])) - text, other_lines = self.get_text_line(text) - text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) - for char in text: - self.write(ord(char), mode=1) - if other_lines and line <= self.rows - 1: - self.text(other_lines, line + 1, align=align) - - def backlight(self, turn_on=True): - self.backlight_status = turn_on - self.write(0) - - def get_text_line(self, text): - line_break = self.width - if len(text) > self.width: - line_break = text[: self.width + 1].rfind(" ") - if line_break < 0: - line_break = self.width - return text[:line_break], text[line_break:].strip() - - def clear(self): - self.write(CLEAR_DISPLAY) - - def read(self): - self._read_i2c_block_data diff --git a/bin/main.py b/bin/main.py deleted file mode 100644 index bf556ca..0000000 --- a/bin/main.py +++ /dev/null @@ -1,183 +0,0 @@ -import time -import os -import speech_recognition as speech -import sounddevice -import bin.hardware_driver as lcd -from gpiozero import CPUTemperature - -ERROR_BAD_REQUEST = "400 Bad Request" -ERROR_UNAUTHORIZED = "401 Unauthorized" -ERROR_NOT_FOUND = "404 Not Found" -SPEECH_NOT_RECOGNIZED = "404-1 Speech is not recognized" -ERROR_TIMEOUT = "408 Request Timeout" - -lcd_instance = lcd.LCD() -cpu_temp = CPUTemperature() -recognizer = speech.Recognizer() -microphone = speech.Microphone() - - -# greeting that starts upon the boot of the device: -# shows a hello line; shorter than 16 chars -# and some small information on the second line -def custom_greeting(): - with open('quotes.txt', 'r') as file: - quotes = file.readlines() - - # Strip newline characters and use the quotes - quotes = [quote.strip() for quote in quotes] - - # Print the quotes - for quote in quotes: - print(quote) - first_line = "" - second_line = "" - count = 0 - for i in quote: - if count < 16: - first_line += i - count += 1 - else: - second_line += i - lcd.text(first_line,1) - lcd.text(secon_line,2) -def pomodoro(): - time = input("How long do you want to wait? : ") - print("Okay \nStarting Now...") - while time > 0: - time.sleep(1) - print(time + "Seconds") - lcd.text(time + " Seconds remaining...", 1) - time -= 1 - - -def weather(): - pass - - -# ram usage, internet speed, -def system_readings(): - lcd_instance.clear() - while True: - load = os.getloadavg()[0] - temperature = cpu_temp.temperature - lcd_instance.clear() - lcd_instance.text(f"CPU Load: {load}", 1) - lcd_instance.text(f"Temp: {temperature:.1f}C", 2) - time.sleep(5) - - -def display_uptime(): - lcd_instance.clear() - with open("/proc/uptime") as f: - uptime_seconds = float(f.readline().split()[0]) - uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) - lcd_instance.clear() - lcd_instance.text(f"Uptime: {uptime_str}", 1, "center") - - -def recognize_speech(): - - lcd_instance.clear() - try: - with microphone as source: - recognizer.adjust_for_ambient_noise(source) - print("Listening...") - audio = recognizer.listen(source) - text = recognizer.recognize_google(audio) - lcd_instance.clear() - lcd_instance.text(text, 1) - - print("Speech recognized:", text) - except speech.UnknownValueError: - lcd_instance.text(ERROR_BAD_REQUEST, 1) - print(ERROR_BAD_REQUEST) - except speech.RequestError: - lcd_instance.text(ERROR_UNAUTHORIZED, 1) - print(ERROR_UNAUTHORIZED) - - return text - - -def save_notes(): - print("Type your notes (type 'stop' to exit):") - print("Type line=1 or line=2 to print something to a specific line") - while True: - line = 1 - output = input(":") - output_length = len(output) - if output.lower() in ["stop", "break", "quit", "exit"]: - break - if output == "line=1": - line = 1 - elif output == "line=2": - line = 2 - - if output_length < 16: - lcd_instance.text(output, line) - time.sleep(2) - else: - output_list = output.split("") - first_line = "" - second_line = "" - for i in output_list: - count = 0 - if count > 16: - first_line += output_list[i] - count += 1 - else: - second_line += output_list[i] - lcd.text(first_line,1) - lcd.text(secon_line,2) - -def command_center(commands): - # checking if we can reconize commands within the user speech - # requires -> - # converting the commands to human readable text - # no under scars - command = recognize_speech() - list = [] - try: - for i in commands: - if i == command: - print("I think i found what you ment...") - command() - except: - print("ERROR 404 - COMMAND NOT RECOGNIZED") - - -FEATURES = { - "READINGS": system_readings, - "UPTIME": display_uptime, - "SPEECH_TRANSCRIBER": recognize_speech, - "NOTES": save_notes, - "COMMAND CENTER": command_center, - } - - -def main(): - lcd_instance.clear() - os.system("cls" if os.name == "nt" else "clear") - print("FEATURES:", ", ".join(FEATURES.keys())) - - while True: - user_input = input("Enter command: ").upper() - action = FEATURES.get(user_input) - - if action: - action() - else: - lcd_instance.text(ERROR_NOT_FOUND, 1) - print(ERROR_NOT_FOUND) - - -def destroy(): - lcd_instance.clear() - os.system("cls" if os.name == "nt" else "clear") - - -if __name__ == "__main__": - try: - main() - except KeyboardInterrupt: - destroy() -- cgit v1.2.3-70-g09d2 From b78c236641f3f8c1c2041910cc156397fdc6c293 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Sun, 24 Nov 2024 13:02:54 +0100 Subject: moving the maiun files ot a source folder --- bin/source/hardware_driver.py | 68 ++++++++++++++++ bin/source/main.py | 183 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 bin/source/hardware_driver.py create mode 100644 bin/source/main.py (limited to 'bin') 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 @@ +from smbus2 import SMBus +from time import sleep + +ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} +CLEAR_DISPLAY = 0x01 +ENABLE_BIT = 0b00000100 +LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} + +LCD_BACKLIGHT = 0x08 +LCD_NOBACKLIGHT = 0x00 + + +class LCD(object): + + def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): + self.address = address + self.bus = SMBus(bus) + self.delay = 0.0005 + self.rows = rows + self.width = width + self.backlight_status = backlight + + self.write(0x33) + self.write(0x32) + self.write(0x06) + self.write(0x0C) + self.write(0x28) + self.write(CLEAR_DISPLAY) + sleep(self.delay) + + def _write_byte(self, byte): + self.bus.write_byte(self.address, byte) + self.bus.write_byte(self.address, (byte | ENABLE_BIT)) + sleep(self.delay) + self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) + sleep(self.delay) + + def write(self, byte, mode=0): + backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT + self._write_byte(mode | (byte & 0xF0) | backlight_mode) + self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) + + def text(self, text, line, align="left"): + self.write(LINES.get(line, LINES[1])) + text, other_lines = self.get_text_line(text) + text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) + for char in text: + self.write(ord(char), mode=1) + if other_lines and line <= self.rows - 1: + self.text(other_lines, line + 1, align=align) + + def backlight(self, turn_on=True): + self.backlight_status = turn_on + self.write(0) + + def get_text_line(self, text): + line_break = self.width + if len(text) > self.width: + line_break = text[: self.width + 1].rfind(" ") + if line_break < 0: + line_break = self.width + return text[:line_break], text[line_break:].strip() + + def clear(self): + self.write(CLEAR_DISPLAY) + + def read(self): + 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 @@ +import time +import os +import speech_recognition as speech +import sounddevice +import bin.hardware_driver as lcd +from gpiozero import CPUTemperature + +ERROR_BAD_REQUEST = "400 Bad Request" +ERROR_UNAUTHORIZED = "401 Unauthorized" +ERROR_NOT_FOUND = "404 Not Found" +SPEECH_NOT_RECOGNIZED = "404-1 Speech is not recognized" +ERROR_TIMEOUT = "408 Request Timeout" + +lcd_instance = lcd.LCD() +cpu_temp = CPUTemperature() +recognizer = speech.Recognizer() +microphone = speech.Microphone() + + +# greeting that starts upon the boot of the device: +# shows a hello line; shorter than 16 chars +# and some small information on the second line +def custom_greeting(): + with open('quotes.txt', 'r') as file: + quotes = file.readlines() + + # Strip newline characters and use the quotes + quotes = [quote.strip() for quote in quotes] + + # Print the quotes + for quote in quotes: + print(quote) + first_line = "" + second_line = "" + count = 0 + for i in quote: + if count < 16: + first_line += i + count += 1 + else: + second_line += i + lcd.text(first_line,1) + lcd.text(secon_line,2) +def pomodoro(): + time = input("How long do you want to wait? : ") + print("Okay \nStarting Now...") + while time > 0: + time.sleep(1) + print(time + "Seconds") + lcd.text(time + " Seconds remaining...", 1) + time -= 1 + + +def weather(): + pass + + +# ram usage, internet speed, +def system_readings(): + lcd_instance.clear() + while True: + load = os.getloadavg()[0] + temperature = cpu_temp.temperature + lcd_instance.clear() + lcd_instance.text(f"CPU Load: {load}", 1) + lcd_instance.text(f"Temp: {temperature:.1f}C", 2) + time.sleep(5) + + +def display_uptime(): + lcd_instance.clear() + with open("/proc/uptime") as f: + uptime_seconds = float(f.readline().split()[0]) + uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) + lcd_instance.clear() + lcd_instance.text(f"Uptime: {uptime_str}", 1, "center") + + +def recognize_speech(): + + lcd_instance.clear() + try: + with microphone as source: + recognizer.adjust_for_ambient_noise(source) + print("Listening...") + audio = recognizer.listen(source) + text = recognizer.recognize_google(audio) + lcd_instance.clear() + lcd_instance.text(text, 1) + + print("Speech recognized:", text) + except speech.UnknownValueError: + lcd_instance.text(ERROR_BAD_REQUEST, 1) + print(ERROR_BAD_REQUEST) + except speech.RequestError: + lcd_instance.text(ERROR_UNAUTHORIZED, 1) + print(ERROR_UNAUTHORIZED) + + return text + + +def save_notes(): + print("Type your notes (type 'stop' to exit):") + print("Type line=1 or line=2 to print something to a specific line") + while True: + line = 1 + output = input(":") + output_length = len(output) + if output.lower() in ["stop", "break", "quit", "exit"]: + break + if output == "line=1": + line = 1 + elif output == "line=2": + line = 2 + + if output_length < 16: + lcd_instance.text(output, line) + time.sleep(2) + else: + output_list = output.split("") + first_line = "" + second_line = "" + for i in output_list: + count = 0 + if count > 16: + first_line += output_list[i] + count += 1 + else: + second_line += output_list[i] + lcd.text(first_line,1) + lcd.text(secon_line,2) + +def command_center(commands): + # checking if we can reconize commands within the user speech + # requires -> + # converting the commands to human readable text + # no under scars + command = recognize_speech() + list = [] + try: + for i in commands: + if i == command: + print("I think i found what you ment...") + command() + except: + print("ERROR 404 - COMMAND NOT RECOGNIZED") + + +FEATURES = { + "READINGS": system_readings, + "UPTIME": display_uptime, + "SPEECH_TRANSCRIBER": recognize_speech, + "NOTES": save_notes, + "COMMAND CENTER": command_center, + } + + +def main(): + lcd_instance.clear() + os.system("cls" if os.name == "nt" else "clear") + print("FEATURES:", ", ".join(FEATURES.keys())) + + while True: + user_input = input("Enter command: ").upper() + action = FEATURES.get(user_input) + + if action: + action() + else: + lcd_instance.text(ERROR_NOT_FOUND, 1) + print(ERROR_NOT_FOUND) + + +def destroy(): + lcd_instance.clear() + os.system("cls" if os.name == "nt" else "clear") + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + destroy() -- cgit v1.2.3-70-g09d2