summaryrefslogtreecommitdiff
path: root/bin/source/main.py
blob: bbd9dfa31a8fc61a17949665c604d93a21164937 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import time
import os
import speech_recognition as speech
import sounddevice
import 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(second_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()