Toggle Navigation
Hatchery
Eggs
Countdowns.
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import display, wifi, time, math, keyboard, buttons radMinutes = display.height() / 4.5 radHours = display.height() / 3.25 radDays = display.height() / 2.5 radYears = display.height() / 2 colMinutes = 0x000000 colHours = 0x000000 colDays = 0x000000 colYears = 0x000000 colEdge = 0xffffff colBackground = 0xffffff colForeground = 0x000000 colText = 0x000000 colTextForeground = 0xffffff fontBig = "permanentmarker22" fontSmall = "7x5" waitYear = 0 waitDayYear = 0 waitHour = 0 waitMin = 0 waitSec = 0 isEink = True mch2021 = (2022, 9, 3, 12, 0, 0, 6, 246) toYear, toMon, toDayMon, toHour, toMin, toSec, toDayWeek, toDayYear = mch2021 nowYear, nowMon, nowDayMon, nowHour, nowMin, nowSec, nowDayWeek, nowDayYear = time.localtime() infoCountdownSelAdd = "A: select event" infoSelectEventSelEdit = "A: select B: edit" infoEditEventAccept = "SELECT: accept A: edit" infoEditEventField = "B: done" def drawInfoText(text, atBottom = False): y = display.height() - display.getTextHeight(text, fontSmall) if atBottom else 0 x = display.width() - display.getTextWidth(text, fontSmall) display.drawText(x, y, text, colText, fontSmall) def drawCenteredText(text): x = (display.width() - display.getTextWidth(text, fontBig)) // 2 y = (display.height() - display.getTextHeight(text, fontBig)) // 2 display.drawText(x, y, text, colText, fontBig) def getTime(): global waitYear, waitDayYear, waitHour, waitMin, waitSec global nowYear, nowMon, nowDayMon, nowHour, nowMin, nowSec, nowDayWeek, nowDayYear nowYear, nowMon, nowDayMon, nowHour, nowMin, nowSec, nowDayWeek, nowDayYear = time.localtime() pWaitYear, pWaitDayYear, pWaitHour, pWaitMin, pWaitSec = waitYear, waitDayYear, waitHour, waitMin, waitSec waitYear = toYear - nowYear waitDayYear = toDayYear - nowDayYear waitHour = toHour - nowHour waitMin = toMin - nowMin waitSec = toSec - nowSec if waitSec < 0: waitMin -= 1 waitSec += 60 if waitMin < 0: waitHour -= 1 waitMin += 60 if waitHour < 0: waitDayYear -= 1 waitHour += 24 if waitDayYear < 0: waitDayYear += 365 waitYear -= 1 if waitYear < 0: waitSec = 0 waitMin = 0 waitHour = 0 waitDayYear = 0 waitYear = 0 if pWaitYear != waitYear or pWaitDayYear != waitDayYear or pWaitHour != waitHour or pWaitMin != waitMin or pWaitSec != waitSec: return True else: return False def getRemainString(): anded = False out = "" if waitYear > 0: out += ("and {} year{}\n" if anded else "{} year{}\n").format(waitYear, "s" if waitYear != 1 else "") anded = True if waitDayYear > 0 or waitYear > 0: out += ("and {} day{}\n" if anded else "{} day{}\n").format(waitDayYear, "s" if waitDayYear != 1 else "") anded = True if waitHour > 0 or waitDayYear > 0 or waitYear > 0: out += ("and {} hour{}\n" if anded else "{} hour{}\n").format(waitHour, "s" if waitHour != 1 else "") anded = True if waitMin > 0 or waitHour > 0 or waitDayYear > 0 or waitYear > 0: out += ("and {} minute{}\n" if anded else "{} minute{}\n").format(waitMin, "s" if waitMin != 1 else "") anded = True out += ("and {} second{}\n" if anded else "{} second{}\n").format(waitSec, "s" if waitSec != 1 else "") return out + "remain{}".format("s" if waitSec == 1 else "") monthsLong = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] monthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] daysOfWeekLong = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] daysOfWeekShort = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] def isLeapYear(year): """ Checks whether or not a year is a leap year.""" return year % 4 == 0 and (not year % 100 == 0 or year % 400 == 0) def getYearDays(year): """ Gets the days in a year, based on whether or not it is a leap year. """ return 366 if isLeapYear(year) else 365 def getMonthDays(month, year): """ Gets the days in a month, based on whether or not the year is a leap year. Months start at 1. """ if month == 2: return 29 if isLeapYear(year) else 28 else: return 30 + (month & 1) ^ (month >= 8) def getDayOfWeek(day, month, year): """ Gets the day of week of a given date. Returns 1->7, monday->sunday. """ mon = month if month <= 2: mon += 12 year -= 1 y = year % 100 c = int(year / 100) w = (day + (mon + 1) * 13 // 5 + y + y // 4 + c // 4 + c * 5 + 5) % 7 + 1 return w def getDayOfYear(day, month, year): """ Gets the day of year of a given date. Returns 1->365 366. """ for i in range(month - 1): day += getMonthDays(i + 1, year) return day class Event: year = 0 # any year month = 0 # 1 -> 12 dayOfMonth = 0 # 1 -> 28 29 30 31 hour = 0 # 0 -> 24 minute = 0 # 0 -> 60 second = 0 # 0 -> 60 dayOfWeek = 0 # 1 -> 7, monday -> sunday dayOfYear = 0 # 0 -> 365 366 awaitText = "" # shown in the countdown splashText = "" # shown when the countdown ends def __init__(self, day, month, year, hour = 12, minute = 0, second = 0, splashText = "", awaitText = ""): self.dayOfMonth = day self.month = month self.year = year self.dayOfWeek = getDayOfWeek(day, month, year) self.dayOfYear = getDayOfYear(day, month, year) self.hour = hour self.minute = minute self.second = second self.awaitText = awaitText self.splashText = splashText def timeStr(self): h = "0" + str(self.hour) if self.hour < 10 else str(self.hour) m = "0" + str(self.minute) if self.minute < 10 else str(self.minute) s = "0" + str(self.second) if self.second < 10 else str(self.second) return h + ":" + m + ":" + s def setEventTime(event): global toYear, toMon, toDayMon, toHour, toMin, toSec, toDayWeek, toDayYear toYear = event.year toMon = event.month toDayMon = event.dayOfMonth toHour = event.hour toMin = event.minute toSec = event.second toDayWeek = event.dayOfWeek toDayYear = event.dayOfYear def EventMCH2021():#(2021, 8, 6, 12, 0, 0, 6, 219) return Event(3, 9, 2022, splashText="MCH2022!", awaitText="until MCH2022") SCREEN_COUNTDOWN = 0 SCREEN_EDIT_EVENT = 1 SCREEN_SELECT_EVENT = 2 SCREEN_WIFI = 3 EDITING_SPLASH = 100 EDITING_SPLASH_TYPING = 101 EDITING_AWAIT = 200 EDITING_AWAIT_TYPING = 201 EDITING_YEAR = 300 EDITING_YEAR_1000 = 301 EDITING_YEAR_100 = 302 EDITING_YEAR_10 = 303 EDITING_YEAR_1 = 304 EDITING_MONTHS = 400 EDITING_MONTHS_11 = 401 EDITING_DAY = 500 EDITING_DAY_1 = 501 EDITING_DAY_10 = 502 EDITING_TIME = 600 EDITING_TIME_HOUR_10 = 601 EDITING_TIME_HOUR_1 = 602 EDITING_TIME_MIN_10 = 603 EDITING_TIME_MIN_1 = 604 EDITING_TIME_SEC_10 = 605 EDITING_TIME_SEC_1 = 606 events = [EventMCH2021(), Event(25, 7, 2020, 12, 0, 0, "Random event!", "until random event")] currentEvent = events[0] currentEventIndex = 0 currentlyEditing = EDITING_SPLASH highlightableEdit = [EDITING_SPLASH, EDITING_AWAIT, EDITING_DAY, EDITING_MONTHS, EDITING_YEAR, EDITING_TIME] currentlyHighlighted = 0 toSplash = "MCH2021!" toAwait = "until MCH2021" setEventTime(events[0]) timeGotten = False numTries = 0 redrawNeeded = True display.clearMatrix() def drawTime(): global radYears, radDays, radHours, radMinutes, redrawNeeded global waitSec, waitMin, waitYear, waitHour, waitDayYear, toAwait, toSplash display.drawFill(colBackground) if waitYear == 0 and waitDayYear == 0 and waitHour == 0 and waitMin == 0 and waitSec == 0: drawCenteredText(toSplash) return display.pushMatrix() display.translate(display.width() / 2, display.height() / 2) yee = 1 if waitYear > 0 else waitDayYear / 365 if waitDayYear > 0 or waitYear > 0: display.drawCircle(0, 0, radYears, (1 - yee) * 360, 360, True, colYears) display.drawCircle(0, 0, radDays + 2, 0, 360, True, colBackground) display.drawCircle(0, 0, radDays, (1 - waitHour / 24) * 360, 360, True, colDays) display.drawCircle(0, 0, radHours + 2, 0, 360, True, colBackground) display.drawCircle(0, 0, radHours, (1 - waitMin / 60) * 360, 360, True, colHours) display.drawCircle(0, 0, radMinutes + 2, 0, 360, True, colBackground) display.drawCircle(0, 0, radMinutes, (1 - waitSec / 60) * 360, 360, True, colMinutes) elif waitHour > 0: display.drawCircle(0, 0, radYears, (1 - waitHour / 24) * 360, 360, True, colDays) display.drawCircle(0, 0, radDays + 2, 0, 360, True, colBackground) display.drawCircle(0, 0, radDays, (1 - waitMin / 60) * 360, 360, True, colHours) display.drawCircle(0, 0, radHours + 2, 0, 360, True, colBackground) display.drawCircle(0, 0, radHours, (1 - waitSec / 60) * 360, 360, True, colMinutes) elif waitMin > 0: display.drawCircle(0, 0, radYears, (1 - waitMin / 60) * 360, 360, True, colHours) display.drawCircle(0, 0, radDays + 2, 0, 360, True, colBackground) display.drawCircle(0, 0, radDays, (1 - waitSec / 60) * 360, 360, True, colMinutes) else: display.drawCircle(0, 0, radYears, (1 - waitSec / 60) * 360, 360, True, colMinutes) display.popMatrix() remain = getRemainString() ht = display.getTextHeight(remain, fontSmall) display.drawText(5, display.height() - ht - 5, getRemainString(), colText, fontSmall) display.drawText(5, 5, toAwait, colText, fontSmall) def onUp(value): global screen, events, dirSelIndex, redrawNeeded, highlightableEdit, currentlyEditing, currentlyHighlighted if value: if screen == SCREEN_SELECT_EVENT: dirSelIndex = (dirSelIndex - 1) % len(events) redrawNeeded = True elif screen == SCREEN_EDIT_EVENT: event = events[dirSelIndex] if currentlyEditing in highlightableEdit: currentlyHighlighted = (currentlyHighlighted - 1) % len(highlightableEdit) currentlyEditing = highlightableEdit[currentlyHighlighted] elif currentlyEditing == EDITING_TIME_HOUR_10: event.hour = (event.hour + 10) % 24 elif currentlyEditing == EDITING_TIME_HOUR_1: event.hour = (event.hour + 1) % 24 elif currentlyEditing == EDITING_TIME_MIN_10: event.minute = (event.minute + 10) % 60 elif currentlyEditing == EDITING_TIME_MIN_1: event.minute = (event.minute + 1) % 60 elif currentlyEditing == EDITING_TIME_SEC_10: event.second = (event.second + 10) % 60 elif currentlyEditing == EDITING_TIME_SEC_1: event.second = (event.second + 1) % 60 elif currentlyEditing == EDITING_DAY_10: event.dayOfMonth = (event.dayOfMonth + 10) % getMonthDays(event.month, event.year) elif currentlyEditing == EDITING_DAY_1: event.dayOfMonth = (event.dayOfMonth + 1) % getMonthDays(event.month, event.year) elif currentlyEditing == EDITING_MONTHS_11: event.month = event.month % 12 + 1 elif currentlyEditing == EDITING_YEAR_100: event.year = (event.year + 100) % 1000 + 2000 elif currentlyEditing == EDITING_YEAR_10: event.year = (event.year + 10) % 1000 + 2000 elif currentlyEditing == EDITING_YEAR_1: event.year = (event.year + 1) % 1000 + 2000 redrawNeeded = True def onDown(value): global screen, events, dirSelIndex, redrawNeeded, highlightableEdit, currentlyEditing, currentlyHighlighted if value: if screen == SCREEN_SELECT_EVENT: dirSelIndex = (dirSelIndex + 1) % len(events) redrawNeeded = True elif screen == SCREEN_EDIT_EVENT: event = events[dirSelIndex] if currentlyEditing in highlightableEdit: currentlyHighlighted = (currentlyHighlighted + 1) % len(highlightableEdit) currentlyEditing = highlightableEdit[currentlyHighlighted] elif currentlyEditing == EDITING_TIME_HOUR_10: event.hour = (event.hour - 10) % 24 elif currentlyEditing == EDITING_TIME_HOUR_1: event.hour = (event.hour - 1) % 24 elif currentlyEditing == EDITING_TIME_MIN_10: event.minute = (event.minute - 10) % 60 elif currentlyEditing == EDITING_TIME_MIN_1: event.minute = (event.minute - 1) % 60 elif currentlyEditing == EDITING_TIME_SEC_10: event.second = (event.second - 10) % 60 elif currentlyEditing == EDITING_TIME_SEC_1: event.second = (event.second - 1) % 60 elif currentlyEditing == EDITING_DAY_10: event.dayOfMonth = (event.dayOfMonth - 10) % getMonthDays(event.month, event.year) elif currentlyEditing == EDITING_DAY_1: event.dayOfMonth = (event.dayOfMonth - 1) % getMonthDays(event.month, event.year) elif currentlyEditing == EDITING_MONTHS_11: event.month = (event.month - 2) % 12 + 1 elif currentlyEditing == EDITING_YEAR_100: event.year = (event.year - 100) % 1000 + 2000 elif currentlyEditing == EDITING_YEAR_10: event.year = (event.year - 10) % 1000 + 2000 elif currentlyEditing == EDITING_YEAR_1: event.year = (event.year - 1) % 1000 + 2000 redrawNeeded = True def onLeft(value): global screen, events, dirSelIndex, redrawNeeded, highlightableEdit, currentlyEditing, currentlyHighlighted if value: if screen == SCREEN_EDIT_EVENT and currentlyEditing not in highlightableEdit: if currentlyEditing // 100 * 100 == EDITING_TIME: currentlyEditing = EDITING_TIME + 1 + (currentlyEditing - 2 - EDITING_TIME) % 6 redrawNeeded = True if currentlyEditing // 100 * 100 == EDITING_YEAR: currentlyEditing = EDITING_YEAR + 1 + (currentlyEditing - 2 - EDITING_YEAR) % 4 redrawNeeded = True elif currentlyEditing == EDITING_DAY_10: currentlyEditing = EDITING_DAY_1 redrawNeeded = True elif currentlyEditing == EDITING_DAY_1: currentlyEditing = EDITING_DAY_10 redrawNeeded = True def onRight(value): global screen, events, dirSelIndex, redrawNeeded, highlightableEdit, currentlyEditing, currentlyHighlighted if value: if screen == SCREEN_EDIT_EVENT and currentlyEditing not in highlightableEdit: if currentlyEditing // 100 * 100 == EDITING_TIME: currentlyEditing = EDITING_TIME + 1 + (currentlyEditing - EDITING_TIME) % 6 redrawNeeded = True if currentlyEditing // 100 * 100 == EDITING_YEAR: currentlyEditing = EDITING_YEAR + 1 + (currentlyEditing - EDITING_YEAR) % 4 redrawNeeded = True elif currentlyEditing == EDITING_DAY_10: currentlyEditing = EDITING_DAY_1 redrawNeeded = True elif currentlyEditing == EDITING_DAY_1: currentlyEditing = EDITING_DAY_10 redrawNeeded = True def onA(value): global screen, events, currentEvent, currentEventIndex, toSplash, toAwait, dirSelIndex, currentlyEditing, redrawNeeded if value: if screen == SCREEN_COUNTDOWN: screen = SCREEN_SELECT_EVENT elif screen == SCREEN_SELECT_EVENT: currentEventIndex = dirSelIndex event = events[currentEventIndex] toSplash = event.splashText toAwait = event.awaitText setEventTime(event) screen = SCREEN_WIFI elif screen == SCREEN_EDIT_EVENT: if currentlyEditing == EDITING_AWAIT: currentlyEditing = EDITING_AWAIT_TYPING keyboard.show("Select await text...", events[dirSelIndex].awaitText, onKeyboardDone) elif currentlyEditing == EDITING_SPLASH: currentlyEditing = EDITING_SPLASH_TYPING keyboard.show("Select splash text...", events[dirSelIndex].splashText, onKeyboardDone) elif currentlyEditing == EDITING_TIME: currentlyEditing = EDITING_TIME_HOUR_1 redrawNeeded = True elif currentlyEditing == EDITING_DAY: currentlyEditing = EDITING_DAY_1 redrawNeeded = True elif currentlyEditing == EDITING_MONTHS: currentlyEditing = EDITING_MONTHS_11 redrawNeeded = True elif currentlyEditing == EDITING_YEAR: currentlyEditing = EDITING_YEAR_1 redrawNeeded = True def onB(value): global screen, events, currentEvent, currentEventIndex, toSplash, toAwait, dirSelIndex, currentlyEditing, currentlyHighlighted, redrawNeeded if value: if screen == SCREEN_SELECT_EVENT: screen = SCREEN_EDIT_EVENT currentlyEditing = EDITING_SPLASH currentlyHighlighted = 0 elif screen == SCREEN_EDIT_EVENT and currentlyEditing // 100 != currentlyEditing / 100: event = events[dirSelIndex] if currentlyEditing != EDITING_SPLASH_TYPING and currentlyEditing != EDITING_AWAIT_TYPING: currentlyEditing = currentlyEditing // 100 * 100 if currentlyEditing == EDITING_DAY or currentlyEditing == EDITING_MONTHS or currentlyEditing == EDITING_YEAR: event.dayOfMonth = min(event.dayOfMonth, getMonthDays(event.month, event.year)) event.dayOfYear = getDayOfYear(event.dayOfMonth, event.month, event.year) event.dayOfWeek = getDayOfWeek(event.dayOfMonth, event.month, event.year) redrawNeeded = True def onSelect(value): global screen, events, currentEvent, currentEventIndex, toSplash, toAwait, dirSelIndex, currentlyEditing, currentlyHighlighted if value: if screen == SCREEN_EDIT_EVENT: if currentlyEditing in highlightableEdit: screen = SCREEN_SELECT_EVENT def onKeyboardDone(result): global screen, currentlyEditing, redrawNeeded, events, dirSelIndex if screen == SCREEN_EDIT_EVENT: if currentlyEditing == EDITING_AWAIT_TYPING: currentlyEditing = EDITING_AWAIT events[dirSelIndex].awaitText = result elif currentlyEditing == EDITING_SPLASH_TYPING: currentlyEditing = EDITING_SPLASH events[dirSelIndex].splashText = result redrawNeeded = True buttons.attach(buttons.BTN_UP, onUp) buttons.attach(buttons.BTN_DOWN, onDown) buttons.attach(buttons.BTN_LEFT, onLeft) buttons.attach(buttons.BTN_RIGHT, onRight) buttons.attach(buttons.BTN_A, onA) buttons.attach(buttons.BTN_B, onB) buttons.attach(buttons.BTN_SELECT, onSelect) def dirCountdown(): global screen, redrawNeeded redrawNeeded = getTime() if redrawNeeded: drawTime() drawInfoText(infoCountdownSelAdd) dirSelIndex = 0 def dirSelectEvent(): global events, currentEvent, currentEventIndex, toSplash, toAwait, dirSelIndex, redrawNeeded if not redrawNeeded: return getTime() display.drawFill(colBackground) yMul = 11 for i in range(len(events)): event0 = events[i] day, mon, year = event0.dayOfMonth, event0.month, event0.year eventText = "{} {} {} {} {} {}:{}:{}".format( event0.awaitText, daysOfWeekShort[getDayOfWeek(day, mon, year) - 1], day, monthsShort[mon - 1], year, event0.hour, event0.minute, event0.second) if i == currentEventIndex: eventText = "> " + eventText else: eventText = " " + eventText if i == dirSelIndex: display.drawRect(0, i * yMul, display.width(), yMul, True, colForeground) display.drawText(0, i * yMul + 1, eventText, colTextForeground, fontSmall) else: display.drawText(0, i * yMul + 1, eventText, colText, fontSmall) nowText = "{} {} {} {} {}:{}:{}".format( daysOfWeekShort[getDayOfWeek(nowDayMon, nowMon, nowYear) - 1], nowDayMon, monthsShort[nowMon - 1], nowYear, nowHour, nowMin, nowSec) ht = display.getTextHeight(nowText, fontSmall) display.drawText(0, display.height() - ht, nowText, colText, fontSmall) drawInfoText(infoSelectEventSelEdit, True) redrawNeeded = False def dirEditEvent(): global redrawNeeded, events, dirSelIndex, currentlyEditing if not redrawNeeded: return yMul = 11 event = events[dirSelIndex] display.drawFill(0xffffff) display.drawText(0, 1, "Splash text:", colText, fontSmall) display.drawText(0, 1 + yMul, "Await text:", colText, fontSmall) display.drawText(0, 1 + yMul * 2, "Day:", colText, fontSmall) display.drawText(0, 1 + yMul * 3, "Month:", colText, fontSmall) display.drawText(0, 1 + yMul * 4, "Year:", colText, fontSmall) display.drawText(0, 1 + yMul * 5, "Time:", colText, fontSmall) xOfs = 6 * 13 if currentlyEditing == EDITING_SPLASH: width = display.getTextWidth(event.splashText, fontSmall) display.drawRect(xOfs - 1, 0, width + 2, yMul, True, colForeground) display.drawText(xOfs, 1, event.splashText, colTextForeground, fontSmall) else: display.drawText(xOfs, 1, event.splashText, colText, fontSmall) if currentlyEditing == EDITING_AWAIT: width = display.getTextWidth(event.awaitText, fontSmall) display.drawRect(xOfs - 1, yMul, width + 2, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul, event.awaitText, colTextForeground, fontSmall) else: display.drawText(xOfs, 1 + yMul, event.awaitText, colText, fontSmall) if currentlyEditing == EDITING_DAY_10: display.drawText(xOfs, 1 + yMul * 2, daysOfWeekLong[getDayOfWeek(event.dayOfMonth, event.month, event.year) - 1], colText, fontSmall) display.drawRect(xOfs - 1 + 10 * 6, yMul * 2, 8, yMul, True, colForeground) display.drawText(xOfs + 10 * 6, 1 + yMul * 2, str(event.dayOfMonth // 10), colTextForeground, fontSmall) display.drawText(xOfs + 11 * 6, 1 + yMul * 2, str(event.dayOfMonth % 10), colText, fontSmall) elif currentlyEditing == EDITING_DAY_1: display.drawText(xOfs, 1 + yMul * 2, daysOfWeekLong[getDayOfWeek(event.dayOfMonth, event.month, event.year) - 1], colText, fontSmall) display.drawRect(xOfs - 1 + 11 * 6, yMul * 2, 8, yMul, True, colForeground) display.drawText(xOfs + 10 * 6, 1 + yMul * 2, str(event.dayOfMonth // 10), colText, fontSmall) display.drawText(xOfs + 11 * 6, 1 + yMul * 2, str(event.dayOfMonth % 10), colTextForeground, fontSmall) elif currentlyEditing == EDITING_DAY: dayStr = "{} {}".format(daysOfWeekLong[getDayOfWeek(event.dayOfMonth, event.month, event.year) - 1], event.dayOfMonth) width = display.getTextWidth(dayStr, fontSmall) display.drawRect(xOfs - 1, yMul * 2, width + 2, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul * 2, dayStr, colTextForeground, fontSmall) else: dayStr = "{} {}".format(daysOfWeekLong[getDayOfWeek(event.dayOfMonth, event.month, event.year) - 1], event.dayOfMonth) display.drawText(xOfs, 1 + yMul * 2, dayStr, colText, fontSmall) if currentlyEditing == EDITING_MONTHS_11: monthStr = monthsLong[event.month - 1] monthNumStr = " (month {})".format(event.month) width = display.getTextWidth(monthStr, fontSmall) display.drawRect(xOfs - 1, yMul * 3, width + 2, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul * 3, monthStr, colTextForeground, fontSmall) display.drawText(xOfs + width + 1, 1 + yMul * 3, monthNumStr, colText, fontSmall) elif currentlyEditing == EDITING_MONTHS: monthStr = "{} (month {})".format(monthsLong[event.month - 1], event.month) width = display.getTextWidth(monthStr, fontSmall) display.drawRect(xOfs - 1, yMul * 3, width + 2, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul * 3, monthStr, colTextForeground, fontSmall) else: monthStr = "{} (month {})".format(monthsLong[event.month - 1], event.month) display.drawText(xOfs, 1 + yMul * 3, monthStr, colText, fontSmall) if currentlyEditing // 100 * 100 == EDITING_YEAR and currentlyEditing // 100 != currentlyEditing / 100: if currentlyEditing == EDITING_YEAR_1000: display.drawRect(xOfs - 1, yMul * 4, 8, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul * 4, str(event.year // 1000), colTextForeground, fontSmall) else: display.drawText(xOfs, 1 + yMul * 4, str(event.year // 1000), colText, fontSmall) if currentlyEditing == EDITING_YEAR_100: display.drawRect(xOfs + 5, yMul * 4, 8, yMul, True, colForeground) display.drawText(xOfs + 6, 1 + yMul * 4, str(event.year // 100 % 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 6, 1 + yMul * 4, str(event.year // 100 % 10), colText, fontSmall) if currentlyEditing == EDITING_YEAR_10: display.drawRect(xOfs + 11, yMul * 4, 8, yMul, True, colForeground) display.drawText(xOfs + 12, 1 + yMul * 4, str(event.year // 10 % 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 12, 1 + yMul * 4, str(event.year // 10 % 10), colText, fontSmall) if currentlyEditing == EDITING_YEAR_1: display.drawRect(xOfs + 17, yMul * 4, 8, yMul, True, colForeground) display.drawText(xOfs + 18, 1 + yMul * 4, str(event.year % 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 18, 1 + yMul * 4, str(event.year % 10), colText, fontSmall) elif currentlyEditing == EDITING_YEAR: width = display.getTextWidth(str(event.year), fontSmall) display.drawRect(xOfs - 1, yMul * 4, width + 2, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul * 4, str(event.year), colTextForeground, fontSmall) else: display.drawText(xOfs, 1 + yMul * 4, str(event.year), colText, fontSmall) if currentlyEditing // 100 * 100 == EDITING_TIME and currentlyEditing // 100 != currentlyEditing / 100: display.drawText(xOfs, 1 + yMul * 5, " : :", colText, fontSmall) if currentlyEditing == EDITING_TIME_HOUR_10: display.drawRect(xOfs - 1, yMul * 5, 8, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul * 5, str(event.hour // 10), colTextForeground, fontSmall) else: display.drawText(xOfs, 1 + yMul * 5, str(event.hour // 10), colText, fontSmall) if currentlyEditing == EDITING_TIME_HOUR_1: display.drawRect(xOfs + 5, yMul * 5, 8, yMul, True, colForeground) display.drawText(xOfs + 6, 1 + yMul * 5, str(event.hour % 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 6, 1 + yMul * 5, str(event.hour % 10), colText, fontSmall) if currentlyEditing == EDITING_TIME_MIN_10: display.drawRect(xOfs + 17, yMul * 5, 8, yMul, True, colForeground) display.drawText(xOfs + 18, 1 + yMul * 5, str(event.minute // 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 18, 1 + yMul * 5, str(event.minute // 10), colText, fontSmall) if currentlyEditing == EDITING_TIME_MIN_1: display.drawRect(xOfs + 23, yMul * 5, 8, yMul, True, colForeground) display.drawText(xOfs + 24, 1 + yMul * 5, str(event.minute % 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 24, 1 + yMul * 5, str(event.minute % 10), colText, fontSmall) if currentlyEditing == EDITING_TIME_SEC_10: display.drawRect(xOfs + 35, yMul * 5, 8, yMul, True, colForeground) display.drawText(xOfs + 36, 1 + yMul * 5, str(event.second // 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 36, 1 + yMul * 5, str(event.second // 10), colText, fontSmall) if currentlyEditing == EDITING_TIME_SEC_1: display.drawRect(xOfs + 41, yMul * 5, 8, yMul, True, colForeground) display.drawText(xOfs + 42, 1 + yMul * 5, str(event.second % 10), colTextForeground, fontSmall) else: display.drawText(xOfs + 42, 1 + yMul * 5, str(event.second % 10), colText, fontSmall) elif currentlyEditing == EDITING_TIME: timeStr = event.timeStr() width = display.getTextWidth(timeStr, fontSmall) display.drawRect(xOfs - 1, yMul * 5, width + 2, yMul, True, colForeground) display.drawText(xOfs, 1 + yMul * 5, timeStr, colTextForeground, fontSmall) else: timeStr = event.timeStr() display.drawText(xOfs, 1 + yMul * 5, timeStr, colText, fontSmall) if currentlyEditing // 100 != currentlyEditing / 100: drawInfoText(infoEditEventField, True) else: drawInfoText(infoEditEventAccept, True) redrawNeeded = False def dirWifi(): global screen, lastScreen, numTries, redrawNeeded if timeGotten: screen = SCREEN_COUNTDOWN lastScreen = SCREEN_COUNTDOWN numTries = 0 redrawNeeded = True return display.drawFill(colBackground) if not wifi.status(): drawCenteredText("Connecting to WiFi...") display.flush() wifi.connect() if not wifi.wait(): display.drawFill(colBackground) drawCenteredText("Could not connect!") display.flush() time.sleep(2) numTries += 1 if numTries >= 3: screen = SCREEN_SELECT_EVENT numTries = 0 return display.drawFill(colBackground) drawCenteredText("Getting time...") display.flush() if not wifi.ntp(): display.drawFill(colBackground) drawCenteredText("Could not get time!") display.flush() time.sleep(2) numTries += 1 if numTries >= 3: screen = SCREEN_SELECT_EVENT numTries = 0 return screen = SCREEN_COUNTDOWN lastScreen = None screenChanged = True screen = SCREEN_WIFI def loop(): global lastScreen, screen, isEink, screenChanged, numTries, redrawNeeded numTries = 0 while True: screenChanged = lastScreen != screen if screenChanged: lastScreen = screen redrawNeeded = True if isEink: display.drawFill(0x000000) display.flush() display.drawFill(0xffffff) pRedrawNeeded = redrawNeeded if screen == SCREEN_COUNTDOWN: dirCountdown() elif screen == SCREEN_EDIT_EVENT: dirEditEvent() elif screen == SCREEN_SELECT_EVENT: dirSelectEvent() elif screen == SCREEN_WIFI: dirWifi() else: screen = SCREEN_WIFI if redrawNeeded or pRedrawNeeded: display.flush(display.FLAG_LUT_FAST) else: time.sleep(0.05) loop()