Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Last active December 29, 2022 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenrobertson/d088d4341ea01fc251be3d2204d5d536 to your computer and use it in GitHub Desktop.
Save glenrobertson/d088d4341ea01fc251be3d2204d5d536 to your computer and use it in GitHub Desktop.
MatrixPortal M4 up/down switch to toggle filesystem read-only/write modes
# This script allows the MatrixPortal M4 to switch between:
# 1. readonly mode: write to drive when connected to computer over USB C (Down button)
# 2. write mode: CircuitPython can write to itself (Up button)
# Useful when developing code to pair Wifi networks.
# Think of "down" button as developer mode (code cannot write to file system)
# and "up" button as production: (code can write to file system)
#
# After hitting "reset" button twice, or reconnecting USB-C cable:
# neopixel will flash orange three times, then teal when accepting up/down button press
# blue indicates readonly mode
# green indicates write mode
import board
import busio
from digitalio import DigitalInOut, Pull
import displayio
import neopixel
import storage
import terminalio
import time
from adafruit_debouncer import Debouncer
LISTEN_TIMEOUT_SECONDS = 5
def listen_for_up_down_button_press():
pin_down = DigitalInOut(board.BUTTON_DOWN)
pin_down.switch_to_input(pull=Pull.UP)
button_down = Debouncer(pin_down)
pin_up = DigitalInOut(board.BUTTON_UP)
pin_up.switch_to_input(pull=Pull.UP)
button_up = Debouncer(pin_up)
status_light = neopixel.NeoPixel(
board.NEOPIXEL, 1, brightness=0.1
)
start_time = time.time()
status_light.fill((0, 150, 150))
status_light.show()
# down to toggle dev mode (readonly true)
# up to toggle circuitpython write access
while True:
button_down.update()
button_up.update()
if button_up.fell:
status_light.fill((0, 200, 0))
status_light.show()
print('remounting to readonly false')
storage.remount('/', readonly=False)
time.sleep(1)
status_light.fill((0, 0, 0))
break
elif button_down.fell:
print('remounting to readonly true')
status_light.fill((0, 0, 200))
status_light.show()
storage.remount('/', readonly=True)
time.sleep(1)
status_light.fill((0, 0, 0))
break
elif time.time() - start_time > LISTEN_TIMEOUT_SECONDS:
print('no up/down presses detected')
status_light.fill((0, 0, 0))
break
listen_for_up_down_button_press()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment