/ MagTag_Christmas_Countdown / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Liz Clark for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import displayio 7 from adafruit_magtag.magtag import MagTag 8 from adafruit_display_shapes.circle import Circle 9 10 # create MagTag and connect to network 11 try: 12 magtag = MagTag() 13 magtag.network.connect() 14 except (ConnectionError, ValueError, RuntimeError) as e: 15 print("*** MagTag(), Some error occured, retrying! -", e) 16 # Exit program and restart in 1 seconds. 17 magtag.exit_and_deep_sleep(1) 18 19 20 21 # displayio groups 22 group = displayio.Group() 23 tree_group = displayio.Group() 24 circle_group = displayio.Group() 25 26 # import tree bitmap 27 filename = "/atree.bmp" 28 29 # CircuitPython 6 & 7 compatible 30 tree = displayio.OnDiskBitmap(open(filename, "rb")) 31 tree_grid = displayio.TileGrid( 32 tree, pixel_shader=getattr(tree, 'pixel_shader', displayio.ColorConverter()) 33 ) 34 35 # # CircuitPython 7+ compatible 36 # tree = displayio.OnDiskBitmap(filename) 37 # tree_grid = displayio.TileGrid(tree, pixel_shader=tree.pixel_shader) 38 39 # add bitmap to its group 40 tree_group.append(tree_grid) 41 # add tree group to the main group 42 group.append(tree_group) 43 44 # list of circle positions 45 spots = ( 46 (246, 53), 47 (246, 75), 48 (206, 42), 49 (206, 64), 50 (206, 86), 51 (176, 31), 52 (176, 53), 53 (176, 75), 54 (176, 97), 55 (136, 42), 56 (136, 64), 57 (136, 86), 58 (106, 31), 59 (106, 53), 60 (106, 75), 61 (106, 97), 62 (66, 31), 63 (66, 53), 64 (66, 75), 65 (66, 97), 66 (36, 20), 67 (36, 42), 68 (36, 64), 69 (36, 86), 70 (36, 108) 71 ) 72 73 # circles to cover-up bitmap's number ornaments 74 75 ball_color = [0x555555, 0xaaaaaa, 0xFFFFFF] # All colors except black (0x000000) 76 ball_index = 0 77 78 # creating the circles & pulling in positions from spots 79 for spot in spots: 80 circle = Circle(x0=spot[0], y0=spot[1], r=11, fill=ball_color[ball_index]) # Each ball has a color 81 ball_index += 1 82 ball_index %= len(ball_color) 83 84 # adding circles to their display group 85 circle_group.append(circle) 86 87 # adding circles group to main display group 88 group.append(circle_group) 89 90 # grabs time from network 91 magtag.get_local_time() 92 # parses time into month, date, etc 93 now = time.localtime() 94 month = now[1] 95 day = now[2] 96 (hour, minutes, seconds) = now[3:6] 97 seconds_since_midnight = 60 * (hour*60 + minutes)+seconds 98 print( f"day is {day}, ({seconds_since_midnight} seconds since midnight)") 99 100 101 # sets colors of circles to transparent to reveal dates that have passed & current date 102 for i in range(day): 103 circle_group[i].fill = None 104 time.sleep(0.1) 105 106 # updates display with bitmap and current circle colors 107 magtag.display.show(group) 108 magtag.display.refresh() 109 time.sleep(5) 110 111 # goes into deep sleep till a 'stroke' past midnight 112 print("entering deep sleep") 113 seconds_to_sleep = 24*60*60 - seconds_since_midnight + 10 114 print( f"sleeping for {seconds_to_sleep} seconds") 115 magtag.exit_and_deep_sleep(seconds_to_sleep) 116 117 # entire code will run again after deep sleep cycle 118 # similar to hitting the reset button