The lpf2 module
LEGO Powered-Up / LPF2 protocol primitives.
Layout
Submodule |
What lives there |
|---|---|
|
|
|
|
|
|
Typed device wrappers returned by |
|
Port expander helpers (sub-port numbering, virtual device). |
|
Enums |
|
Design notes
Every user-visible lpf2 type is a MicroPython wrapper around a
C++ object. The wrappers hold mp_obj_t anchors to related Python
objects (attached devices, descriptors, port references) so the GC
does not collect them while the C++ side still references them.
Automatic polling
Every lpf2 class whose C++ base has an update() method
(currently: lpf2.hub, all lpf2.port subclasses,
lpf2.virtual.device, lpf2.port_expander.virtual_device)
adds itself to a C-side registry at construction time. The firmware
main loop calls every registered update() each tick, so
you do not need to call ``update()`` from Python — device
detection, transport pumping and value parsing all happen in the
background. Deleting the Python wrapper (__del__) removes the
object from the registry.
Built-in local ports (hub.ports.A/B/C/D and the
wrapped built-ins) are constructed on the C++ side and are also pumped
by the update() registry — same net effect.
Subclassing
lpf2.virtual.device is designed to be subclassed. Overrides
run on the Python side via a C++ trampoline. Default implementations
call the C++ default non-virtually so super().startPower(pw) is
safe (no infinite recursion).
See Virtual devices for the pattern.