diff --git a/src/display.py b/src/display.py index 4b03a20..4058e58 100644 --- a/src/display.py +++ b/src/display.py @@ -1,3 +1,5 @@ +import time + import machine from lib.i2c_lcd import I2cLcd @@ -20,9 +22,10 @@ def __init__(self, sda_pin: int, scl_pin: int): self.__lcd = I2cLcd(self.__i2c, 0x27, 2, 16) self.__init_symbols() - def print(self, text: str, line: int = 1, position: int = 1, clear_line: bool = False, clear_full: bool = False): + def print(self, text: str, line: int = 1, position: int = 1, clear_line: bool = False, clear_full: bool = False, trim: bool = True): self.__validate_inputs(line, position) - text = self.__trim_text(text) + if trim: + text = self.__trim_text(text) if clear_full: self.clear() @@ -49,6 +52,17 @@ def clear(self, line: int = None): else: self.__lcd.clear() + def print_scroll_text(self, text: str, line: int = 1, delay: float = 0): + self.__validate_inputs(line) + if len(text) <= 16: + self.print(text, line) + return + + padded_text = text + " " * 16 + for start in range(len(padded_text) - 15): + self.print(padded_text[start:start + 16], line, clear_line=True) + time.sleep(delay) + def __init_symbols(self): for symbol in Symbols.__dict__.values(): if isinstance(symbol, Symbol): diff --git a/src/main.py b/src/main.py index ff06158..9966c45 100644 --- a/src/main.py +++ b/src/main.py @@ -32,6 +32,16 @@ def __init__(self): self.__last_rfid_successful_read_time = 0 self.__rfid_read_delay = Config.RFID.READ_DELAY + def handle_error(self, exception: Exception) -> None: + self.__display.clear() + self.__display.print_centered("Exception", line=1) + self.__display.print_centered("occurred", line=2) + self.__buzzer.play_failure() + + time.sleep(2) + + self.__display.print_scroll_text(f"{type(exception).__name__}:{str(exception)}", line=2) + def run(self) -> None: self.__display.print_centered(f"{chr(Symbols.NOTE.index)} Music Box {chr(Symbols.NOTE.index)}", line=1) self.__display.print_centered("Initializing...", line=2) @@ -41,7 +51,7 @@ def run(self) -> None: return self.__display.print("Devices:", line=1, clear_full=True) - self.__display_current_device() + self.__display_current_device(reset=True) while True: self.__check_buttons_press() @@ -67,9 +77,9 @@ def __check_api_health(self) -> bool: self.__buzzer.play_success() return True - def __display_current_device(self) -> None: + def __display_current_device(self, reset: bool = False) -> None: self.__display.print("Loading...", line=2, clear_line=True) - current_device_data = self.__api.get_current_device(reset=True) + current_device_data = self.__api.get_current_device(reset=reset) self.__display_device(current_device_data) def __display_device(self, device_data: dict) -> None: @@ -93,6 +103,8 @@ def __check_buttons_press(self) -> None: current_device_data = self.__api.previous_device() elif was_right_button_pressed: current_device_data = self.__api.next_device() + else: + return self.__display_device(current_device_data) self.__buttons.reset() @@ -125,4 +137,8 @@ def __check_rfid_read(self) -> None: if __name__ == "__main__": app = App() - app.run() + + try: + app.run() + except Exception as e: + app.handle_error(e)