Skip to content

Instantly share code, notes, and snippets.

@dglaude
Last active October 22, 2022 08:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dglaude/4bf8d0a13c9c8ca8b05d6c0e9176bd20 to your computer and use it in GitHub Desktop.
Save dglaude/4bf8d0a13c9c8ca8b05d6c0e9176bd20 to your computer and use it in GitHub Desktop.
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)
@paulywill
Copy link

Do you think it's possible to rotate the projects every hour throughout the day?

@dglaude
Copy link
Author

dglaude commented Apr 1, 2021

@paulywill This "meta-project" only act at startup, and then hand the control to one of the "project" installed.
What you ask is not easy... there is no "clock" that would run when the magtag sleep, so you would have to check the time by connecting to the internet, maybe at each startup. This is time (sic) consuming and will make the "meta" a bit annoying.
Also, you need to make sure the project you run to wake up in a consistent way every now and then.
What you could do is:

  1. Change code at line 32+ to do this, it will at each restart, change to project +1
if pushed is not None:
    alarm.sleep_memory[0] = pushed
    print("Button pushed:", pushed)
elif alarm.sleep_memory[0] in range (0,5):
    alarm.sleep_memory[0] = alarm.sleep_memory[0] + 1 # Force the next project
if alarm.sleep_memory[0] not in range (0,5): # verify we have a valid value in the range
    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.
  1. Check the project code, make sure they sleep exactly the time you want and that way it will switch to the next one...

  2. Maybe you will have project that need frequent refresh... in witch case you would need a table that say how many refresh before changing project. Then you may want to have a table of the number of restart before changing. Like if the weather forcast restart every 10 minutes, but the other project restart every hour, then you could want to allow 6 run of the weather.
    So you could use a memory location such as alarm.sleep_memory[1] to store the number of remaining restart. and have a table app_restart_number = [6, 1, 1, 1, 1].
    You have to be careful on when to set the value, when to decrement the value and when reaching 0 switch project.

I hope this will work for you.

Maybe you want to implement a random project at each startup, that would be even more fun than always the same one after the same one.

You could check the official version of this project, as describe in the learn guide: https://learn.adafruit.com/adafruit-magtag-project-selector

And then suggest a change by adding a comment (you can link to this discussion). The author will be contacted, he is working for Adafruit... I am not. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment