summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2024-11-07 22:25:39 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2024-11-07 22:25:39 +0100
commite458ed522f45a41cdcef32acc8aee5c340fba504 (patch)
tree49d4313569b810ad39dee0b60dde6cea2cac5c54
parentd069e34d555a39117aa1b7911776696ffec07092 (diff)
FIXED THE FEW BUGS I THINK
-rw-r--r--bin/kasper_source.py98
1 files changed, 52 insertions, 46 deletions
diff --git a/bin/kasper_source.py b/bin/kasper_source.py
index 4ff5e70..50fb48c 100644
--- a/bin/kasper_source.py
+++ b/bin/kasper_source.py
@@ -1,30 +1,23 @@
1from smbus2 import SMBus 1from smbus2 import SMBus
2from time import sleep 2from time import sleep
3from gpiozero import CPUTemperature 3from gpiozero import CPUTemperature
4
5import speech_recognition as sr 4import speech_recognition as sr
6import sounddevice 5import time
7import os 6import os
8 7
9ALIGN_FUNC = { 8ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"}
10 'left': 'ljust',
11 'right': 'rjust',
12 'center': 'center'}
13CLEAR_DISPLAY = 0x01 9CLEAR_DISPLAY = 0x01
14ENABLE_BIT = 0b00000100 10ENABLE_BIT = 0b00000100
15LINES = { 11LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4}
16 1: 0x80,
17 2: 0xC0,
18 3: 0x94,
19 4: 0xD4}
20 12
21LCD_BACKLIGHT = 0x08 13LCD_BACKLIGHT = 0x08
22LCD_NOBACKLIGHT = 0x00 14LCD_NOBACKLIGHT = 0x00
23 15
24ERROR_BAD_REQUEST = '400' 16ERROR_BAD_REQUEST = "400"
25ERROR_UNAUTHORIZED = '401' 17ERROR_UNAUTHORIZED = "401"
26ERROR_NOT_FOUND = '404' 18ERROR_NOT_FOUND = "404"
27ERROR_TIMEOUT = '408' 19ERROR_TIMEOUT = "408"
20
28 21
29class LCD(object): 22class LCD(object):
30 23
@@ -48,17 +41,17 @@ class LCD(object):
48 self.bus.write_byte(self.address, byte) 41 self.bus.write_byte(self.address, byte)
49 self.bus.write_byte(self.address, (byte | ENABLE_BIT)) 42 self.bus.write_byte(self.address, (byte | ENABLE_BIT))
50 sleep(self.delay) 43 sleep(self.delay)
51 self.bus.write_byte(self.address,(byte & ~ENABLE_BIT)) 44 self.bus.write_byte(self.address, (byte & ~ENABLE_BIT))
52 sleep(self.delay) 45 sleep(self.delay)
53 46
54 def write(self, byte, mode=0): 47 def write(self, byte, mode=0):
55 backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT 48 backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT
56 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) 49 self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode)
57 50
58 def text(self, text, line, align='left'): 51 def text(self, text, line, align="left"):
59 self.write(LINES.get(line, LINES[1])) 52 self.write(LINES.get(line, LINES[1]))
60 text, other_lines = self.get_text_line(text) 53 text, other_lines = self.get_text_line(text)
61 text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width) 54 text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width)
62 for char in text: 55 for char in text:
63 self.write(ord(char), mode=1) 56 self.write(ord(char), mode=1)
64 if other_lines and line <= self.rows - 1: 57 if other_lines and line <= self.rows - 1:
@@ -71,7 +64,7 @@ class LCD(object):
71 def get_text_line(self, text): 64 def get_text_line(self, text):
72 line_break = self.width 65 line_break = self.width
73 if len(text) > self.width: 66 if len(text) > self.width:
74 line_break = text[:self.width + 1].rfind(' ') 67 line_break = text[: self.width + 1].rfind(" ")
75 if line_break < 0: 68 if line_break < 0:
76 line_break = self.width 69 line_break = self.width
77 return text[:line_break], text[line_break:].strip() 70 return text[:line_break], text[line_break:].strip()
@@ -79,30 +72,34 @@ class LCD(object):
79 def clear(self): 72 def clear(self):
80 self.write(CLEAR_DISPLAY) 73 self.write(CLEAR_DISPLAY)
81 74
75
82LCD_DISPLAY = LCD() 76LCD_DISPLAY = LCD()
83VOICE_REC = sr.Recognizer() 77VOICE_REC = sr.Recognizer()
84MIC = sr.Microphone() 78MIC = sr.Microphone()
85PROCES_LOAD = os.getloadavg() 79PROCES_LOAD = os.getloadavg()
86TIME = current_time.time() 80TIME = time.localtime()
81CURRENT_TIME = time.strftime("%H:%M:%S", TIME)
87UPTIME = time.CLOCK_UPTIME() 82UPTIME = time.CLOCK_UPTIME()
88CPU_TEMP = CPUTemperature() 83CPU_TEMP = CPUTemperature()
89 84
90# clearing the lcd from any text that was on it before the program started to ensure smooth operations 85# clearing the lcd from any text that was on it before the program started to ensure smooth operations
91lcd.clear() 86LCD_DISPLAY.clear()
87
92 88
93# Listening to the user's voice and putting it into a variable 89# Listening to the user's voice and putting it into a variable
94def listen_voice(): 90def listen_voice():
95 global audio 91 global audio
96 with mic as source: 92 with MIC as source:
97 r.adjust_for_ambient_noise(source) 93 VOICE_REC.adjust_for_ambient_noise(source)
98 audio = r.listen(source) 94 audio = VOICE_REC.listen(source)
99 return audio 95 return audio
100 96
97
101# Transcribing the audio to text and printing it out 98# Transcribing the audio to text and printing it out
102# Using the Google Speech Recognizer 99# Using the Google Speech Recognizer
103def recognize_speech(audio): 100def recognize_speech(audio):
104 try: 101 try:
105 words = r.recognize_google(audio) 102 words = VOICE_REC.recognize_google(audio)
106 LCD_DISPLAY.text(words, 1) 103 LCD_DISPLAY.text(words, 1)
107 print(f"Printing on screen: {words}") 104 print(f"Printing on screen: {words}")
108 except sr.UnknownValueError: 105 except sr.UnknownValueError:
@@ -110,40 +107,48 @@ def recognize_speech(audio):
110 print(ERROR_BAD_REQUEST) 107 print(ERROR_BAD_REQUEST)
111 except sr.RequestError: 108 except sr.RequestError:
112 LCD_DISPLAY.text(ERROR_UNAUTHORIZED, 1) 109 LCD_DISPLAY.text(ERROR_UNAUTHORIZED, 1)
113 print(ERROR_UNAUTHORIZED) 110 print(ERROR_UNAUTHORIZED)
111
114 112
115def CPU_INFO(): 113def CPU_INFO():
116 print("you chose to display the cpou") 114 print("you chose to display the cpou")
117 while (True):
118 LCD.text(PROCES_LOAD(),1,left)
119
120def CURRENT_TIME():
121 while True: 115 while True:
122 backlight_mode = True 116 LCD.text(
123 LCD.text(TIME,2,center) 117 PROCES_LOAD(),
118 1,
119 )
124 120
125def UPTIME():
126 while True:
127 LCD.text(UPTIME,1,left)
128
129def CPU_TEMP():
130 while True:
131 LCD.text(cpu.temperature)
132 121
133def CPU_LOAD(): 122def CPU_LOAD():
134 backlight_mode = True 123 LCD.backlight_mode = True
135 LCD.text(PROCES_LOAD,1,left) 124 LCD.text(
125 PROCES_LOAD,
126 1,
127 )
128
136 129
137def NOTES(): 130def NOTES():
138 count = 0 131 count = 0
139 user_notes = input() 132 user_notes = input()
140 for i in user_notes: 133 for i in user_notes:
141 while count < 20: 134 while count < 20:
142 lcd.text(i,1,left) 135 LCD_DISPLAY.text(
136 i,
137 1,
138 )
143 count += 1 139 count += 1
144 140
145 141
146OPTIONS = ["CPU_CLOCK", "TIME", "UPTIME", "CPU_TEMP", "CPU_LOAD", "NOTES", "SPEECH_TRANSCRIBER"] 142OPTIONS = [
143 "CPU_CLOCK",
144 "TIME",
145 "UPTIME",
146 "CPU_TEMP",
147 "CPU_LOAD",
148 "NOTES",
149 "SPEECH_TRANSCRIBER",
150]
151
147 152
148def PROGRAM(USER_INPUT): 153def PROGRAM(USER_INPUT):
149 print("WELCOME TO THE I2C COMMAND LINE CENTER \n WHAT DO YOU WISH TO DO? ") 154 print("WELCOME TO THE I2C COMMAND LINE CENTER \n WHAT DO YOU WISH TO DO? ")
@@ -159,4 +164,5 @@ def PROGRAM(USER_INPUT):
159 else: 164 else:
160 print(ERROR_NOT_FOUND) 165 print(ERROR_NOT_FOUND)
161 166
162PROGRAM() \ No newline at end of file 167
168PROGRAM()