from machine import I2C, Pin, Timer import time class CounterScreen: def __init__(self, lcd, counterArray): self.lcd = lcd self.lcd.clear() self.counterArray = counterArray self._zfill = lambda s, length: '{:0>{w}}'.format(s, w=length) self.last_line1 = "" self.last_line2 = "" self.too_many_counters_warning_shown = False def _too_many_counters_warning(self): # Function: # show a short message saying that not more that 5 counters can be displayed. # WARNING: # This is a INTERNAL class function, don't call from outside! # Stops program flow for approx. 15s self.lcd.move_to(0,0) self.lcd.putstr(" WARNING! ") message = "Can't display more than 5 counters. Cutting the overflow. " message = message + " +++ " + message while message != " ": self.lcd.move_to(0,1) self.lcd.putstr(message[:16]) message = message[1:] time.sleep(0.07) self.lcd.clear() self.too_many_counters_warning_shown = True def show_screen(self): # Function: # show a screen on the lcd, table layout: row 1 shows the short names of the counters, # row 2 shows the values. # WARNING: # This function SHALL be called once at init of the class as it can stop execution for # approx. 15s at first run! line1 = "" line2 = "" number_counters = len(self.counterArray._counters) if number_counters > 5: if not self.too_many_counters_warning_shown: self._too_many_counters_warning() number_counters = 5 ## show different views depending on how many counters exist if number_counters < 1: line1 = " WARNING! " line2 = " No counters. " elif number_counters == 1: line1 = "* C1 " line2 = "* " + str(list(self.counterArray._counters.items())[0][1].value) elif number_counters == 2: v0 = str(list(self.counterArray._counters.items())[0][1].value) v1 = str(list(self.counterArray._counters.items())[1][1].value) line1 = "* C1 * C2 " line2 = "* " + v0 + (6-len(v0))*" " + "* " + v1 + (6-len(v1)) * " " elif number_counters == 3: v0 = str(list(self.counterArray._counters.items())[0][1].value) v1 = str(list(self.counterArray._counters.items())[1][1].value) v2 = str(list(self.counterArray._counters.items())[2][1].value) line1 = "C1 * C2 * C3 " line2 = v0 + (3-len(v0))*" " + " * " + v1 + (3-len(v1))*" " + " * " + v2 + (4-len(v2))*" " elif number_counters == 4: line1 = "|C1 |C2 |C3 |C4 " for i in range(4): line2 += "|" + self._zfill(list(self.counterArray._counters.items())[i][1].value, 3)[:3] else: line1 = "|C1|C2|C3|C4|C5|" for i in range(5): line2 += "|" + self._zfill(list(self.counterArray._counters.items())[i][1].value, 2)[:2] line2 += "|" # check if something changed, if so, redraw it if line1 != self.last_line1: self.lcd.putstr(line1[:16]) self.last_line1 = line1 self.lcd.move_to(0,1) if line2 != self.last_line2: self.lcd.putstr(line2[:16]) self.last_line2 = line2