- Code: Select all | TOGGLE FULL SIZE
import random
import board
from adafruit_magtag.magtag import MagTag
import alarm
from alarm.pin import PinAlarm
DISPLAY_NUMBER = 0
DISPLAY_LETTER = 1
magtag = MagTag()
display_type = DISPLAY_LETTER
if alarm.wake_alarm:
magtag.peripherals.neopixel_disable = False
magtag.peripherals.neopixels.fill((0, 255, 0))
songs = ( # Wheels on the Bus
((147, 2), (196, 1.5), (196, .5), (196, 1.5), (196, .5), (196, 2),
(247, 2), (294, 2), (247, 2), (196, 2)),
# Twinkle Twinkle
((196, 2), (196, 2), (294, 2), (294, 2), (330, 2), (330, 2), (294, 4),
#(262, 2), (262, 2), (220, 2), (220, 2), (196, 4)
),
)
song = random.choice(songs)
for notepair in song:
magtag.peripherals.play_tone(notepair[0], notepair[1] * 0.2)
if alarm.wake_alarm.pin == board.BUTTON_B:
display_type = DISPLAY_NUMBER
magtag.peripherals.neopixel_disable = True
magtag.peripherals.speaker_disable = True
if display_type == DISPLAY_LETTER:
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
display_str = random.choice(LETTERS)
else:
display_str = str(random.randint(0, 99))
magtag.add_text(
text_font = "Merriweather-100.bdf",
text_position=(
magtag.graphics.display.width // 2,
magtag.graphics.display.height // 2,
),
text_scale = 0.75,
line_spacing = 1,
text_anchor_point=(0.5, 0.5),
)
magtag.set_text(display_str, 0)
magtag.peripherals.buttons[0].deinit()
magtag.peripherals.buttons[1].deinit()
alarm.exit_and_deep_sleep_until_alarms(
PinAlarm(pin=board.BUTTON_A, value=False, pull=True),
PinAlarm(pin=board.BUTTON_B, value=False, pull=True)
)
If button B is pressed, a random integer from 0 to 99 is displayed instead.
Potential improvements: shuffle the list of letters on first boot, and store them along with an index in sleep memory so all letters are eventually shown before any repeats. (Might be sufficient to store the random seed and re-shuffle with that seed each boot.)