Simple test
Ensure your device works with this simple test.
examples/magtag_simpletest.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4import time
5
6from adafruit_magtag.magtag import MagTag
7
8magtag = MagTag()
9
10magtag.add_text(
11 text_position=(
12 50,
13 (magtag.graphics.display.height // 2) - 1,
14 ),
15 text_scale=3,
16)
17
18magtag.set_text("Hello World")
19
20button_colors = ((255, 0, 0), (255, 150, 0), (0, 255, 255), (180, 0, 255))
21button_tones = (1047, 1318, 1568, 2093)
22
23while True:
24 for i, b in enumerate(magtag.peripherals.buttons):
25 if not b.value:
26 print(f"Button {chr(ord('A') + i)} pressed")
27 magtag.peripherals.neopixel_disable = False
28 magtag.peripherals.neopixels.fill(button_colors[i])
29 magtag.peripherals.play_tone(button_tones[i], 0.25)
30 break
31 else:
32 magtag.peripherals.neopixel_disable = True
33 time.sleep(0.01)
Other Demos
examples/magtag_quote_demo.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2#
3# SPDX-License-Identifier: Unlicense
4import os
5
6from adafruit_magtag.magtag import MagTag
7
8# Set up where we'll be fetching data from
9DATA_SOURCE = "https://www.adafruit.com/api/quotes.php"
10QUOTE_LOCATION = [0, "text"]
11AUTHOR_LOCATION = [0, "author"]
12
13# the current working directory (where this file is)
14try:
15 cwd = os.path.dirname(os.path.realpath(__file__))
16except AttributeError:
17 cwd = ("/" + __file__).rsplit("/", 1)[0]
18
19magtag = MagTag(
20 url=DATA_SOURCE,
21 json_path=(QUOTE_LOCATION, AUTHOR_LOCATION),
22 default_bg=0x000000,
23)
24
25
26def add_quote_marks(quote_text):
27 return f'"{quote_text}"'
28
29
30def add_hyphen(author_name):
31 return f"- {author_name}"
32
33
34magtag.add_text(
35 text_position=(
36 (magtag.graphics.display.width // 2) - 1,
37 (magtag.graphics.display.height // 2) - 20,
38 ),
39 text_color=0xFFFFFF,
40 text_wrap=44,
41 text_maxlen=180,
42 line_spacing=1.0,
43 text_transform=add_quote_marks,
44 text_anchor_point=(0.5, 0.5),
45)
46
47magtag.add_text(
48 text_position=(
49 (magtag.graphics.display.width // 2) - 120,
50 (magtag.graphics.display.height // 2) + 34,
51 ),
52 text_color=0xFFFFFF,
53 text_wrap=0,
54 text_maxlen=30,
55 text_transform=add_hyphen,
56)
57
58try:
59 value = magtag.fetch()
60 print("Response is", value)
61except (ValueError, RuntimeError) as e:
62 print("Some error occured, retrying! -", e)
63magtag.exit_and_deep_sleep(60)
Deep Sleep and wake up from Button press.
examples/magtag_btn_sleep_demo.py
1# SPDX-FileCopyrightText: 2023 Tim Cocks
2#
3# SPDX-License-Identifier: Unlicense
4
5import time
6
7import alarm
8import board
9
10from adafruit_magtag.magtag import MagTag
11
12IDLE_TIMEOUT = 10 # seconds idle, then sleep
13
14magtag = MagTag()
15
16magtag.add_text(
17 text_position=(
18 50,
19 (magtag.graphics.display.height // 2) - 1,
20 ),
21 text_scale=3,
22)
23
24magtag.set_text("Awake")
25
26button_colors = ((255, 0, 0), (255, 150, 0), (0, 255, 255), (180, 0, 255))
27button_tones = (1047, 1318, 1568, 2093)
28
29now = time.monotonic()
30last_action_time = now
31while True:
32 now = time.monotonic()
33 if now - last_action_time >= 10.0:
34 magtag.set_text("Sleeping")
35 magtag.peripherals.deinit()
36 time.sleep(2)
37 # go to sleep
38 pin_alarm = alarm.pin.PinAlarm(pin=board.D11, value=False, pull=True)
39
40 # Exit the program, and then deep sleep until the alarm wakes us.
41 alarm.exit_and_deep_sleep_until_alarms(pin_alarm)
42
43 for i, b in enumerate(magtag.peripherals.buttons):
44 if not b.value:
45 print(f"Button {chr(ord('A') + i)} pressed")
46 last_action_time = now
47 magtag.peripherals.neopixel_disable = False
48 magtag.peripherals.neopixels.fill(button_colors[i])
49 magtag.peripherals.play_tone(button_tones[i], 0.25)
50 break
51 else:
52 magtag.peripherals.neopixel_disable = True
53 time.sleep(0.01)