diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-07 22:25:39 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-07 22:25:39 +0100 |
| commit | e458ed522f45a41cdcef32acc8aee5c340fba504 (patch) | |
| tree | 49d4313569b810ad39dee0b60dde6cea2cac5c54 | |
| parent | d069e34d555a39117aa1b7911776696ffec07092 (diff) | |
FIXED THE FEW BUGS I THINK
| -rw-r--r-- | bin/kasper_source.py | 98 |
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 @@ | |||
| 1 | from smbus2 import SMBus | 1 | from smbus2 import SMBus |
| 2 | from time import sleep | 2 | from time import sleep |
| 3 | from gpiozero import CPUTemperature | 3 | from gpiozero import CPUTemperature |
| 4 | |||
| 5 | import speech_recognition as sr | 4 | import speech_recognition as sr |
| 6 | import sounddevice | 5 | import time |
| 7 | import os | 6 | import os |
| 8 | 7 | ||
| 9 | ALIGN_FUNC = { | 8 | ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} |
| 10 | 'left': 'ljust', | ||
| 11 | 'right': 'rjust', | ||
| 12 | 'center': 'center'} | ||
| 13 | CLEAR_DISPLAY = 0x01 | 9 | CLEAR_DISPLAY = 0x01 |
| 14 | ENABLE_BIT = 0b00000100 | 10 | ENABLE_BIT = 0b00000100 |
| 15 | LINES = { | 11 | LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} |
| 16 | 1: 0x80, | ||
| 17 | 2: 0xC0, | ||
| 18 | 3: 0x94, | ||
| 19 | 4: 0xD4} | ||
| 20 | 12 | ||
| 21 | LCD_BACKLIGHT = 0x08 | 13 | LCD_BACKLIGHT = 0x08 |
| 22 | LCD_NOBACKLIGHT = 0x00 | 14 | LCD_NOBACKLIGHT = 0x00 |
| 23 | 15 | ||
| 24 | ERROR_BAD_REQUEST = '400' | 16 | ERROR_BAD_REQUEST = "400" |
| 25 | ERROR_UNAUTHORIZED = '401' | 17 | ERROR_UNAUTHORIZED = "401" |
| 26 | ERROR_NOT_FOUND = '404' | 18 | ERROR_NOT_FOUND = "404" |
| 27 | ERROR_TIMEOUT = '408' | 19 | ERROR_TIMEOUT = "408" |
| 20 | |||
| 28 | 21 | ||
| 29 | class LCD(object): | 22 | class 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 | |||
| 82 | LCD_DISPLAY = LCD() | 76 | LCD_DISPLAY = LCD() |
| 83 | VOICE_REC = sr.Recognizer() | 77 | VOICE_REC = sr.Recognizer() |
| 84 | MIC = sr.Microphone() | 78 | MIC = sr.Microphone() |
| 85 | PROCES_LOAD = os.getloadavg() | 79 | PROCES_LOAD = os.getloadavg() |
| 86 | TIME = current_time.time() | 80 | TIME = time.localtime() |
| 81 | CURRENT_TIME = time.strftime("%H:%M:%S", TIME) | ||
| 87 | UPTIME = time.CLOCK_UPTIME() | 82 | UPTIME = time.CLOCK_UPTIME() |
| 88 | CPU_TEMP = CPUTemperature() | 83 | CPU_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 |
| 91 | lcd.clear() | 86 | LCD_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 |
| 94 | def listen_voice(): | 90 | def 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 |
| 103 | def recognize_speech(audio): | 100 | def 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 | ||
| 115 | def CPU_INFO(): | 113 | def 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 | |||
| 120 | def 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 | ||
| 125 | def UPTIME(): | ||
| 126 | while True: | ||
| 127 | LCD.text(UPTIME,1,left) | ||
| 128 | |||
| 129 | def CPU_TEMP(): | ||
| 130 | while True: | ||
| 131 | LCD.text(cpu.temperature) | ||
| 132 | 121 | ||
| 133 | def CPU_LOAD(): | 122 | def 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 | ||
| 137 | def NOTES(): | 130 | def 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 | ||
| 146 | OPTIONS = ["CPU_CLOCK", "TIME", "UPTIME", "CPU_TEMP", "CPU_LOAD", "NOTES", "SPEECH_TRANSCRIBER"] | 142 | OPTIONS = [ |
| 143 | "CPU_CLOCK", | ||
| 144 | "TIME", | ||
| 145 | "UPTIME", | ||
| 146 | "CPU_TEMP", | ||
| 147 | "CPU_LOAD", | ||
| 148 | "NOTES", | ||
| 149 | "SPEECH_TRANSCRIBER", | ||
| 150 | ] | ||
| 151 | |||
| 147 | 152 | ||
| 148 | def PROGRAM(USER_INPUT): | 153 | def 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 | ||
| 162 | PROGRAM() \ No newline at end of file | 167 | |
| 168 | PROGRAM() | ||
