diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-05 18:40:27 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2024-11-05 18:40:27 +0100 |
| commit | fa7007d00c04fc73f668e2b7f339c3f60a64d055 (patch) | |
| tree | 1c054d4f10759201134d496dbe170e2cfe49bfe5 /bin | |
| parent | d7b8b6ae21c07fe52e691e9090c24de0d09a401f (diff) | |
did alot :)
Diffstat (limited to 'bin')
| -rw-r--r-- | bin/script.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/bin/script.py b/bin/script.py new file mode 100644 index 0000000..5c33638 --- /dev/null +++ b/bin/script.py | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | from smbus2 import SMBus | ||
| 2 | from time import sleep | ||
| 3 | |||
| 4 | import speech_recognition as sr | ||
| 5 | import sounddevice | ||
| 6 | import os | ||
| 7 | |||
| 8 | ALIGN_FUNC = { | ||
| 9 | 'left': 'ljust', | ||
| 10 | 'right': 'rjust', | ||
| 11 | 'center': 'center'} | ||
| 12 | CLEAR_DISPLAY = 0x01 | ||
| 13 | ENABLE_BIT = 0b00000100 | ||
| 14 | LINES = { | ||
| 15 | 1: 0x80, | ||
| 16 | 2: 0xC0, | ||
| 17 | 3: 0x94, | ||
| 18 | 4: 0xD4} | ||
| 19 | |||
| 20 | LCD_BACKLIGHT = 0x08 | ||
| 21 | LCD_NOBACKLIGHT = 0x00 | ||
| 22 | |||
| 23 | ERROR_BAD_REQUEST = '400' | ||
| 24 | ERROR_UNAUTHORIZED = '401' | ||
| 25 | ERROR_NOT_FOUND = '404' | ||
| 26 | ERROR_TIMEOUT = '408' | ||
| 27 | |||
| 28 | class LCD(object): | ||
| 29 | |||
| 30 | def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): | ||
| 31 | self.address = address | ||
| 32 | self.bus = SMBus(bus) | ||
| 33 | self.delay = 0.0005 | ||
| 34 | self.rows = rows | ||
| 35 | self.width = width | ||
| 36 | self.backlight_status = backlight | ||
| 37 | |||
| 38 | self.write(0x33) | ||
| 39 | self.write(0x32) | ||
| 40 | self.write(0x06) | ||
| 41 | self.write(0x0C) | ||
| 42 | self.write(0x28) | ||
| 43 | self.write(CLEAR_DISPLAY) | ||
| 44 | sleep(self.delay) | ||
| 45 | |||
| 46 | def _write_byte(self, byte): | ||
| 47 | self.bus.write_byte(self.address, byte) | ||
| 48 | self.bus.write_byte(self.address, (byte | ENABLE_BIT)) | ||
| 49 | sleep(self.delay) | ||
| 50 | self.bus.write_byte(self.address,(byte & ~ENABLE_BIT)) | ||
| 51 | sleep(self.delay) | ||
| 52 | |||
| 53 | def write(self, byte, mode=0): | ||
| 54 | backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT | ||
| 55 | self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) | ||
| 56 | |||
| 57 | def text(self, text, line, align='left'): | ||
| 58 | self.write(LINES.get(line, LINES[1])) | ||
| 59 | text, other_lines = self.get_text_line(text) | ||
| 60 | text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width) | ||
| 61 | for char in text: | ||
| 62 | self.write(ord(char), mode=1) | ||
| 63 | if other_lines and line <= self.rows - 1: | ||
| 64 | self.text(other_lines, line + 1, align=align) | ||
| 65 | |||
| 66 | def backlight(self, turn_on=True): | ||
| 67 | self.backlight_status = turn_on | ||
| 68 | self.write(0) | ||
| 69 | |||
| 70 | def get_text_line(self, text): | ||
| 71 | line_break = self.width | ||
| 72 | if len(text) > self.width: | ||
| 73 | line_break = text[:self.width + 1].rfind(' ') | ||
| 74 | if line_break < 0: | ||
| 75 | line_break = self.width | ||
| 76 | return text[:line_break], text[line_break:].strip() | ||
| 77 | |||
| 78 | def clear(self): | ||
| 79 | self.write(CLEAR_DISPLAY) | ||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | LCD = 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 | |||
| 90 | |||
| 91 | |||
| 92 | def CPU_INFO(): | ||
| 93 | while (True): | ||
| 94 | LCD.text(PROCES_LOAD(),1,left) | ||
| 95 | |||
| 96 | def CURRENT_TIME(): | ||
| 97 | while True: | ||
| 98 | backlight_mode = true | ||
| 99 | LCD.text(UPTIME,1,left) | ||
| 100 | LCD.text(TIME,2,center) | ||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | OPTIONS = ["CPU_CLOCK", "TIME", "UPTIME", "CPU_TEMP", "CPU LOAD", "NOTES"] | ||
| 106 | |||
| 107 | def USER_GUI(): | ||
| 108 | print("WELCOME TO THE I2C COMMAND LINE CENTER \n WHAT DO YOU WISH TO DO? ") | ||
| 109 | print(OPTIONS) | ||
