lpf2.virtual

In-firmware emulated LPF2 ports and devices.

Pair a port with a device (typically a subclass) to present a fake device to the rest of the stack — used by lpf2.hub_emulation to expose Python-implemented devices to LEGO apps, and by the port expander to synthesise sub-ports.

Classes

device

Emulated LPF2 device backed by a device_descriptor.

device_descriptor

Static descriptor for a device (modes + firmware/hardware versions).

port

LPF2 port whose transport is a Python-side device.

Package Contents

class lpf2.virtual.device(descriptor: lpf2.device_descriptor)[source]

Emulated LPF2 device backed by a device_descriptor.

Subclass to implement custom behaviour: override startPower(), setMode(), writeData() etc. The default overrides on this class call the C++ default logging implementation, so it is safe to call super().startPower(pw) from a subclass — recursion into Python is prevented on the C++ side.

Registers itself with the firmware’s update registry at construction, so any C++ update() behaviour runs each tick automatically. Python code does not need to poll the device.

Build a device from a lpf2.device_descriptor.

The descriptor defines the modes, versions and I/O masks the emulated device advertises.

gotoAbsPosition(absPos: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) None[source]

Move to absolute encoder position. Default: log the call.

presetEncoder(pos: int) None[source]

Reset the encoder to pos. Default: log the call.

setAccTime(time: int, profile: int) None[source]

Set acceleration time/profile. Default: log the call.

setDecTime(time: int, profile: int) None[source]

Set deceleration time/profile. Default: log the call.

setMode(mode: int, delta: float = 1.0) int[source]

Select the active input mode. Default: log the call. Returns 0.

setModeCombo(idx: int, deltas: Sequence[float] = ()) int[source]

Select a mode combination. Default: log the call. Returns 0.

setModeData(mode: int, buf: bytes) None[source]

Store buf as the latest raw payload for mode and fire the value-change callback.

setWriteDataCallback(cb: object) None[source]

Install a callback cb(mode, data, userData) that intercepts writes made via writeData() before the default logging implementation runs.

startPower(pw: int) None[source]

Motor power (-100..100). Default: log the call.

startSpeed(speed: int = 100, maxPower: int = 100, useProfile: int = 0) None[source]

Run at speed for as long as commanded. Default: log the call.

startSpeedForDegrees(degrees: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) None[source]

Rotate by degrees. Default: log the call.

startSpeedForTime(time: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) None[source]

Run at speed for time ms. Default: log the call.

writeData(mode: int, buf: bytes) int[source]

Handle a raw write on mode. Default: dispatch to the write callback installed via setWriteDataCallback(), else log the payload. Return 0 on success.

class lpf2.virtual.device_descriptor[source]

Static descriptor for a device (modes + firmware/hardware versions).

Used to build virtual devices without a live LPF2 handshake.

fwVersion: version

Firmware version reported by the device.

hwVersion: version

Hardware version reported by the device.

modes: list[mode]

List of mode descriptors, indexed by mode number.

class lpf2.virtual.port[source]

Bases: lpf2.port

LPF2 port whose transport is a Python-side device.

attachDevice binds a device instance; from then on calls to the port’s motor/sensor methods are forwarded to the device’s overrides.

attachDevice(device: port.attachDevice.device) None[source]

Bind device to this port. Replaces any previously attached device.

detachDevice() None[source]

Detach the current device. The port then reports disconnected.