/ MagTag_Project_Selector / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Eva Herrada for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Based on code written by @DavidGlaude on Twitter 6 # https://twitter.com/DavidGlaude/status/1340365817138044933 7 # https://gist.github.com/dglaude/4bf8d0a13c9c8ca8b05d6c0e9176bd20 8 9 import time 10 import alarm 11 import displayio 12 import board 13 import adafruit_imageload 14 from adafruit_display_shapes.rect import Rect 15 from adafruit_magtag.magtag import Graphics 16 from digitalio import DigitalInOut, Direction, Pull 17 18 projects = [ 19 "weather", 20 "spacex", 21 "covid", 22 "showerthoughts", 23 "tides", 24 "year", 25 "showtimes", 26 "slideshow", 27 ] 28 29 btnA = DigitalInOut(board.D15) 30 btnA.direction = Direction.INPUT 31 btnA.pull = Pull.UP 32 33 btnB = DigitalInOut(board.D14) 34 btnB.direction = Direction.INPUT 35 btnB.pull = Pull.UP 36 37 btnC = DigitalInOut(board.D12) 38 btnC.direction = Direction.INPUT 39 btnC.pull = Pull.UP 40 41 btnD = DigitalInOut(board.D11) 42 btnD.direction = Direction.INPUT 43 btnD.pull = Pull.UP 44 45 graphics = Graphics(auto_refresh=False) 46 display = graphics.display 47 group = displayio.Group() 48 49 selector = False 50 if not btnA.value or not btnB.value or not btnC.value or not btnD.value: 51 selector = True 52 53 if selector: 54 background = Rect(0, 0, 296, 128, fill=0xFFFFFF) 55 group.append(background) 56 for i in range(8): 57 sprite_sheet, palette = adafruit_imageload.load( 58 f"/bmps/{projects[i]}.bmp", 59 bitmap=displayio.Bitmap, 60 palette=displayio.Palette, 61 ) 62 sprite = displayio.TileGrid( 63 sprite_sheet, 64 pixel_shader=palette, 65 width=1, 66 height=1, 67 tile_width=62, 68 tile_height=54, 69 x=6 + 74 * (i % 4), 70 y=6 + 62 * (i // 4), 71 ) 72 group.append(sprite) 73 74 rect = Rect(4, 4, 66, 58, outline=0x000000, stroke=2) 75 group.append(rect) 76 display.show(group) 77 display.refresh() 78 79 time.sleep(5) 80 print("Ready") 81 selected = 0 82 while True: 83 if not btnA.value and not btnD.value: 84 alarm.sleep_memory[0] = selected 85 break 86 87 if not btnA.value and selected != 0 and selected != 4: 88 selected -= 1 89 rect.x -= 74 90 display.refresh() 91 print("left") 92 time.sleep(5) 93 continue 94 95 if not btnB.value and selected > 3: 96 selected -= 4 97 rect.y -= 62 98 display.refresh() 99 print("up") 100 time.sleep(5) 101 continue 102 103 if not btnC.value and selected < 4: 104 selected += 4 105 rect.y += 62 106 display.refresh() 107 print("down") 108 time.sleep(5) 109 continue 110 111 if not btnD.value and selected != 3 and selected != 7: 112 selected += 1 113 rect.x += 74 114 display.refresh() 115 print("right") 116 time.sleep(5) 117 continue 118 119 btnA.deinit() 120 btnB.deinit() 121 btnC.deinit() 122 btnD.deinit() 123 124 print("Starting ", projects[int(alarm.sleep_memory[0])]) 125 __import__("/projects/" + projects[int(alarm.sleep_memory[0])])