Skip to content

Commit 48af295

Browse files
authored
Merge pull request #39 from FoamyGuy/add_magtag_support
Add magtag support
2 parents 8f9a28c + c62c646 commit 48af295

File tree

7 files changed

+247
-11
lines changed

7 files changed

+247
-11
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Introduction
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_PyBadger/actions/
1414
:alt: Build Status
1515

16-
Badge-focused CircuitPython helper library for PyBadge, PyBadge LC, PyGamer and CLUE.
16+
Badge-focused CircuitPython helper library for PyBadge, PyBadge LC, PyGamer, CLUE, and Mag Tag.
1717

1818

1919
Dependencies

adafruit_pybadger/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@
3737
from .pyportal import pyportal as pybadger
3838
elif "Circuit Playground Bluefruit" in os.uname().machine:
3939
from .cpb_gizmo import cpb_gizmo as pybadger
40+
elif "MagTag with ESP32S2" in os.uname().machine:
41+
from .mag_tag import mag_tag as pybadger

adafruit_pybadger/mag_tag.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2020 Tim C for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_pybadger.mag_tag`
24+
================================================================================
25+
26+
Badge-focused CircuitPython helper library for Mag Tag.
27+
28+
29+
* Author(s): Kattni Rembor, Tim C
30+
31+
Implementation Notes
32+
--------------------
33+
34+
**Hardware:**
35+
36+
* `Adafruit Mag Tag <https://www.adafruit.com/product/4800>`_
37+
38+
**Software and Dependencies:**
39+
40+
* Adafruit CircuitPython firmware for the supported boards:
41+
https://github.com/adafruit/circuitpython/releases
42+
43+
"""
44+
45+
from collections import namedtuple
46+
import board
47+
48+
# import digitalio
49+
# from gamepad import GamePad
50+
import neopixel
51+
from adafruit_pybadger.pybadger_base import PyBadgerBase
52+
53+
__version__ = "0.0.0-auto.0"
54+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git"
55+
56+
Buttons = namedtuple("Buttons", "a b c d")
57+
58+
59+
class MagTag(PyBadgerBase):
60+
"""Class that represents a single Mag Tag."""
61+
62+
_neopixel_count = 4
63+
64+
def __init__(self):
65+
super().__init__()
66+
67+
# NeoPixels
68+
self._neopixels = neopixel.NeoPixel(
69+
board.NEOPIXEL, self._neopixel_count, brightness=1, pixel_order=neopixel.GRB
70+
)
71+
72+
# self._buttons = GamePad(
73+
# ,
74+
# digitalio.DigitalInOut(board.BUTTON_B),
75+
# digitalio.DigitalInOut(board.BUTTON_C),
76+
# digitalio.DigitalInOut(board.BUTTON_D),
77+
# )
78+
79+
@property
80+
def button(self):
81+
"""The buttons on the board.
82+
83+
Example use:
84+
85+
.. code-block:: python
86+
87+
from adafruit_pybadger import pybadger
88+
89+
while True:
90+
if pybadger.button.a:
91+
print("Button A")
92+
elif pybadger.button.b:
93+
print("Button B")
94+
"""
95+
# button_values = self._buttons.get_pressed()
96+
# return Buttons(
97+
# button_values & PyBadgerBase.BUTTON_B, button_values & PyBadgerBase.BUTTON_A,
98+
# button_values & PyBadgerBase.BUTTON_START, button_values & PyBadgerBase.BUTTON_SELECT
99+
# )
100+
101+
@property
102+
def _unsupported(self):
103+
"""This feature is not supported on Mag Tag."""
104+
raise NotImplementedError("This feature is not supported on Mag Tag.")
105+
106+
# The following is a list of the features available in other PyBadger modules but
107+
# not available for Mag Tag. If called while using a Mag Tag, they will result in the
108+
# NotImplementedError raised in the property above.
109+
play_file = _unsupported
110+
light = _unsupported
111+
acceleration = _unsupported
112+
button = _unsupported
113+
114+
115+
mag_tag = MagTag() # pylint: disable=invalid-name
116+
"""Object that is automatically created on import."""

adafruit_pybadger/pybadger_base.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,26 @@
5050
import board
5151
from micropython import const
5252
import digitalio
53-
54-
try:
55-
import audiocore
56-
except ImportError:
57-
import audioio as audiocore
5853
from adafruit_bitmap_font import bitmap_font
5954
import displayio
6055
from adafruit_display_shapes.rect import Rect
6156
from adafruit_display_text import label
6257
import terminalio
6358
import adafruit_miniqr
6459

60+
AUDIO_ENABLED = False
61+
try:
62+
import audiocore
63+
64+
AUDIO_ENABLED = True
65+
except ImportError:
66+
try:
67+
import audioio as audiocore
68+
69+
AUDIO_ENABLED = True
70+
except ImportError:
71+
# Allow to work with no audio
72+
pass
6573
__version__ = "0.0.0-auto.0"
6674
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git"
6775

@@ -685,11 +693,17 @@ def _sine_sample(length):
685693
yield int(tone_volume * math.sin(2 * math.pi * (i / length)) + shift)
686694

687695
def _generate_sample(self, length=100):
688-
if self._sample is not None:
689-
return
690-
self._sine_wave = array.array("H", PyBadgerBase._sine_sample(length))
691-
self._sample = self._audio_out(board.SPEAKER) # pylint: disable=not-callable
692-
self._sine_wave_sample = audiocore.RawSample(self._sine_wave)
696+
if AUDIO_ENABLED:
697+
if self._sample is not None:
698+
return
699+
self._sine_wave = array.array("H", PyBadgerBase._sine_sample(length))
700+
# pylint: disable=not-callable
701+
self._sample = self._audio_out(
702+
board.SPEAKER
703+
) # pylint: disable=not-callable
704+
self._sine_wave_sample = audiocore.RawSample(self._sine_wave)
705+
else:
706+
print("Required audio modules were missing")
693707

694708
def _enable_speaker(self, enable):
695709
if not hasattr(board, "SPEAKER_ENABLE"):

examples/Blinka_MagTag.bmp

38.1 KB
Binary file not shown.

examples/Blinka_MagTag.bmp.license

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: BLINKA is a registered trademark of Adafruit, LLC. The design is exclusive property of Adafruit, LLC
2+
#
3+
# SPDX-License-Identifier: MIT
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""Simpletest example using the Mag Tag.
2+
Use the A, B, and C buttons to change between examples."""
3+
import time
4+
import board
5+
import digitalio
6+
from adafruit_pybadger import pybadger
7+
8+
9+
def try_refresh():
10+
""" Attempt to refresh the display. Catch 'refresh too soon' error
11+
and retry after waiting 10 seconds.
12+
"""
13+
try:
14+
board.DISPLAY.refresh()
15+
except RuntimeError as too_soon_error:
16+
# catch refresh too soon
17+
print(too_soon_error)
18+
print("waiting before retry refresh()")
19+
time.sleep(10)
20+
board.DISPLAY.refresh()
21+
22+
23+
print("wait before anything")
24+
time.sleep(5)
25+
26+
btn_a = digitalio.DigitalInOut(board.BUTTON_A)
27+
btn_a.direction = digitalio.Direction.INPUT
28+
btn_a.pull = digitalio.Pull.UP
29+
30+
btn_b = digitalio.DigitalInOut(board.BUTTON_B)
31+
btn_b.direction = digitalio.Direction.INPUT
32+
btn_b.pull = digitalio.Pull.UP
33+
34+
btn_c = digitalio.DigitalInOut(board.BUTTON_C)
35+
btn_c.direction = digitalio.Direction.INPUT
36+
btn_c.pull = digitalio.Pull.UP
37+
38+
prev_a = btn_a.value
39+
prev_b = btn_b.value
40+
prev_c = btn_c.value
41+
42+
SHOWING = "badge"
43+
44+
pybadger.show_badge(
45+
name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3
46+
)
47+
48+
try_refresh()
49+
50+
print("after show, going to loop")
51+
52+
neopixel_pwr = digitalio.DigitalInOut(board.NEOPIXEL_POWER)
53+
neopixel_pwr.direction = digitalio.Direction.OUTPUT
54+
neopixel_pwr.value = False
55+
pybadger.pixels.fill(0x000022)
56+
57+
while True:
58+
59+
cur_a = btn_a.value
60+
cur_b = btn_b.value
61+
cur_c = btn_c.value
62+
63+
if prev_a and not cur_a:
64+
pybadger.pixels.fill(0x000000)
65+
neopixel_pwr.value = True
66+
if SHOWING != "badge":
67+
print("changing to badge")
68+
SHOWING = "badge"
69+
pybadger.show_badge(
70+
name_string="Mag Tag", hello_scale=2, my_name_is_scale=2, name_scale=3
71+
)
72+
try_refresh()
73+
74+
if prev_b and not cur_b:
75+
pybadger.pixels.fill(0x000000)
76+
neopixel_pwr.value = True
77+
if SHOWING != "qr":
78+
print("changing to qr")
79+
SHOWING = "qr"
80+
pybadger.show_qr_code(data="https://www.adafruit.com/product/4800")
81+
try_refresh()
82+
83+
if prev_c and not cur_c:
84+
pybadger.pixels.fill(0x000000)
85+
neopixel_pwr.value = True
86+
if SHOWING != "card":
87+
print("changing to card")
88+
SHOWING = "card"
89+
pybadger.show_business_card(
90+
image_name="Blinka_MagTag.bmp",
91+
name_string="Blinka",
92+
name_scale=2,
93+
email_string_one="blinka@",
94+
email_string_two="adafruit.com",
95+
)
96+
# show_business_card() calls refresh() internally
97+
98+
prev_a = cur_a
99+
prev_b = cur_b
100+
prev_c = cur_c
101+
time.sleep(1)

0 commit comments

Comments
 (0)