summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/__pycache__/source.cpython-313.pycbin0 -> 5737 bytes
-rw-r--r--bin/i2c.py47
-rw-r--r--bin/icon.pngbin3754 -> 0 bytes
-rw-r--r--bin/kasper_gui.py6
-rw-r--r--bin/kasper_source.py154
-rw-r--r--bin/main.py9
-rw-r--r--bin/source.py128
7 files changed, 184 insertions, 160 deletions
diff --git a/bin/__pycache__/source.cpython-313.pyc b/bin/__pycache__/source.cpython-313.pyc
new file mode 100644
index 0000000..79aee1d
--- /dev/null
+++ b/bin/__pycache__/source.cpython-313.pyc
Binary files differ
diff --git a/bin/i2c.py b/bin/i2c.py
new file mode 100644
index 0000000..a79d9d1
--- /dev/null
+++ b/bin/i2c.py
@@ -0,0 +1,47 @@
1import smbus2 as SMBus
2import time
3
4LCD_BACKLIGHT = 0x08
5LCD_NOBACKLIGHT = 0x00
6ENABLE_BIT = 0b00000100
7LINES = {1: 0x80, 2: 0xC0}
8ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
9
10
11class LCD:
12
13 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
14 self.address = address
15 self.bus = SMBus(bus)
16 self.width = width
17 self.rows = rows
18 self.backlight_status = backlight
19 self.delay = 0.0005
20
21 for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01):
22 self.write(cmd)
23 time.sleep(self.delay)
24
25 def write(self, byte, mode=0):
26 backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
27 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight)
28
29 def _write_byte(self, byte):
30 self.bus.write_byte(self.address, byte)
31 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
32 time.sleep(self.delay)
33 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
34 time.sleep(self.delay)
35
36 def display_text(self, text, line=1, align="left"):
37 self.write(LINES.get(line, LINES[1]))
38 aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
39 for char in aligned_text:
40 self.write(ord(char), mode=1)
41
42 def clear(self):
43 self.write(0x01)
44
45 def set_backlight(self, turn_on=True):
46 self.backlight_status = turn_on
47 self.write(0)
diff --git a/bin/icon.png b/bin/icon.png
deleted file mode 100644
index c372c49..0000000
--- a/bin/icon.png
+++ /dev/null
Binary files differ
diff --git a/bin/kasper_gui.py b/bin/kasper_gui.py
deleted file mode 100644
index e5edc6a..0000000
--- a/bin/kasper_gui.py
+++ /dev/null
@@ -1,6 +0,0 @@
1import tkinter
2
3display = tkinter.Tk()
4display.title("Kasper")
5
6display.mainloop() \ No newline at end of file
diff --git a/bin/kasper_source.py b/bin/kasper_source.py
deleted file mode 100644
index 8067997..0000000
--- a/bin/kasper_source.py
+++ /dev/null
@@ -1,154 +0,0 @@
1from smbus import SMBus
2from gpiozero import CPUTemperature
3import speech_recognition as speech
4import os
5import time
6from time import sleep
7
8# LCD Constants
9LCD_BACKLIGHT = 0x08
10LCD_NOBACKLIGHT = 0x00
11ENABLE_BIT = 0b00000100
12LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4}
13ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
14
15# Error Messages
16ERROR_BAD_REQUEST = "400 Bad Request"
17ERROR_UNAUTHORIZED = "401 Unauthorized"
18ERROR_NOT_FOUND = "404 Not Found"
19ERROR_TIMEOUT = "408 Request Timeout"
20
21# LCD Control Class
22class LCD:
23
24 def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True):
25 self.address = address
26 self.bus = SMBus(bus)
27 self.width = width
28 self.rows = rows
29 self.backlight_status = backlight
30 self.delay = 0.0005
31
32 # LCD Initialization
33 for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01):
34 self.write(cmd)
35 time.sleep(self.delay)
36
37 def write(self, byte, mode=0):
38 """Send a command or character to the LCD."""
39 backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
40 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight)
41
42 def _write_byte(self, byte):
43 """Write a byte to the I2C bus."""
44 self.bus.write_byte(self.address, byte)
45 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
46 time.sleep(self.delay)
47 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
48 time.sleep(self.delay)
49
50 def display_text(self, text, line=1, align="left"):
51 """Display text on a specified line with alignment."""
52 self.write(LINES.get(line, LINES[1]))
53 aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
54 for char in aligned_text:
55 self.write(ord(char), mode=1)
56
57 def clear(self):
58 """Clear the display."""
59 self.write(0x01)
60
61 def set_backlight(self, turn_on=True):
62 """Toggle backlight on or off."""
63 self.backlight_status = turn_on
64 self.write(0)
65
66# Initialize components
67lcd = LCD()
68cpu_temp = CPUTemperature()
69recognizer = speech.Recognizer()
70microphone = speech.Microphone()
71
72
73# Display Functions
74def display_cpu_info():
75 # clearing the display before accessing it
76 lcd.clear()
77 """Display CPU load and temperature on the LCD."""
78 while True:
79 load = os.getloadavg()[0] # 1-minute load average
80 temperature = cpu_temp.temperature
81 lcd.clear()
82 lcd.display_text(f"CPU Load: {load:.2f}", 1)
83 lcd.display_text(f"Temp: {temperature:.1f}C", 2)
84 time.sleep(5)
85
86
87def display_uptime():
88 # clearing the display before accessing it
89 lcd.clear()
90 """Display system uptime on the LCD."""
91 with open("/proc/uptime") as f:
92 uptime_seconds = float(f.readline().split()[0])
93 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
94 lcd.clear()
95 lcd.display_text(f"Uptime: {uptime_str}", 1)
96
97
98def recognize_speech():
99 # clearing the display before accessing it
100 lcd.clear()
101 """Capture and transcribe speech input."""
102 try:
103 with microphone as source:
104 recognizer.adjust_for_ambient_noise(source)
105 print("Listening...")
106 audio = recognizer.listen(source)
107 text = recognizer.recognize_google(audio)
108 lcd.clear()
109 lcd.display_text(text, 1)
110 print("Speech recognized:", text)
111 except speech.UnknownValueError:
112 lcd.display_text(ERROR_BAD_REQUEST, 1)
113 print(ERROR_BAD_REQUEST)
114 except speech.RequestError:
115 lcd.display_text(ERROR_UNAUTHORIZED, 1)
116 print(ERROR_UNAUTHORIZED)
117
118def notes():
119 while True:
120 OUTPUT = input()
121 print(OUTPUT)
122 lcd.display_text(OUTPUT, 1)
123 sleep(2)
124
125
126# Main Program Options
127OPTIONS = {
128 "CPU_INFO": display_cpu_info,
129 "UPTIME": display_uptime,
130 "SPEECH_TRANSCRIBER": recognize_speech,
131 "NOTES": notes,
132}
133
134
135def main():
136 # clearing the display before doing anything
137 lcd.clear()
138 # Main program loop to accept user commands.
139 print("WELCOME TO THE I2C COMMAND LINE CENTER")
140 print("Options:", ", ".join(OPTIONS.keys()))
141
142 while True:
143 user_input = input("Enter command: ").upper()
144 action = OPTIONS.get(user_input)
145
146 if action:
147 action()
148 else:
149 lcd.display_text(ERROR_NOT_FOUND, 1)
150 print(ERROR_NOT_FOUND)
151
152
153if __name__ == "__main__":
154 main()
diff --git a/bin/main.py b/bin/main.py
new file mode 100644
index 0000000..2231547
--- /dev/null
+++ b/bin/main.py
@@ -0,0 +1,9 @@
1import tk
2import source as main
3
4source = main()
5
6display = tk.Tk()
7display.title("Kasper")
8
9display.mainloop() \ No newline at end of file
diff --git a/bin/source.py b/bin/source.py
new file mode 100644
index 0000000..c232351
--- /dev/null
+++ b/bin/source.py
@@ -0,0 +1,128 @@
1import time
2import os
3import ollama
4import speech_recognition as speech
5import i2c as LCD
6from gpiozero import CPUTemperature
7
8ERROR_BAD_REQUEST = "400 Bad Request"
9ERROR_UNAUTHORIZED = "401 Unauthorized"
10ERROR_NOT_FOUND = "404 Not Found"
11ERROR_TIMEOUT = "408 Request Timeout"
12
13lcd = LCD()
14cpu_temp = CPUTemperature()
15lama = ollama()
16recognizer = speech.Recognizer()
17microphone = speech.Microphone()
18
19
20def display_cpu_info():
21 lcd.clear()
22 while True:
23 load = os.getloadavg()[0] # 1-minute load average
24 temperature = cpu_temp.temperature
25 lcd.clear()
26 lcd.display_text(f"CPU Load:i {load}", 1)
27 lcd.display_text(f"Temp: {temperature:}C", 2)
28 time.sleep(5)
29
30
31def display_uptime():
32 lcd.clear()
33 with open("/proc/uptime") as f:
34 uptime_seconds = float(f.readline().split()[0])
35 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
36 lcd.clear()
37 lcd.display_text(f"Uptime: {uptime_str}", 1, "center")
38
39
40def recognize_speech():
41 lcd.clear()
42 try:
43 with microphone as source:
44 recognizer.adjust_for_ambient_noise(source)
45 print("Listening...")
46 audio = recognizer.listen(source)
47 text = recognizer.recognize_google(audio)
48 lcd.clear()
49 lcd.display_text(text, 1)
50 print("Speech recognized:", text)
51 except speech.UnknownValueError:
52 lcd.display_text(ERROR_BAD_REQUEST, 1)
53 print(ERROR_BAD_REQUEST)
54 except speech.RequestError:
55 lcd.display_text(ERROR_UNAUTHORIZED, 1)
56 print(ERROR_UNAUTHORIZED)
57
58
59def save_notes():
60 PRINT_REQUEST = True
61 EXIT_CODES = ['stop', 'break', 'quit', 'exit']
62 if PRINT_REQUEST == True:
63 while True:
64 OUTPUT = input()
65 print(OUTPUT)
66 lcd.display_text(OUTPUT, 1)
67 time.sleep(2)
68 for i in EXIT_CODES:
69 if OUTPUT == i:
70 PRINT_REQUEST == False
71
72def lama():
73 QUESTION_REQUEST = True
74 while QUESTION_REQUEST == True:
75 QUESTION_REQUEST = input
76 ("do you want to ask a question") == 'yes'
77 if QUESTION_REQUEST == True:
78 USER_QUESTION = input()
79 response = lama.chat(
80 model="llama3.2",
81 messages=[
82 {
83 "role": "user",
84 "content": USER_QUESTION,
85 },
86 ],
87 )
88 print(response["messages"]["content"])
89 else:
90 break
91
92
93
94OPTIONS = {
95 "LAMA": ollama(),
96 "CPU_INFO": display_cpu_info(),
97 "UPTIME": display_uptime(),
98 "SPEECH_TRANSCRIBER": recognize_speech(),
99 "NOTES": save_notes(),
100}
101
102
103def main():
104 lcd.clear()
105 print("WELCOME TO THE I2C COMMAND LINE CENTER")
106 print("Options:", ", ".join(OPTIONS.keys()))
107
108 while True:
109 user_input = input("Enter command: ").upper()
110 action = OPTIONS.get(user_input)
111
112 if action:
113 action()
114 else:
115 lcd.display_text(ERROR_NOT_FOUND, 1)
116 print(ERROR_NOT_FOUND)
117
118
119def destroy():
120 lcd.clear()
121 os.system("cls" if os.name == "nt" else "clear")
122
123
124if __name__ == "__main__":
125 try:
126 main()
127 except KeyboardInterrupt:
128 destroy()