Refactor code

This commit is contained in:
Mike Cifelli 2022-12-23 14:57:33 -05:00
parent bce19458ee
commit 688db76bc6
4 changed files with 77 additions and 41 deletions

View File

@ -15,25 +15,24 @@
class CharacterDisplay: class CharacterDisplay:
def __init__(self, unicorn): def __init__(self, unicorn, minX=0, maxX=15):
self.unicorn = unicorn self.unicorn = unicorn
self.minX = minX
self.maxX = maxX
def fullLine(self, start, row): def fullLine(self, start, row):
for x in range(max(start, 0), start+3): for x in range(start, start+3):
self.unicorn.set_pixel(x, row, 255, 0, 0) self.setPixel(x, row, 255, 0, 0)
def bothSides(self, start, row): def bothSides(self, start, row):
if (start >= 0): self.setPixel(start, row, 255, 0, 0)
self.unicorn.set_pixel(start, row, 255, 0, 0) self.setPixel(start+2, row, 255, 0, 0)
self.unicorn.set_pixel(start+2, row, 255, 0, 0)
def leftSide(self, start, row): def leftSide(self, start, row):
if (start >= 0): self.setPixel(start, row, 255, 0, 0)
self.unicorn.set_pixel(start, row, 255, 0, 0)
def rightSide(self, start, row): 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): def displayZero(self, x, y):
self.clearNumberPixels(x, y) self.clearNumberPixels(x, y)
@ -141,12 +140,16 @@ class CharacterDisplay:
def clearNumberPixels(self, x, y): def clearNumberPixels(self, x, y):
for y1 in range(y, y-5, -1): for y1 in range(y, y-5, -1):
for x1 in range(max(x, 0), x+3): for x1 in range(x, x+3):
self.unicorn.set_pixel(x1, y1, 0, 0, 0) self.setPixel(x1, y1, 0, 0, 0)
self.unicorn.show() self.unicorn.show()
def displayTimeDots(self, x, y): def displayTimeDots(self, x, y):
self.unicorn.set_pixel(x, y-1, 255, 0, 0) self.setPixel(x, y-1, 255, 0, 0)
self.unicorn.set_pixel(x, y-3, 255, 0, 0) self.setPixel(x, y-3, 255, 0, 0)
self.unicorn.show() 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)

View File

@ -25,8 +25,8 @@ def main():
signal(SIGTERM, cleanExit(unicorn)) signal(SIGTERM, cleanExit(unicorn))
unicorn.brightness(0.3) unicorn.brightness(0.3)
character_display = CharacterDisplay(unicorn) characterDisplay = CharacterDisplay(unicorn)
timeDisplay = TimeDisplay(character_display, lineTop=15) timeDisplay = TimeDisplay(characterDisplay, topRow=15)
while True: while True:
timeDisplay.showTime() timeDisplay.showTime()

View File

@ -1,3 +1,6 @@
[DEFAULT] [DEFAULT]
weather_user=user weather_user=user
weather_pass=password weather_pass=password
weather_url=url
weather_lat=lat
weather_lon=lon

View File

@ -3,39 +3,69 @@ from datetime import datetime
class TimeDisplay: class TimeDisplay:
def __init__(self, character_display, lineTop): def __init__(self, characterDisplay, topRow):
self.displayedHourParts = [-1, -1] self.currentHour = [-1, -1]
self.displayedMinuteParts = [-1, -1] self.currentMinute = [-1, -1]
self.character_display = character_display self.characterDisplay = characterDisplay
self.lineTop = lineTop 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): def showTime(self):
hourParts = self.getTimeParts('%l') self.showHour(self.getHourDigits())
minuteParts = self.getTimeParts('%M') self.showMinute(self.getMinuteDigits())
if hourParts[0] != self.displayedHourParts[0]: def showHour(self, hour):
self.displayedHourParts[0] = hourParts[0] if hour[0] != self.currentHour[0]:
self.currentHour[0] = hour[0]
if hourParts[0] == 0: if hour[0] == 0:
self.character_display.clearNumberPixels(-1, self.lineTop) self.characterDisplay.clearNumberPixels(
self.hourStartColumn,
self.topRow
)
else: 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]: if hour[1] != self.currentHour[1]:
self.displayedHourParts[1] = hourParts[1] self.currentHour[1] = hour[1]
self.character_display.displayNumber(3, self.lineTop, hourParts[1]) self.characterDisplay.displayNumber(
self.hourStartColumn + 4,
self.topRow,
hour[1]
)
if minuteParts[0] != self.displayedMinuteParts[0]: def showMinute(self, minute):
self.displayedMinuteParts[0] = minuteParts[0] if minute[0] != self.currentMinute[0]:
self.character_display.displayNumber(9, self.lineTop, minuteParts[0]) self.currentMinute[0] = minute[0]
self.characterDisplay.displayNumber(
self.minuteStartColumn,
self.topRow,
minute[0]
)
if minuteParts[1] != self.displayedMinuteParts[1]: if minute[1] != self.currentMinute[1]:
self.displayedMinuteParts[1] = minuteParts[1] self.currentMinute[1] = minute[1]
self.character_display.displayNumber(13, self.lineTop, minuteParts[1]) self.characterDisplay.displayNumber(
self.minuteStartColumn + 4,
self.topRow,
minute[1]
)
def getTimeParts(self, timePart): def getHourDigits(self):
parts = datetime.now().strftime(timePart).strip().rjust(2, "0") 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]