Toggle Navigation
Hatchery
Eggs
MNIST handwritten clock old
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import display, wifi, time, random, system, easydraw, machine if machine.nvs_getstr("system", "default_app") != "mnist_handwritten_clock": machine.nvs_setstr("system", "default_app", "mnist_handwritten_clock") # Set as default app wifi.connect() if (not wifi.wait(20)): easydraw.msg("Could not connect to WiFi!", "MNIST clock", True) easydraw.msg("Retrying in 10 seconds..") easydraw.display.flush(easydraw.display.FLAG_FULL) system.sleep(10000) wifi.ntp() if time.localtime()[4] % 20 == 0: wifi.ntp(onlyIfNeeded=False) # Force NTP refresh time.sleep(5) # necessary? random.seed(int(time.time())) imageNumbers = [85, 126, 116, 107, 110, 87, 87, 99, 89, 94] image_height = 84 image_width = 60 colon_width = 21 spacing = int((display.width() - 4 * image_width - colon_width)/4) y = int((display.height() - image_height)/2) try: directory = '/'.join(__file__.split('/')[:-1]) except: directory = '/' #directory = '/lib/mnist_handwritten_clock' def randomDigit(number): return '{}/{}_{}.PNG'.format(directory, number, random.randrange(imageNumbers[number])) def printTime(hours, minutes): digits = [int(hours / 10 % 10), int(hours % 10), int(minutes / 10 % 10), int(minutes % 10)] try: display.drawPng(0, y, randomDigit(digits[0])) display.drawPng(image_width + spacing, y, randomDigit(digits[1])) display.drawPng(2 * (image_width + spacing), y, '{}/colon.png'.format(directory)) second_start = 2 * image_width + colon_width + 3 * spacing display.drawPng(second_start, y, randomDigit(digits[2])) display.drawPng(second_start + image_width + spacing, y, randomDigit(digits[3])) except: display.drawFill(0xFFFFFF) textWidth = display.getTextWidth('12 : 34', 'PermanentMarker36') display.drawText(int((display.width() - textWidth)/2), int((display.height() - 36)/2), '{}{} : {}{}'.format(*digits), 0x000000, 'PermanentMarker36') display.drawText(0,0, 'font error') curTime = time.localtime() display.drawFill(0xFFFFFF) printTime(curTime[3], curTime[4]) display.flush(display.FLAG_LUT_GREYSCALE) sleepTime = 1000 * (60 - time.localtime()[5]) system.sleep(sleepTime) # Sleep until next minute