From cc29f158b45ee7f51e5d1427e289d6287a25d7f4 Mon Sep 17 00:00:00 2001 From: BlueFox Date: Mon, 28 Oct 2024 16:42:37 +0000 Subject: [PATCH] [WelcomeScreen.py] Made the libarary more adaptable (added parameter, mostly optional) Added support for: - changing the string cycling through (e.g. "Starting") - changing the string after the cycling is finished (e.g. "Started") - changing the string shown below the cycling text (e.g., the devices name, ...) - enabling/disabling fade out in the end - changing the timing of the fade out animation - changing the timing of how long to wait after the cycling animation --- WelcomeScreen.py | 68 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/WelcomeScreen.py b/WelcomeScreen.py index 3c7382a..52bf389 100644 --- a/WelcomeScreen.py +++ b/WelcomeScreen.py @@ -20,18 +20,33 @@ import time class WelcomeScreen: - def __init__(self, lcd): + def __init__(self, lcd, subtitle="Lorem ipsum.", starting_msg="Starting...", started_msg="Started!"): self.lcd = lcd self.columns = self.lcd.num_columns self.lines = self.lcd.num_lines + self.subtitle = subtitle + self.starting_msg = starting_msg + self.started_msg = started_msg - def show(self, cycles=1): # loops says how often the Starting text goes through + # show() - Display the actual message + # --- + # cycles says how often the Starting text goes through + # --- + # wait_after_cycles is an integer number defining how long to wait before returning / if fading + # out is activated, the time to wait between end of the cycling animation and the fade out animation + # --- + # fade_down is a dict with following keys: + # - "enabled" - REQUIRED - wether fading is enabled (default: true) + # - "wait_between" - OPTIONAL - the time to wait between each line fade + # - "wait_after" - OPTIONAL - the time to wait after the fade out + # --- + # interruptable influences wether the program can be interrupted by pins in the self.interrupt_pins list + # if this list is empty, even when interruptable is set to true, nothing will be able to interrupt! + def show(self, cycles=1, wait_after_cycles=1, fade_down={"enabled": True}, interruptable=True): if cycles < 1: cycles = 1 padding = " " * self.columns # as much spaces as padding as one display line is long padding_hyphen = "-" * self.columns # as much hyphens as padding as one display line is long - starting_str = "Starting..." - started_str = "Started!".center(self.columns) - + # mechanism for centering on a 4*20 display y_offset = 0 if self.lines == 4: @@ -43,9 +58,9 @@ class WelcomeScreen: self.lcd.putstr(padding_hyphen) for i in range(cycles): - line1 = padding + starting_str + padding - line2 = "The Program!".center(self.columns) - for i in range(self.columns + len(starting_str)): + line1 = padding + self.starting_msg + padding + line2 = self.subtitle.center(self.columns) + for i in range(self.columns + len(self.starting_msg)): self.lcd.move_to(0,y_offset) self.lcd.putstr(line1[0:self.columns]) self.lcd.move_to(0,y_offset+1) @@ -53,17 +68,28 @@ class WelcomeScreen: line1 = line1[1:] self.lcd.move_to(0,y_offset) - self.lcd.putstr(started_str) - - # now fade down - if self.lines == 4: - current_display = padding_hyphen + started_str + "The Program!".center(self.columns) + padding_hyphen - else: - current_display = started_str + "The Program!".center(self.columns) - time.sleep(1) - self.lcd.move_to(0,y_offset) - self.lcd.putstr(padding + started_str) - time.sleep(0.1) - self.lcd.clear() - time.sleep(0.3) + self.lcd.putstr(self.started_msg.center(16)) + + time.sleep(wait_after_cycles) + + # now fade down if enabled via the params + if fade_down["enabled"]: + # get all the waiting times + if "wait_between" in fade_down.keys(): + wait_between = fade_down["wait_between"] + else: + wait_between = 0.1 + if "wait_after" in fade_down.keys(): + wait_after = fade_down["wait_after"] + else: + wait_after = 0.3 + #if self.lines == 4: + # current_display = padding_hyphen + self.started_msg.center(16) + self.subtitle.center(self.columns) + padding_hyphen + #else: + # current_display = self.started_msg.center(16) + self.subtitle.center(self.columns) + self.lcd.move_to(0,y_offset) + self.lcd.putstr(padding + self.started_msg.center(16)) + time.sleep(wait_between) + self.lcd.clear() + time.sleep(wait_after)