summaryrefslogtreecommitdiff
path: root/bin/main.py
blob: 60978bbcaf9750f08bf4b587bddf0a2347f9ffda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import time
import os
import speech_recognition as speech
import sounddevice
import i2c as lcd
from gpiozero import CPUTemperature

ERROR_BAD_REQUEST = "400 Bad Request"
ERROR_UNAUTHORIZED = "401 Unauthorized"
ERROR_NOT_FOUND = "404 Not Found"
ERROR_TIMEOUT = "408 Request Timeout"

lcd_instance = lcd.LCD()
cpu_temp = CPUTemperature()
recognizer = speech.Recognizer()
microphone = speech.Microphone()


def display_cpu_info():
    lcd_instance.clear()
    while True:
        load = os.getloadavg()[0]
        temperature = cpu_temp.temperature
        lcd_instance.clear()
        lcd_instance.display_text(f"CPU Load: {load}", 1)
        lcd_instance.display_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.display_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.display_text(text, 1)
        print("Speech recognized:", text)
    except speech.UnknownValueError:
        lcd_instance.display_text(ERROR_BAD_REQUEST, 1)
        print(ERROR_BAD_REQUEST)
    except speech.RequestError:
        lcd_instance.display_text(ERROR_UNAUTHORIZED, 1)
        print(ERROR_UNAUTHORIZED)


def save_notes():
    print("Type your notes (type 'stop' to exit):")
    while True:
        output = input()
        if output.lower() in ["stop", "break", "quit", "exit"]:
            break
        lcd_instance.display_text(output, 1)
        time.sleep(2)


OPTIONS = {
    "CPU_INFO": display_cpu_info,
    "UPTIME": display_uptime,
    "SPEECH_TRANSCRIBER": recognize_speech,
    "NOTES": save_notes,
}


def main():
    lcd_instance.clear()
    print("WELCOME TO THE I2C COMMAND LINE CENTER")
    print("Options:", ", ".join(OPTIONS.keys()))

    while True:
        user_input = input("Enter command: ").upper()
        action = OPTIONS.get(user_input)

        if action:
            action()
        else:
            lcd_instance.display_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()