diff options
| author | nasrlol <nsrddyn@gmail.com> | 2024-11-13 00:30:41 +0100 |
|---|---|---|
| committer | nasrlol <nsrddyn@gmail.com> | 2024-11-13 00:30:41 +0100 |
| commit | ecd194028a652cc29f300e4a6ca24675b5b8d9ba (patch) | |
| tree | 4732c5edc5d8f30f5bacc6823298e4365cebc1fb | |
| parent | 0a7689fbf896aa3629f9783c4316a4b453928b6f (diff) | |
fixed the speechtranscriber bug
| -rw-r--r-- | bin/kasper_source.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/bin/kasper_source.py b/bin/kasper_source.py index bd61942..d878f6c 100644 --- a/bin/kasper_source.py +++ b/bin/kasper_source.py | |||
| @@ -19,6 +19,7 @@ ERROR_TIMEOUT = "408 Request Timeout" | |||
| 19 | 19 | ||
| 20 | # LCD Control Class | 20 | # LCD Control Class |
| 21 | class LCD: | 21 | class LCD: |
| 22 | |||
| 22 | def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): | 23 | def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): |
| 23 | self.address = address | 24 | self.address = address |
| 24 | self.bus = SMBus(bus) | 25 | self.bus = SMBus(bus) |
| @@ -26,14 +27,17 @@ class LCD: | |||
| 26 | self.rows = rows | 27 | self.rows = rows |
| 27 | self.backlight_status = backlight | 28 | self.backlight_status = backlight |
| 28 | self.delay = 0.0005 | 29 | self.delay = 0.0005 |
| 30 | |||
| 29 | # LCD Initialization | 31 | # LCD Initialization |
| 30 | for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): | 32 | for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): |
| 31 | self.write(cmd) | 33 | self.write(cmd) |
| 32 | time.sleep(self.delay) | 34 | time.sleep(self.delay) |
| 35 | |||
| 33 | def write(self, byte, mode=0): | 36 | def write(self, byte, mode=0): |
| 34 | """Send a command or character to the LCD.""" | 37 | """Send a command or character to the LCD.""" |
| 35 | backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT | 38 | backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT |
| 36 | self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) | 39 | self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) |
| 40 | |||
| 37 | def _write_byte(self, byte): | 41 | def _write_byte(self, byte): |
| 38 | """Write a byte to the I2C bus.""" | 42 | """Write a byte to the I2C bus.""" |
| 39 | self.bus.write_byte(self.address, byte) | 43 | self.bus.write_byte(self.address, byte) |
| @@ -41,15 +45,18 @@ class LCD: | |||
| 41 | time.sleep(self.delay) | 45 | time.sleep(self.delay) |
| 42 | self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) | 46 | self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) |
| 43 | time.sleep(self.delay) | 47 | time.sleep(self.delay) |
| 48 | |||
| 44 | def display_text(self, text, line=1, align="left"): | 49 | def display_text(self, text, line=1, align="left"): |
| 45 | """Display text on a specified line with alignment.""" | 50 | """Display text on a specified line with alignment.""" |
| 46 | self.write(LINES.get(line, LINES[1])) | 51 | self.write(LINES.get(line, LINES[1])) |
| 47 | aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) | 52 | aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) |
| 48 | for char in aligned_text: | 53 | for char in aligned_text: |
| 49 | self.write(ord(char), mode=1) | 54 | self.write(ord(char), mode=1) |
| 55 | |||
| 50 | def clear(self): | 56 | def clear(self): |
| 51 | """Clear the display.""" | 57 | """Clear the display.""" |
| 52 | self.write(0x01) | 58 | self.write(0x01) |
| 59 | |||
| 53 | def set_backlight(self, turn_on=True): | 60 | def set_backlight(self, turn_on=True): |
| 54 | """Toggle backlight on or off.""" | 61 | """Toggle backlight on or off.""" |
| 55 | self.backlight_status = turn_on | 62 | self.backlight_status = turn_on |
| @@ -97,7 +104,7 @@ def recognize_speech(): | |||
| 97 | except speech.UnknownValueError: | 104 | except speech.UnknownValueError: |
| 98 | lcd.display_text(ERROR_BAD_REQUEST, line=1) | 105 | lcd.display_text(ERROR_BAD_REQUEST, line=1) |
| 99 | print(ERROR_BAD_REQUEST) | 106 | print(ERROR_BAD_REQUEST) |
| 100 | except s.RequestError: | 107 | except speech.RequestError: |
| 101 | lcd.display_text(ERROR_UNAUTHORIZED, line=1) | 108 | lcd.display_text(ERROR_UNAUTHORIZED, line=1) |
| 102 | print(ERROR_UNAUTHORIZED) | 109 | print(ERROR_UNAUTHORIZED) |
| 103 | 110 | ||
