# SPDX-License-Identifier: MIT
"""
boot.py - runs once at power-on, before code.py.

Enables a second USB serial channel (usb_cdc.data) so the web config app can talk
to the MacroPad without fighting the REPL, and gives the device write access to its
own filesystem so it can persist config.json.

ESCAPE HATCH: hold KEY1 (top-left key) while plugging in / resetting to KEEP the
CIRCUITPY drive writable from your computer (needed to edit code from the Mac).
When KEY1 is NOT held, the device owns the filesystem and can save pushed configs.
"""

import board
import digitalio
import storage
import usb_cdc

# console = REPL, data = dedicated channel the web app uses
usb_cdc.enable(console=True, data=True)

key1 = digitalio.DigitalInOut(board.KEY1)
key1.switch_to_input(pull=digitalio.Pull.UP)

# Pull-up means value is True when NOT pressed.
if key1.value:
    # KEY1 not held -> device owns the filesystem so code.py can save config.json.
    # (This makes the CIRCUITPY drive read-only to your computer.)
    storage.remount("/", readonly=False)
# KEY1 held -> skip remount: computer keeps write access for editing code,
# but the device cannot persist config (pushed configs still apply live).
