lpf2.local

The physical ports on your hub (hub.ports.A …).

They work exactly like lpf2.port, plus a few extras such as port.forceDeviceType() for motors that don’t identify themselves.

Classes

emulated_port

Advanced: turn one of your hub's ports around, so it acts like a device.

mode

Advanced: describes one "mode" of a device — one kind of value it can report or accept.

port

A physical port on your hub, like hub.ports.A.

Package Contents

class lpf2.local.emulated_port(port: emulated_port.__init__.port)[source]

Advanced: turn one of your hub’s ports around, so it acts like a device.

Connect that port with a cable to another LEGO hub, and the other hub sees whatever pretend device you attach (see lpf2.virtual.device). For example, your hub can pretend to be a port expander.

Use port (e.g. hub.ports.F) as a device port.

The port stops detecting devices; call port.enable() later to use it normally again.

attachDevice(device: lpf2.virtual.device) → None[source]

Choose the pretend device the other hub will see — a lpf2.virtual.device or a port expander.

detachDevice() → None[source]

Remove the pretend device again.

init() → None[source]

Start waiting for the other hub to connect. Call once, after attachDevice().

isHostConnected() → bool[source]

True once the other hub has recognised the device and is talking to it.

isSlowEnumeration() → bool[source]

True if set up for an EV3 (see setSlowEnumeration()).

setSlowEnumeration(slow: bool) → None[source]

Pass True when connecting to a LEGO MINDSTORMS EV3 instead of a Powered Up hub.

Call it before init().

update() → None[source]

Advanced: process news right now. Not needed — this already happens in the background.

class lpf2.local.mode[source]

Advanced: describes one “mode” of a device — one kind of value it can report or accept.

For example a color sensor has a mode for color, one for reflected light, and so on. Holds the mode’s name and value ranges.

PCTmax: float

Percent-scale maximum.

PCTmin: float

Percent-scale minimum.

SImax: float

SI-scale maximum.

SImin: float

SI-scale minimum.

dataSets: int

Number of datasets per sample.

decimals: int

Decimals shown for display.

figures: int

Total display figures (integer + decimals).

flags: int

Capability/flags bitmask.

format: int

Sample element format (0=int8, 1=int16, 2=int32, 3=float).

inMapping: int

Input-mapping bitmask (see LWP spec).

max: float

Raw value maximum.

min: float

Raw value minimum.

motorBias: int

Motor bias, per LWP.

name: str

Mode name (from the device).

outMapping: int

Output-mapping bitmask (see LWP spec).

rawData: bytes

Latest raw payload for this mode.

class lpf2.local.port[source]

Bases: lpf2.port

A physical port on your hub, like hub.ports.A.

Everything from lpf2.port works here too.

forceDeviceType(type: int) → None[source]

Tell the port what’s plugged in, for motors that can’t say it themselves.

For example LEGO MINDSTORMS EV3 motors: hub.ports.A.forceDeviceType(device_type.EV3_LARGE_MOTOR), then drive them with startPower(). Call enable() to go back to detecting devices automatically.

getCapabilities() → int[source]

Capability bitmask (bit 0x04 = combinable, if the device has combos).

getDeviceType() → int[source]

A number saying what kind of device is plugged in — compare it with lpf2.device_type.

getInfoStr() → str[source]

Everything the port knows about its device, as text. Handy to print while exploring.

getInputModes() → int[source]

Bitmask of modes that produce values.

getMode(num: int) → lpf2.mode | None[source]

mode descriptor for mode num or None.

getModeCombo(combo: int) → int[source]

Mode-combo bitmask at index combo. Bit N = mode N.

getModeComboCount() → int[source]

Number of active mode-combo entries.

getModeCount() → int[source]

Total number of modes exposed by the device.

getOutputModes() → int[source]

Bitmask of modes that accept values.

getValue(mode: int, dataSet: int) → float[source]

Read a value from the device: value number dataSet (starting at 0) of mode.

Use this for devices that have no special class in lpf2.devices; otherwise their own functions are easier.

getValueStr(mode: int) → str[source]

The value(s) of mode as readable text.

getViewCount() → int[source]

Number of modes the device would show in the LEGO app.

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

Turn the motor to position absPos degrees, then stop.

Position 0 is where it was at start-up or where you last called presetEncoder(). speed is 0..100. endState says what the motor does afterwards: 0 = let it spin freely (coast), 127 = brake, 126 = hold its position.

isDeviceConnected() → bool[source]

True if something is plugged into this port.

presetEncoder(pos: int) → None[source]

Stop the motor and say “this position is now pos degrees” (usually 0).

rawToSpeed(raw: int) → int[source]

Advanced: convert a device byte (0..255) back to a -100..100 speed.

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

How long (milliseconds) the motor takes to speed up, stored in slot profile (0..15).

Use it with the useProfile option of the speed functions.

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

How long (milliseconds) the motor takes to slow down, stored in slot profile (0..15).

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

Advanced: choose which kind of value the device should report (its “mode”).

delta is how much the value must change before an update is sent (0 = always). Returns 0 if it worked. The device classes in lpf2.devices pick modes for you.

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

Advanced: have the device report several modes at once.

idx picks one of the device’s built-in combinations; deltas gives the minimum change for each mode in it. Returns 0 if it worked.

setRgbColor(r: int, g: int, b: int) → None[source]

Set a light to any color by mixing red, green and blue, each 0..255.

setRgbColorIdx(idx: int) → None[source]

Set a light to one of the colors in lpf2.color, e.g. color.RED.

speedToRaw(speed: int) → int[source]

Advanced: convert a -100..100 speed to the byte (0..255) the device uses.

startPower(pw: int) → None[source]

Run a motor at a power from -100 to 100.

Positive turns one way, negative the other, 0 stops. Works with every motor, even simple ones without a rotation sensor.

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

Run the motor at a steady speed from -100 to 100 until told otherwise.

Unlike startPower() the motor works harder uphill to keep its speed (needs a motor with a rotation sensor). maxPower (0..100) limits how hard it may push. useProfile picks the speed-up/slow-down times set with setAccTime() / setDecTime() (0x21 = speed up with slot 1, slow down with slot 2).

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

Turn the motor by degrees (360 = one full turn), then stop.

Give a positive number of degrees; make speed negative to turn the other way. endState says what the motor does afterwards: 0 = let it spin freely (coast), 127 = brake, 126 = hold its position.

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

Run the motor at speed for time milliseconds, then stop.

endState says what the motor does afterwards: 0 = let it spin freely (coast), 127 = brake, 126 = hold its position.

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

Advanced: send raw bytes to the device for mode. Returns 0 if it worked.