Buttons

hub.buttons (hub._buttons_module) covers the 5-way directional pad (center = power button).

Level readers

Return the current held state:

if hub.buttons.center():
    ...
if hub.buttons.left():
    ...

Names: center, up, down, left, right.

Callbacks

Callbacks fire on rising edge (release → press). They are dispatched automatically from the C firmware loop (Hub::loop) under the GIL — no poll() call is needed in user code.

Decorator form:

@hub.buttons.on("left")
def left_pressed():
    print("<--")

@hub.buttons.on("center")
def center_pressed():
    print("center")

Manual form:

hub.buttons.on("up", my_cb)   # register
hub.buttons.on("up", None)    # unregister
hub.buttons.off("up")         # alt: unregister

Exiting a program

Holding the centre button for >= 2 s causes the C firmware to schedule a KeyboardInterrupt on the MicroPython task. The runner catches this and shows the “Done” screen. User code cannot suppress it — treat it as a physical stop signal.

To exit from Python: hub.exit() (raises SystemExit, does not return).

Power-off

Holding the centre button for >= 5 s triggers a hardware power-off in the C firmware loop. User code cannot suppress this.

To power off from Python: hub.powerOff() (does not return).

Callback registry snapshots

_snapshot() and _restore() swap the registry — used internally by the program runner so the menu’s own callbacks do not fire while a user script owns the buttons.