[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
This commit is contained in:
		| @@ -20,18 +20,33 @@ import time | |||||||
|  |  | ||||||
|  |  | ||||||
| class WelcomeScreen: | class WelcomeScreen: | ||||||
|     def __init__(self, lcd): |     def __init__(self, lcd, subtitle="Lorem ipsum.", starting_msg="Starting...", started_msg="Started!"): | ||||||
|         self.lcd = lcd |         self.lcd = lcd | ||||||
|         self.columns = self.lcd.num_columns |         self.columns = self.lcd.num_columns | ||||||
|         self.lines = self.lcd.num_lines |         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 |         if cycles < 1: cycles = 1 | ||||||
|         padding = " " * self.columns  # as much spaces as padding as one display line is long |         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 |         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 |         # mechanism for centering on a 4*20 display | ||||||
|         y_offset = 0 |         y_offset = 0 | ||||||
|         if self.lines == 4: |         if self.lines == 4: | ||||||
| @@ -43,9 +58,9 @@ class WelcomeScreen: | |||||||
|             self.lcd.putstr(padding_hyphen) |             self.lcd.putstr(padding_hyphen) | ||||||
|          |          | ||||||
|         for i in range(cycles): |         for i in range(cycles): | ||||||
|             line1 = padding + starting_str + padding |             line1 = padding + self.starting_msg + padding | ||||||
|             line2 = "The Program!".center(self.columns) |             line2 = self.subtitle.center(self.columns) | ||||||
|             for i in range(self.columns + len(starting_str)): |             for i in range(self.columns + len(self.starting_msg)): | ||||||
|                 self.lcd.move_to(0,y_offset) |                 self.lcd.move_to(0,y_offset) | ||||||
|                 self.lcd.putstr(line1[0:self.columns]) |                 self.lcd.putstr(line1[0:self.columns]) | ||||||
|                 self.lcd.move_to(0,y_offset+1) |                 self.lcd.move_to(0,y_offset+1) | ||||||
| @@ -53,17 +68,28 @@ class WelcomeScreen: | |||||||
|                 line1 = line1[1:] |                 line1 = line1[1:] | ||||||
|          |          | ||||||
|         self.lcd.move_to(0,y_offset) |         self.lcd.move_to(0,y_offset) | ||||||
|         self.lcd.putstr(started_str) |         self.lcd.putstr(self.started_msg.center(16)) | ||||||
|              |          | ||||||
|         # now fade down |         time.sleep(wait_after_cycles) | ||||||
|         if self.lines == 4: |          | ||||||
|             current_display = padding_hyphen + started_str + "The Program!".center(self.columns) + padding_hyphen |         # now fade down if enabled via the params | ||||||
|         else: |         if fade_down["enabled"]: | ||||||
|             current_display = started_str + "The Program!".center(self.columns) |             # get all the waiting times | ||||||
|         time.sleep(1) |             if "wait_between" in fade_down.keys(): | ||||||
|         self.lcd.move_to(0,y_offset) |                 wait_between = fade_down["wait_between"] | ||||||
|         self.lcd.putstr(padding + started_str) |             else: | ||||||
|         time.sleep(0.1) |                 wait_between = 0.1 | ||||||
|         self.lcd.clear() |             if "wait_after" in fade_down.keys(): | ||||||
|         time.sleep(0.3) |                 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) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user