Skip to content

Instantly share code, notes, and snippets.

@dglaude

dglaude/code.py

Last active Dec 24, 2020
Embed
What would you like to do?
Change the MagTag program you want to run at startup.
# This file should be saved as code.py
# At MagTag startup (or at wakeup time), keep one one of the 4 button pressed.
# The MagTag will switch to the program you choose and remember that decision at each wakeup.
# The trick is to have for each button one CircuitPython program to import.
# You can customise the app_to_start list, here is the current setting:
# no button => magtag_weather
# 1 (D15) => magtag_showtimes
# 2 (D14) => magtag_tree
# 3 (D12) => magtag_spacex
# 4 (D11) => magtag_year_progress_percent
import alarm
import board
from digitalio import DigitalInOut, Direction, Pull
print("Checking if one of the button is pushed at startup.")
# Four button, but 5 applications to start, if no button is pressed then app[0] will be used.
buttons_pin = [board.D15, board.D14, board.D12, board.D11]
app_to_start = ['magtag_weather', 'magtag_showtimes', 'magtag_tree', 'magtag_spacex', 'magtag_year_progress_percent']
pushed=None
for idx, val in enumerate(buttons_pin):
btn = DigitalInOut(val)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
if not btn.value:
pushed = idx + 1 # Shift by one, zero as a special meaning
btn.deinit() # Pin must be release for MagTag library to use it.
if pushed is not None:
alarm.sleep_memory[0] = pushed
print("Button pushed:", pushed)
elif alarm.sleep_memory[0] not in range (0,5):
print("alarm.sleep_memory[0] forced to zero as it had unexpected value: ", alarm.sleep_memory[0])
alarm.sleep_memory[0] = 0 # Force 0 if the value is not expected.
# Start the application requested
import_me=app_to_start[alarm.sleep_memory[0]]
print("Starting ", import_me)
__import__(import_me)
@dglaude

This comment has been minimized.

Copy link
Owner Author

@dglaude dglaude commented Dec 20, 2020

Improvement:

  • a 5th application is possible when no button are pressed, that is the default when powering up
  • the applications to import are now into an array of 5 positions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment