|
| 1 | +import time |
| 2 | +import json |
| 3 | +import terminalio |
| 4 | +import digitalio |
| 5 | +import random |
| 6 | +from adafruit_display_shapes.rect import Rect |
| 7 | +from adafruit_magtag.magtag import MagTag |
| 8 | +magtag = MagTag() |
| 9 | + |
| 10 | +# --------------------------------- |
| 11 | +# Prepare text regions |
| 12 | +# --------------------------------- |
| 13 | + |
| 14 | +# Fetch list of chapters |
| 15 | +MAX_LLEN = 8 |
| 16 | +data = {} |
| 17 | +with open("deck.json") as fp: |
| 18 | + data = json.load(fp) |
| 19 | +chap_list = list(data.keys()) |
| 20 | +num_chap = len(chap_list) |
| 21 | +list_len = min(num_chap,MAX_LLEN) |
| 22 | + |
| 23 | +# Print list of chapters |
| 24 | +for i in range(list_len): |
| 25 | + magtag.add_text( |
| 26 | + text_font=terminalio.FONT, |
| 27 | + text_position=(10, 3+(i*10)), |
| 28 | + line_spacing=1.0, |
| 29 | + text_anchor_point=(0, 0), # Top left |
| 30 | + is_data=False, # Text will be set manually |
| 31 | + ) |
| 32 | + if i == 0: |
| 33 | + magtag.set_text("> " + chap_list[i], i, auto_refresh=False) |
| 34 | + else: |
| 35 | + magtag.set_text(" " + chap_list[i], i, auto_refresh=False) |
| 36 | + |
| 37 | +# Add button labels at the bottom of the screen |
| 38 | +BUTTON_TEXT_IDX = list_len |
| 39 | +magtag.graphics.splash.append(Rect(0, magtag.graphics.display.height - 14, |
| 40 | + magtag.graphics.display.width, |
| 41 | + magtag.graphics.display.height, fill=0x0)) |
| 42 | +magtag.add_text( |
| 43 | + text_font=terminalio.FONT, |
| 44 | + text_position=(3, magtag.graphics.display.height - 14), |
| 45 | + text_color=0xFFFFFF, |
| 46 | + line_spacing=1.0, |
| 47 | + text_anchor_point=(0, 0), # Top left |
| 48 | + is_data=False, # Text will be set manually |
| 49 | +) |
| 50 | +magtag.set_text("Select Up Down Begin", |
| 51 | + BUTTON_TEXT_IDX, auto_refresh=False) |
| 52 | + |
| 53 | +# Add message label at the top of the screen |
| 54 | +MSG_TEXT_IDX = list_len + 1 |
| 55 | +magtag.add_text( |
| 56 | + text_font=terminalio.FONT, |
| 57 | + text_position=(3, magtag.graphics.display.height - 30), |
| 58 | + line_spacing=1.0, |
| 59 | + text_anchor_point=(0, 0), # Top left |
| 60 | + is_data=False, # Text will be set manually |
| 61 | +) |
| 62 | +magtag.set_text("Press Begin to default to all chapters", MSG_TEXT_IDX) |
| 63 | + |
| 64 | +# Empty text region for card displays |
| 65 | +CARD_TEXT_IDX = list_len + 2 |
| 66 | +magtag.add_text( |
| 67 | + text_font="yasashi20.pcf", |
| 68 | + text_position=( |
| 69 | + magtag.graphics.display.width // 2, |
| 70 | + magtag.graphics.display.height // 2, |
| 71 | + ), |
| 72 | + line_spacing=0.85, |
| 73 | + text_anchor_point=(0.5, 0.5), |
| 74 | +) |
| 75 | + |
| 76 | +# Button management |
| 77 | +curr_btns = [False] * 4 |
| 78 | +prev_btns = [False] * 4 |
| 79 | +BTN_A = 0 |
| 80 | +BTN_B = 1 |
| 81 | +BTN_C = 2 |
| 82 | +BTN_D = 3 |
| 83 | +def update_button(idx, pressed): |
| 84 | + curr_btns[idx] = pressed |
| 85 | + if curr_btns[idx] and not prev_btns[idx]: |
| 86 | + print("Exit menu") |
| 87 | + return True |
| 88 | + prev_btns[idx] = curr_btns[idx] |
| 89 | + return False |
| 90 | + |
| 91 | +# Cursor settings |
| 92 | +cursor_pos = 0 |
| 93 | +list_offset = 0 |
| 94 | +selected = [False] * num_chap |
| 95 | +btn_updated = False |
| 96 | + |
| 97 | +# --------------------------------- |
| 98 | +# Program Loop |
| 99 | +# --------------------------------- |
| 100 | + |
| 101 | +while True: |
| 102 | + |
| 103 | + # --------------------------------- |
| 104 | + # Chapter Select |
| 105 | + # --------------------------------- |
| 106 | + |
| 107 | + while True: |
| 108 | + if btn_updated: |
| 109 | + # Clear default message only when items are selected |
| 110 | + if any(selected): |
| 111 | + magtag.set_text("", MSG_TEXT_IDX, auto_refresh=False) |
| 112 | + else: |
| 113 | + magtag.set_text("Press Begin to default to all chapters", |
| 114 | + MSG_TEXT_IDX, auto_refresh=False) |
| 115 | + |
| 116 | + magtag.peripherals.neopixels.fill((128, 0, 0)) |
| 117 | + for i in range(list_len): |
| 118 | + prefix = "" |
| 119 | + if i == cursor_pos: |
| 120 | + prefix += ">" |
| 121 | + else: |
| 122 | + prefix += " " |
| 123 | + if selected[i + list_offset]: |
| 124 | + prefix += "*" |
| 125 | + else: |
| 126 | + prefix += " " |
| 127 | + magtag.set_text(prefix + chap_list[i+list_offset], |
| 128 | + i, auto_refresh=False) |
| 129 | + magtag.refresh() |
| 130 | + magtag.peripherals.neopixels.fill((0, 0, 0)) |
| 131 | + btn_updated = False |
| 132 | + # UP |
| 133 | + if update_button(BTN_B, magtag.peripherals.button_b_pressed): |
| 134 | + cursor_pos -= 1 |
| 135 | + btn_updated = True |
| 136 | + # DOWN |
| 137 | + if update_button(BTN_C, magtag.peripherals.button_c_pressed): |
| 138 | + cursor_pos += 1 |
| 139 | + btn_updated = True |
| 140 | + # SELECT |
| 141 | + if update_button(BTN_A, magtag.peripherals.button_a_pressed): |
| 142 | + selected[cursor_pos + list_offset] = not selected[cursor_pos + list_offset] |
| 143 | + btn_updated = True |
| 144 | + # BEGIN |
| 145 | + if update_button(BTN_D, magtag.peripherals.button_d_pressed): |
| 146 | + # if nothing was selected, default to all decks |
| 147 | + magtag.peripherals.neopixels.fill((128, 0, 0)) |
| 148 | + if not any(selected): |
| 149 | + selected = [True] * list_len |
| 150 | + break |
| 151 | + # detect if you're past the list bounds |
| 152 | + if cursor_pos == MAX_LLEN: |
| 153 | + cursor_pos = MAX_LLEN - 1 |
| 154 | + if (num_chap - list_offset - 1) > MAX_LLEN: |
| 155 | + list_offset += 1 |
| 156 | + |
| 157 | + if cursor_pos == -1: |
| 158 | + cursor_pos = 0 |
| 159 | + if list_offset > 0: |
| 160 | + list_offset -= 1 |
| 161 | + |
| 162 | + # --------------------------------- |
| 163 | + # Deck Loop |
| 164 | + # --------------------------------- |
| 165 | + |
| 166 | + # Clear the menu and message box |
| 167 | + for i in range(list_len): |
| 168 | + magtag.set_text("", i, auto_refresh=False) |
| 169 | + magtag.set_text("", MSG_TEXT_IDX,auto_refresh=False) |
| 170 | + |
| 171 | + # Grab the cards from the chapters we want, and shuffle them |
| 172 | + cards = [] |
| 173 | + for i in range(len(selected)): |
| 174 | + if selected[i]: |
| 175 | + cards.extend(data[chap_list[i]]) |
| 176 | + cards = sorted(cards, key=lambda _: random.random()) |
| 177 | + |
| 178 | + # make a separate holding deck for cards the user gets wrong |
| 179 | + forgotten_cards = [] |
| 180 | + |
| 181 | + exit_called = False |
| 182 | + while True: |
| 183 | + for card in cards: |
| 184 | + magtag.set_text("Exit -- -- Turn Over", |
| 185 | + BUTTON_TEXT_IDX,auto_refresh=False) |
| 186 | + text = '\n'.join(magtag.wrap_nicely(card[0], 11)) |
| 187 | + magtag.set_text(text, CARD_TEXT_IDX) |
| 188 | + magtag.peripherals.neopixels.fill((0, 0, 0)) |
| 189 | + |
| 190 | + while True: |
| 191 | + # EXIT |
| 192 | + if update_button(BTN_A, magtag.peripherals.button_a_pressed): |
| 193 | + exit_called = True |
| 194 | + break |
| 195 | + # TURN |
| 196 | + if update_button(BTN_D, magtag.peripherals.button_d_pressed): |
| 197 | + break |
| 198 | + magtag.peripherals.neopixels.fill((128, 0, 0)) |
| 199 | + if exit_called: |
| 200 | + break |
| 201 | + |
| 202 | + magtag.set_text("Exit -- Forgot Good", |
| 203 | + BUTTON_TEXT_IDX,auto_refresh=False) |
| 204 | + text = '\n'.join(magtag.wrap_nicely(card[1], 11)) |
| 205 | + text += '\n' |
| 206 | + text += '\n'.join(magtag.wrap_nicely(card[2], 20)) |
| 207 | + magtag.set_text(text, CARD_TEXT_IDX) |
| 208 | + magtag.peripherals.neopixels.fill((0, 0, 0)) |
| 209 | + |
| 210 | + while True: |
| 211 | + # EXIT |
| 212 | + if update_button(BTN_A, magtag.peripherals.button_a_pressed): |
| 213 | + exit_called = True |
| 214 | + break |
| 215 | + # FORGOT |
| 216 | + if update_button(BTN_C, magtag.peripherals.button_c_pressed): |
| 217 | + forgotten_cards.append(card) |
| 218 | + break |
| 219 | + # GOOD |
| 220 | + if update_button(BTN_D, magtag.peripherals.button_d_pressed): |
| 221 | + break |
| 222 | + magtag.peripherals.neopixels.fill((128, 0, 0)) |
| 223 | + if exit_called: |
| 224 | + break |
| 225 | + # Next card |
| 226 | + # If there were forgotten cards, make them the new deck and restart |
| 227 | + if forgotten_cards: |
| 228 | + cards = forgotten_cards |
| 229 | + forgotten_cards = [] |
| 230 | + else: |
| 231 | + break |
| 232 | + |
| 233 | + # --------------------------------- |
| 234 | + # Complete and Reset |
| 235 | + # --------------------------------- |
| 236 | + |
| 237 | + # Show completion text if deck was finished |
| 238 | + if not exit_called: |
| 239 | + magtag.set_text("-- -- -- --", |
| 240 | + BUTTON_TEXT_IDX,auto_refresh=False) |
| 241 | + magtag.set_text("Complete!", CARD_TEXT_IDX) |
| 242 | + else: |
| 243 | + exit_called = False |
| 244 | + |
| 245 | + # Clear and reprint list of chapters |
| 246 | + magtag.set_text("", CARD_TEXT_IDX, auto_refresh=False) |
| 247 | + for i in range(list_len): |
| 248 | + if i == 0: |
| 249 | + magtag.set_text("> " + chap_list[i], i, auto_refresh=False) |
| 250 | + else: |
| 251 | + magtag.set_text(" " + chap_list[i], i, auto_refresh=False) |
| 252 | + magtag.set_text("Select Up Down Begin", |
| 253 | + BUTTON_TEXT_IDX, auto_refresh=False) |
| 254 | + magtag.set_text("Press Begin to default to all chapters", MSG_TEXT_IDX) |
| 255 | + |
| 256 | + # Reset cursor: |
| 257 | + cursor_pos = 0 |
| 258 | + list_offset = 0 |
| 259 | + selected = [False] * list_len |
| 260 | + btn_updated = False |
| 261 | + |
| 262 | + # Done resetting, return to chapter selection |
| 263 | + magtag.peripherals.neopixels.fill((0, 0, 0)) |
0 commit comments