From 688db76bc620d2984f21449157fac9959e3530d0 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Fri, 23 Dec 2022 14:57:33 -0500 Subject: [PATCH] Refactor code --- character_display.py | 31 +++++++++-------- chutney.py | 4 +-- chutney.sample.cfg | 3 ++ time_display.py | 80 ++++++++++++++++++++++++++++++-------------- 4 files changed, 77 insertions(+), 41 deletions(-) diff --git a/character_display.py b/character_display.py index 968e86b..ffb4a9c 100644 --- a/character_display.py +++ b/character_display.py @@ -15,25 +15,24 @@ class CharacterDisplay: - def __init__(self, unicorn): + def __init__(self, unicorn, minX=0, maxX=15): self.unicorn = unicorn + self.minX = minX + self.maxX = maxX def fullLine(self, start, row): - for x in range(max(start, 0), start+3): - self.unicorn.set_pixel(x, row, 255, 0, 0) + for x in range(start, start+3): + self.setPixel(x, row, 255, 0, 0) def bothSides(self, start, row): - if (start >= 0): - self.unicorn.set_pixel(start, row, 255, 0, 0) - - self.unicorn.set_pixel(start+2, row, 255, 0, 0) + self.setPixel(start, row, 255, 0, 0) + self.setPixel(start+2, row, 255, 0, 0) def leftSide(self, start, row): - if (start >= 0): - self.unicorn.set_pixel(start, row, 255, 0, 0) + self.setPixel(start, row, 255, 0, 0) def rightSide(self, start, row): - self.unicorn.set_pixel(start+2, row, 255, 0, 0) + self.setPixel(start+2, row, 255, 0, 0) def displayZero(self, x, y): self.clearNumberPixels(x, y) @@ -141,12 +140,16 @@ class CharacterDisplay: def clearNumberPixels(self, x, y): for y1 in range(y, y-5, -1): - for x1 in range(max(x, 0), x+3): - self.unicorn.set_pixel(x1, y1, 0, 0, 0) + for x1 in range(x, x+3): + self.setPixel(x1, y1, 0, 0, 0) self.unicorn.show() def displayTimeDots(self, x, y): - self.unicorn.set_pixel(x, y-1, 255, 0, 0) - self.unicorn.set_pixel(x, y-3, 255, 0, 0) + self.setPixel(x, y-1, 255, 0, 0) + self.setPixel(x, y-3, 255, 0, 0) self.unicorn.show() + + def setPixel(self, x, y, r, g, b): + if x >= self.minX and x <= self.maxX: + self.unicorn.set_pixel(x, y, r, g, b) diff --git a/chutney.py b/chutney.py index 897ffb9..67693c1 100644 --- a/chutney.py +++ b/chutney.py @@ -25,8 +25,8 @@ def main(): signal(SIGTERM, cleanExit(unicorn)) unicorn.brightness(0.3) - character_display = CharacterDisplay(unicorn) - timeDisplay = TimeDisplay(character_display, lineTop=15) + characterDisplay = CharacterDisplay(unicorn) + timeDisplay = TimeDisplay(characterDisplay, topRow=15) while True: timeDisplay.showTime() diff --git a/chutney.sample.cfg b/chutney.sample.cfg index 570f712..8566b03 100644 --- a/chutney.sample.cfg +++ b/chutney.sample.cfg @@ -1,3 +1,6 @@ [DEFAULT] weather_user=user weather_pass=password +weather_url=url +weather_lat=lat +weather_lon=lon diff --git a/time_display.py b/time_display.py index 39d6123..bf97bbc 100644 --- a/time_display.py +++ b/time_display.py @@ -3,39 +3,69 @@ from datetime import datetime class TimeDisplay: - def __init__(self, character_display, lineTop): - self.displayedHourParts = [-1, -1] - self.displayedMinuteParts = [-1, -1] - self.character_display = character_display - self.lineTop = lineTop + def __init__(self, characterDisplay, topRow): + self.currentHour = [-1, -1] + self.currentMinute = [-1, -1] + self.characterDisplay = characterDisplay + self.topRow = topRow + self.hourStartColumn = -1 + self.timeDotsColumn = 7 + self.minuteStartColumn = 9 - self.character_display.displayTimeDots(7, self.lineTop) + self.characterDisplay.displayTimeDots(self.timeDotsColumn, self.topRow) def showTime(self): - hourParts = self.getTimeParts('%l') - minuteParts = self.getTimeParts('%M') + self.showHour(self.getHourDigits()) + self.showMinute(self.getMinuteDigits()) - if hourParts[0] != self.displayedHourParts[0]: - self.displayedHourParts[0] = hourParts[0] + def showHour(self, hour): + if hour[0] != self.currentHour[0]: + self.currentHour[0] = hour[0] - if hourParts[0] == 0: - self.character_display.clearNumberPixels(-1, self.lineTop) + if hour[0] == 0: + self.characterDisplay.clearNumberPixels( + self.hourStartColumn, + self.topRow + ) else: - self.character_display.displayNumber(-1, self.lineTop, hourParts[0]) + self.characterDisplay.displayNumber( + self.hourStartColumn, + self.topRow, + hour[0] + ) - if hourParts[1] != self.displayedHourParts[1]: - self.displayedHourParts[1] = hourParts[1] - self.character_display.displayNumber(3, self.lineTop, hourParts[1]) + if hour[1] != self.currentHour[1]: + self.currentHour[1] = hour[1] + self.characterDisplay.displayNumber( + self.hourStartColumn + 4, + self.topRow, + hour[1] + ) - if minuteParts[0] != self.displayedMinuteParts[0]: - self.displayedMinuteParts[0] = minuteParts[0] - self.character_display.displayNumber(9, self.lineTop, minuteParts[0]) + def showMinute(self, minute): + if minute[0] != self.currentMinute[0]: + self.currentMinute[0] = minute[0] + self.characterDisplay.displayNumber( + self.minuteStartColumn, + self.topRow, + minute[0] + ) - if minuteParts[1] != self.displayedMinuteParts[1]: - self.displayedMinuteParts[1] = minuteParts[1] - self.character_display.displayNumber(13, self.lineTop, minuteParts[1]) + if minute[1] != self.currentMinute[1]: + self.currentMinute[1] = minute[1] + self.characterDisplay.displayNumber( + self.minuteStartColumn + 4, + self.topRow, + minute[1] + ) - def getTimeParts(self, timePart): - parts = datetime.now().strftime(timePart).strip().rjust(2, "0") + def getHourDigits(self): + return self.getTimeDigits('%l') - return [int(x) for x in parts] + def getMinuteDigits(self): + return self.getTimeDigits('%M') + + def getTimeDigits(self, format): + digits = datetime.now().strftime(format).strip().rjust(2, '0') + + return [int(x) for x in digits]