diff options
Diffstat (limited to 'bin/main.py')
| -rw-r--r-- | bin/main.py | 109 |
1 files changed, 103 insertions, 6 deletions
diff --git a/bin/main.py b/bin/main.py index 2231547..921082f 100644 --- a/bin/main.py +++ b/bin/main.py | |||
| @@ -1,9 +1,106 @@ | |||
| 1 | import tk | 1 | import time |
| 2 | import source as main | 2 | import os |
| 3 | import speech_recognition as speech | ||
| 4 | import sounddevice | ||
| 5 | import i2c as LCD | ||
| 6 | from gpiozero import CPUTemperature | ||
| 3 | 7 | ||
| 4 | source = main() | 8 | ERROR_BAD_REQUEST = "400 Bad Request" |
| 9 | ERROR_UNAUTHORIZED = "401 Unauthorized" | ||
| 10 | ERROR_NOT_FOUND = "404 Not Found" | ||
| 11 | ERROR_TIMEOUT = "408 Request Timeout" | ||
| 5 | 12 | ||
| 6 | display = tk.Tk() | 13 | lcd = LCD |
| 7 | display.title("Kasper") | 14 | cpu_temp = CPUTemperature() |
| 15 | recognizer = speech.Recognizer() | ||
| 16 | microphone = speech.Microphone() | ||
| 8 | 17 | ||
| 9 | display.mainloop() \ No newline at end of file | 18 | |
| 19 | def display_cpu_info(): | ||
| 20 | lcd.clear() | ||
| 21 | while True: | ||
| 22 | load = os.getloadavg()[0] | ||
| 23 | temperature = cpu_temp.temperature | ||
| 24 | lcd.clear() | ||
| 25 | lcd.display_text(f"CPU Load:i {load}", 1) | ||
| 26 | lcd.display_text(f"Temp: {temperature:}C", 2) | ||
| 27 | time.sleep(5) | ||
| 28 | |||
| 29 | |||
| 30 | def display_uptime(): | ||
| 31 | lcd.clear() | ||
| 32 | with open("/proc/uptime") as f: | ||
| 33 | uptime_seconds = float(f.readline().split()[0]) | ||
| 34 | uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) | ||
| 35 | lcd.clear() | ||
| 36 | lcd.display_text(f"Uptime: {uptime_str}", 1, "center") | ||
| 37 | |||
| 38 | |||
| 39 | def recognize_speech(): | ||
| 40 | lcd.clear() | ||
| 41 | try: | ||
| 42 | with microphone as source: | ||
| 43 | recognizer.adjust_for_ambient_noise(source) | ||
| 44 | print("Listening...") | ||
| 45 | audio = recognizer.listen(source) | ||
| 46 | text = recognizer.recognize_google(audio) | ||
| 47 | lcd.clear() | ||
| 48 | lcd.display_text(text, 1) | ||
| 49 | print("Speech recognized:", text) | ||
| 50 | except speech.UnknownValueError: | ||
| 51 | lcd.display_text(ERROR_BAD_REQUEST, 1) | ||
| 52 | print(ERROR_BAD_REQUEST) | ||
| 53 | except speech.RequestError: | ||
| 54 | lcd.display_text(ERROR_UNAUTHORIZED, 1) | ||
| 55 | print(ERROR_UNAUTHORIZED) | ||
| 56 | |||
| 57 | |||
| 58 | def save_notes(): | ||
| 59 | PRINT_REQUEST = True | ||
| 60 | EXIT_CODES = ["stop", "break", "quit", "exit"] | ||
| 61 | if PRINT_REQUEST == True: | ||
| 62 | while True: | ||
| 63 | OUTPUT = input() | ||
| 64 | print(OUTPUT) | ||
| 65 | lcd.display_text(OUTPUT, 1) | ||
| 66 | time.sleep(2) | ||
| 67 | for i in EXIT_CODES: | ||
| 68 | if OUTPUT == i: | ||
| 69 | PRINT_REQUEST == False | ||
| 70 | |||
| 71 | |||
| 72 | OPTIONS = { | ||
| 73 | "CPU_INFO": display_cpu_info(), | ||
| 74 | "UPTIME": display_uptime(), | ||
| 75 | "SPEECH_TRANSCRIBER": recognize_speech(), | ||
| 76 | "NOTES": save_notes(), | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | def main(): | ||
| 81 | lcd.clear() | ||
| 82 | print("WELCOME TO THE I2C COMMAND LINE CENTER") | ||
| 83 | print("Options:", ", ".join(OPTIONS.keys())) | ||
| 84 | |||
| 85 | while True: | ||
| 86 | user_input = input("Enter command: ").upper() | ||
| 87 | action = OPTIONS.get(user_input) | ||
| 88 | |||
| 89 | if action: | ||
| 90 | action() | ||
| 91 | else: | ||
| 92 | lcd.display_text(ERROR_NOT_FOUND, 1) | ||
| 93 | print(ERROR_NOT_FOUND) | ||
| 94 | |||
| 95 | |||
| 96 | def destroy(): | ||
| 97 | lcd.clear() | ||
| 98 | os.system("cls" if os.name == "nt" else "clear") | ||
| 99 | |||
| 100 | |||
| 101 | if __name__ == "__main__": | ||
| 102 | os.system("cls" if os.name == "nt" else "clear") | ||
| 103 | try: | ||
| 104 | main() | ||
| 105 | except KeyboardInterrupt: | ||
| 106 | destroy() | ||
