Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/display.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

import machine
from lib.i2c_lcd import I2cLcd

Expand All @@ -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()
Expand All @@ -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):
Expand Down
24 changes: 20 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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)