Toggle Navigation
Hatchery
Eggs
MNIST handwritten clock
__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, os from .metadata import imageNumbers appName = system.currentApp() if appName and machine.nvs_getstr("system", "default_app") != appName: machine.nvs_setstr("system", "default_app", appName) # Set as default app try: directory = '/'.join(__file__.split('/')[:-1]) except: directory = '/lib/mnist_handwritten_clock' print('directory: ', directory) try: testFile = os.stat('{}/digits/9_{}.PNG'.format(directory, imageNumbers[9]-1)) except: easydraw.msg("Extracting image data, please wait..", "MNIST clock", True) easydraw.msg("(this can take a few minutes)") easydraw.display.flush(easydraw.display.FLAG_FULL) from . import extractData if extractData.unpackImages(): easydraw.msg("Done! restarting..") easydraw.display.flush(easydraw.display.FLAG_FULL) system.sleep(3000) else: easydraw.msg("Failed! Please remove and reinstall this app.") easydraw.msg("Restarting in 30s..") easydraw.display.flush(easydraw.display.FLAG_FULL) system.sleep(30000) 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(onlyIfNeeded=False) # Force NTP refresh time.sleep(5) 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) def randomDigit(number): return '{}/digits/{}_{}.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') random.seed(int(time.time())) 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