lpf2.virtual

Advanced: pretend devices that you program yourself in Python.

Make a device (usually your own class based on it), put it in a port, and it behaves like a real LEGO device. Combine it with lpf2.hub_emulation so a LEGO app can “see” your custom gadget, or put it behind a port expander.

Classes

device

A pretend device whose behaviour you write in Python.

device_descriptor

Advanced: describes a pretend device — its modes and version numbers.

port

A pretend port that holds a pretend device.

Package Contents

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

A pretend device whose behaviour you write in Python.

Make your own class based on it and write functions like startPower() to decide what happens when someone drives your “motor”. By default every command is just printed to the log, and calling super().startPower(pw) from your version does the same. Describe its modes with a lpf2.device_descriptor.

Create a pretend device described by descriptor (its modes and versions).

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

Called when someone asks to go to a position. Default: print it to the log.

presetEncoder(pos: int) → None[source]

Called when someone resets the position. Default: print it to the log.

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

Called when someone sets the speed-up time. Default: print it to the log.

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

Called when someone sets the slow-down time. Default: print it to the log.

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

Called when someone picks a mode. Default: print it to the log. Returns 0.

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

Called when someone picks a mode combination. Default: print it to the log. Returns 0.

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

Publish a new value (as raw bytes) for mode — like a sensor reporting a reading.

setWriteDataCallback(cb: object) → None[source]

Run cb(mode, data, userData) whenever raw data is sent to the device.

startPower(pw: int) → None[source]

Called when someone sets the motor power (-100..100). Default: print it to the log.

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

Called when someone asks for a steady speed. Default: print it to the log.

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

Called when someone asks to turn by some degrees. Default: print it to the log.

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

Called when someone asks to run for a time. Default: print it to the log.

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

Called when raw data is sent to mode. Default: run the setWriteDataCallback() function, or print it. Return 0 if OK.

class lpf2.virtual.device_descriptor[source]

Advanced: describes a pretend device — its modes and version numbers.

Used together with lpf2.virtual.device.

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

A pretend port that holds a pretend device.

It works like any other lpf2.port; commands sent to it go to your device’s functions.

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

Put device into this port (replacing any device already there).

detachDevice() → None[source]

Take the device out; the port is then empty.