lpf2.devices

Typed wrappers for LPF2 devices attached to a lpf2.port.

Each class corresponds to a C++ Lpf2::Devices::*Control interface and is returned by lpf2.port.device() once the port’s factory has identified the attached device.

Classes

accelerometer

Built-in hub accelerometer (mode 0 = GRV, unit: mG).

basic_motor

Simple DC motor with no encoder (train motor, PF-M motor).

color_distance_sensor

Boost Color & Distance Sensor (legacy, WeDo/Boost).

color_sensor

Technic Color Sensor.

distance_sensor

Technic Distance Sensor (time-of-flight).

encoder_motor

Motor with rotary encoder + position/speed control loops.

gyroscope

Built-in hub gyroscope (mode 0 = ROT, unit: dps).

hub_led

RGB LED attached to a hub's built-in LED port.

port_expander

LPF2 Port Expander (extension module giving four sub-ports on one port).

Functions

registerDefault(→ None)

Register the built-in device factories with the global registry.

Package Contents

class lpf2.devices.accelerometer[source]

Built-in hub accelerometer (mode 0 = GRV, unit: mG).

getDeviceType() int[source]

Reported LPF2 device type.

getX() float[source]

X-axis acceleration in mG.

getY() float[source]

Y-axis acceleration in mG.

getZ() float[source]

Z-axis acceleration in mG.

name() str[source]

Human-readable device name ("Hub Accelerometer").

class lpf2.devices.basic_motor[source]

Simple DC motor with no encoder (train motor, PF-M motor).

getDeviceType() int[source]

Reported LPF2 device type (see lpf2.device_type).

name() str[source]

Human-readable device name ("DC Motor (dumb)").

startPower(pw: int) None[source]

Set motor power (-100..100). Negative = CCW, positive = CW.

Special values: LPF2_POWER_FLOAT = float, LPF2_POWER_BRAKE = brake.

class lpf2.devices.color_distance_sensor[source]

Boost Color & Distance Sensor (legacy, WeDo/Boost).

getAmbientLight() int[source]

Ambient-light percentage (0..100).

getColorIdx() int[source]

Detected colour as lpf2.color index.

getDeviceType() int[source]

Reported LPF2 device type.

getDistance() float[source]

Distance in centimetres.

getReflectedLight() int[source]

Reflected-light percentage (0..100).

getRgb() tuple[int, int, int][source]

Raw RGB channels.

name() str[source]

Human-readable device name ("Color Distance Sensor").

setIrTx(value: int) None[source]

Emit a value on the LEGO Power Functions RC IR channel.

setLedColor(color: int) None[source]

Set the on-board LED colour (lpf2.color index).

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

Switch active mode; delta = change threshold for callbacks.

MODE_AMBI: int = 4[source]

Ambient-light percentage.

MODE_COLOR: int = 0[source]

Detected colour index.

MODE_DIST: int = 1[source]

Measured distance (cm).

MODE_IR: int = 7[source]

LEGO PF IR transmitter (write-only).

MODE_LED: int = 5[source]

On-board LED colour control (write-only).

MODE_REFLT: int = 3[source]

Reflected-light percentage.

MODE_RGB: int = 6[source]

Raw RGB channels.

class lpf2.devices.color_sensor[source]

Technic Color Sensor.

Modes 0/1/5/6 are polled through an internal combo so multiple values (colour, reflectivity, RGB, HSV) can be read without repeated mode switches.

getColorIdx() int[source]

Detected colour as lpf2.color index.

getDeviceType() int[source]

Reported LPF2 device type.

getHSV() tuple[int, int, int][source]

(h, s, v) — H 0..360, S 0..100, V 0..360.

getRGB() tuple[int, int, int, int][source]

(r, g, b, intensity) raw channels.

getReflectivity() float[source]

Reflected-light percentage (0..100).

name() str[source]

Human-readable device name ("Technic Color Sensor").

setLight(l1: int, l2: int, l3: int) None[source]

Drive the on-board light channels (each 0..100 PCT).

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

Switch active mode. delta = change threshold for callbacks (0 = every update).

MODE_AMBI: int = 2[source]

Ambient-light intensity (0..100 PCT).

MODE_COLOR: int = 0[source]

Detected colour index (lpf2.color).

MODE_HSV: int = 6[source]

HSV reading (H 0..360, S 0..100, V 0..360).

MODE_LIGHT: int = 3[source]

On-board LED output control (write-only).

MODE_REFLT: int = 1[source]

Reflected-light intensity (0..100 PCT).

MODE_RGB: int = 5[source]

Raw RGB channels + intensity.

class lpf2.devices.distance_sensor[source]

Technic Distance Sensor (time-of-flight).

getDeviceType() int[source]

Reported LPF2 device type.

getDistance() float[source]

Distance in centimetres.

name() str[source]

Human-readable device name ("Technic Distance Sensor").

setLight(l1: int, l2: int, l3: int, l4: int) None[source]

Drive the four on-board light segments (each 0..100 PCT).

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

Switch active mode; delta = change threshold for callbacks.

MODE_LIGHT: int = 5[source]

On-board light output control (write-only).

class lpf2.devices.encoder_motor[source]

Motor with rotary encoder + position/speed control loops.

getDeviceType() int[source]

Reported LPF2 device type.

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

Move to absolute encoder position absPos at speed then apply endState.

name() str[source]

Human-readable device name ("Motor with Encoder").

presetEncoder(pos: int) None[source]

Reset the encoder counter to pos (also stops the motor).

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

Set acceleration ramp time (ms) and profile index.

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

Set deceleration ramp time (ms) and profile index.

startPower(pw: int) None[source]

Set motor power (-100..100). Same semantics as basic_motor.startPower().

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

Run continuously at speed (-100..100), power capped at maxPower.

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

Rotate by degrees (positive; direction from speed sign) then apply endState.

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

Run at speed for time ms then apply endState (BrakingStyle).

class lpf2.devices.gyroscope[source]

Built-in hub gyroscope (mode 0 = ROT, unit: dps).

getDeviceType() int[source]

Reported LPF2 device type.

getX() float[source]

X-axis angular velocity in dps.

getY() float[source]

Y-axis angular velocity in dps.

getZ() float[source]

Z-axis angular velocity in dps.

name() str[source]

Human-readable device name ("Hub Gyroscope").

class lpf2.devices.hub_led[source]

RGB LED attached to a hub’s built-in LED port.

getDeviceType() int[source]

Reported LPF2 device type.

name() str[source]

Human-readable device name ("Hub LED").

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

Set colour by explicit RGB (each 0..255).

setColorIdx(color: int) None[source]

Set colour by lpf2.color index.

class lpf2.devices.port_expander[source]

LPF2 Port Expander (extension module giving four sub-ports on one port).

getDeviceType() int[source]

Reported LPF2 device type.

getPort(port_num: int) lpf2.port[source]

Get the sub-port port_num (0..3, see lpf2.port_expander.port_num). Returns the sub-port as a lpf2.port.

name() str[source]

Human-readable device name ("Port Expander").

lpf2.devices.registerDefault() None[source]

Register the built-in device factories with the global registry.

Called automatically at module import; expose so custom builds can re-register after a soft reset.