|
| 1 | +import machine |
| 2 | +from lib.i2c_lcd import I2cLcd |
| 3 | + |
| 4 | + |
| 5 | +class Symbol: |
| 6 | + def __init__(self, index: int, data: bytearray): |
| 7 | + self.index = index |
| 8 | + self.data = data |
| 9 | + |
| 10 | + |
| 11 | +class Symbols: |
| 12 | + TICK = Symbol(0, bytearray([0x00, 0x00, 0x01, 0x02, 0x14, 0x08, 0x00, 0x00])) |
| 13 | + CROSS = Symbol(1, bytearray([0x00, 0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00])) |
| 14 | + |
| 15 | + |
| 16 | +class DisplayInterface: |
| 17 | + def __init__(self, sda_pin: int, scl_pin: int): |
| 18 | + self.__i2c = machine.I2C(0, sda=machine.Pin(sda_pin), scl=machine.Pin(scl_pin), freq=400000) |
| 19 | + self.__lcd = I2cLcd(self.__i2c, 0x27, 2, 16) |
| 20 | + self.__init_symbols() |
| 21 | + |
| 22 | + def print(self, text: str, line: int = 1, position: int = 1, clear_line: bool = False, clear_full: bool = False): |
| 23 | + self.__validate_inputs(line, position) |
| 24 | + text = self.__trim_text(text) |
| 25 | + |
| 26 | + if clear_full: |
| 27 | + self.clear() |
| 28 | + |
| 29 | + if clear_line: |
| 30 | + self.clear(line) |
| 31 | + |
| 32 | + self.__lcd.move_to(position - 1, line - 1) |
| 33 | + self.__lcd.putstr(text) |
| 34 | + |
| 35 | + def print_centered(self, text: str, line: int = 1): |
| 36 | + self.__validate_inputs(line) |
| 37 | + text = self.__trim_text(text) |
| 38 | + |
| 39 | + self.__lcd.move_to(0, line - 1) |
| 40 | + self.__lcd.putstr(text.center(16)) |
| 41 | + |
| 42 | + def clear(self, line: int = None): |
| 43 | + self.__validate_inputs(line) |
| 44 | + |
| 45 | + if line: |
| 46 | + self.__lcd.move_to(0, line - 1) |
| 47 | + self.__lcd.putstr(" " * 16) |
| 48 | + else: |
| 49 | + self.__lcd.clear() |
| 50 | + |
| 51 | + def __init_symbols(self): |
| 52 | + for symbol in Symbols.__dict__.values(): |
| 53 | + if isinstance(symbol, Symbol): |
| 54 | + self.__lcd.custom_char(symbol.index, symbol.data) |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def __validate_inputs(line: int = None, position: int = None): |
| 58 | + if line and line not in [1, 2]: |
| 59 | + raise ValueError("Line must be 1 or 2") |
| 60 | + |
| 61 | + if position and position not in range(1, 16): |
| 62 | + raise ValueError("Position must be between 1 and 16") |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def __trim_text(text: str): |
| 66 | + if len(text) > 16: |
| 67 | + text = text[:16] |
| 68 | + |
| 69 | + return text |
0 commit comments