Skip to content

Commit 2d408ec

Browse files
authored
Merge pull request #1401 from adafruit/TheKitty-patch-1
Create README.md
2 parents 2bd09a5 + 7764297 commit 2d408ec

File tree

7 files changed

+366
-0
lines changed

7 files changed

+366
-0
lines changed

MagTag_Flashcards/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## MagTag Flashcards
2+
3+
Code accompanying the Adafruit Learning System Guide Language Flashcards on the MagTag
4+
5+
https://learn.adafruit.com/magtag-flashcards/overview

MagTag_Flashcards/basic/code.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import time
2+
import json
3+
import terminalio
4+
import digitalio
5+
import random
6+
from adafruit_magtag.magtag import MagTag
7+
8+
# Set up the magtag
9+
print("Magtag Basic Flashcards")
10+
magtag = MagTag()
11+
12+
# Import cards
13+
cards = {}
14+
with open("deck.json") as fp:
15+
cards = json.load(fp)
16+
17+
# Create a text area
18+
magtag.add_text(
19+
text_font="yasashi20.pcf",
20+
text_position=(
21+
magtag.graphics.display.width // 2,
22+
magtag.graphics.display.height // 2,
23+
),
24+
line_spacing=0.85,
25+
text_anchor_point=(0.5, 0.5),
26+
)
27+
28+
# Set up buttons
29+
cur_btn = False
30+
prev_btn = False
31+
32+
while True:
33+
# Shuffle the deck
34+
cards = sorted(cards, key=lambda _: random.random())
35+
for card in cards:
36+
37+
# Show the first side and wait for the D button
38+
text = ''.join(magtag.wrap_nicely(card[0], 20))
39+
magtag.set_text(text)
40+
while True:
41+
cur_btn = magtag.peripherals.button_d_pressed
42+
if cur_btn and not prev_btn:
43+
print("Show Result")
44+
time.sleep(0.1)
45+
break
46+
prev_btn = cur_btn
47+
48+
# Show the second side and wait for the D button
49+
text = '\n'.join(magtag.wrap_nicely(card[1], 11))
50+
text += '\n'
51+
text += '\n'.join(magtag.wrap_nicely(card[2], 20))
52+
print(text)
53+
magtag.set_text(text)
54+
while True:
55+
cur_btn = magtag.peripherals.button_d_pressed
56+
if cur_btn and not prev_btn:
57+
print("Next Card")
58+
time.sleep(0.1)
59+
break
60+
prev_btn = cur_btn

MagTag_Flashcards/basic/deck.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
["Monday","げつ ようび","Getsu yōbi"],
3+
["Tuesday","か ようび","Ka yōbi"],
4+
["Wednesday","すい ようび","Sui yōbi"],
5+
["Thursday","もく ようび","Moku yōbi"],
6+
["Friday","きん ようび","Kin yōbi"],
7+
["Saturday","ど ようび","Do yōbi"],
8+
["Sunday","にち ようび","Nichi yōbi"]
9+
]

MagTag_Flashcards/basic/yasashi20.pcf

149 KB
Binary file not shown.

MagTag_Flashcards/chapters/code.py

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
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))

MagTag_Flashcards/chapters/deck.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"Everyday Phrases":[
3+
["You're Welcome", "どういたしまして", "Dō Itashimashite"],
4+
["Good Morning", "おはよう ございます", "Ohayō gozaimasu"],
5+
["Yes", "はい", "Hai"],
6+
["No", "いいえ", "Iie"],
7+
["Hello", "こんにちは", "Konnichi wa"],
8+
["Please", "おねがい します", "Onegai Shimasu"],
9+
["Excuse Me", "すみません", "Sumimasen"],
10+
["Thank You", "ありがとう", "Arigatō"],
11+
],
12+
"Days of the Week":[
13+
["Monday","げつ ようび","Getsu yōbi"],
14+
["Tuesday","か ようび","Ka yōbi"],
15+
["Wednesday","すい ようび","Sui yōbi"],
16+
["Thursday","もく ようび","Moku yōbi"],
17+
["Friday","きん ようび","Kin yōbi"],
18+
["Saturday","ど ようび","Do yōbi"],
19+
["Sunday","にち ようび","Nichi yōbi"],
20+
],
21+
"Animals":[
22+
["Dog","いぬ","Inu"],
23+
["Cat","ねこ","Neko"],
24+
["Horse","うま","Uma"],
25+
["Monkey","さる","Saru"],
26+
["Elephant","ぞう",""],
27+
["Rabbit","うさぎ","Usagi"],
28+
]
29+
}
149 KB
Binary file not shown.

0 commit comments

Comments
 (0)