import colors from datetime import datetime class TimeDisplay: def __init__(self, characterDisplay, topRow): self.characterDisplay = characterDisplay self.topRow = topRow self.currentHour = [-1, -1] self.currentMinute = [-1, -1] self.currentColor = colors.BLACK self.color = colors.RED self.hourStartColumn = -1 self.timeDotsColumn = 7 self.minuteStartColumn = 9 def showTime(self): self.showHour(self.getHourDigits()) self.showTimeDots() self.showMinute(self.getMinuteDigits()) self.currentColor = self.color def showHour(self, hour): if hour[0] != self.currentHour[0]: self.currentHour[0] = hour[0] if hour[0] == '0': self.hideDigit(self.hourStartColumn) else: self.showDigit(self.hourStartColumn, hour[0]) if hour[1] != self.currentHour[1]: self.currentHour[1] = hour[1] self.showDigit(self.hourStartColumn + 4, hour[1]) def showTimeDots(self): if self.color != self.currentColor: self.characterDisplay.displayTimeDots( x=self.timeDotsColumn, y=self.topRow, color=self.color ) def showMinute(self, minute): if minute[0] != self.currentMinute[0]: self.currentMinute[0] = minute[0] self.showDigit(self.minuteStartColumn, minute[0]) if minute[1] != self.currentMinute[1]: self.currentMinute[1] = minute[1] self.showDigit(self.minuteStartColumn + 4, minute[1]) def showDigit(self, x, digit): self.characterDisplay.displayDigit( x=x, y=self.topRow, digit=digit, color=self.color ) def hideDigit(self, x): self.characterDisplay.clearDigit( x=x, y=self.topRow ) def getHourDigits(self): return self.getTimeDigits('%l') def getMinuteDigits(self): return self.getTimeDigits('%M') def getTimeDigits(self, format): digits = datetime.now().strftime(format).strip().rjust(2, '0') return list(digits)