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