summaryrefslogtreecommitdiff
path: root/bin/kasper_source.py
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2024-11-14 19:31:03 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2024-11-14 19:31:03 +0100
commitfe441f69a4632e5245588574923ef8dc467eced5 (patch)
treec9975c3b706a4c810ba975c2884c748d7255c770 /bin/kasper_source.py
parent9ebcbfc83a64b8a27d0725dbd12f6a8fdb042934 (diff)
tried implementing ollama
Diffstat (limited to 'bin/kasper_source.py')
-rw-r--r--bin/kasper_source.py154
1 files changed, 0 insertions, 154 deletions
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()