Ports

lpf2.port is the base class for every port kind:

Lifecycle & polling

Every port registers itself with the C-side update registry when it is constructed. The firmware main loop calls its update() each tick, so user code does not need to call ``update()``. Each call:

  1. Pumps one tick of the transport (UART framing for local, BLE receive for remote, no-op for virtual).

  2. Detects/handshakes new devices via the device factory (see lpf2.devices.registerDefault()).

  3. Parses inbound data through the mode descriptors and fires the value-change callback for any dataset whose delta exceeds the threshold set via setMode().

Calling update() manually is harmless (it just runs an extra tick early); calling it is only useful if you want to force an immediate poll before reading state in the same iteration.

Use disable() to pause polling temporarily; the port keeps its device wrapper but the background poll becomes a no-op until re-enabled.

Detecting a device

if port.isDeviceConnected():
    dev = port.device()          # typed wrapper, or None
    print(type(dev).__name__)
    print(port.getDeviceType())  # numeric device type
    print(port.getInfoStr())     # diagnostic dump

The returned object is one of the lpf2.devices classes. Use isinstance if you need to branch:

from lpf2 import devices
dev = port.device()
if isinstance(dev, devices.encoder_motor):
    dev.startSpeed(50)

Mode selection

LPF2 devices publish a set of modes (see lpf2.mode); one is active at any time on the input side.

port.setMode(0, delta=1.0)   # mode 0, callback on >= 1-unit change

Combined modes let you sample several datasets in one round-trip:

port.setModeCombo(idx=0, deltas=[1.0, 5.0])

Direct motor commands

The port itself exposes the LPF2 output commands so you do not need to fetch the device wrapper for every motor call — see Motors.

Introspection

port.getModeCount()
port.getViewCount()
for i in range(port.getModeCount()):
    m = port.getMode(i)
    print(i, m.name, m.SImin, m.SImax, m.format)

port.getInputModes()      # bitmask
port.getOutputModes()     # bitmask
port.getCapabilities()    # 0x04 bit = combinable

Raw I/O

port.writeData(mode=1, buf=b"\\x80")
port.getValue(mode=0, dataSet=0)
port.getValueStr(mode=0)

speedToRaw / rawToSpeed convert between the signed -100..100 scale and the LPF2 raw byte (0..255).

Reference: lpf2.port, lpf2.local.port, lpf2.virtual.port.