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