lpf2
Talk to LEGO Powered Up motors, sensors, lights and hubs.
The ports on your hub (hub.ports.A …) are port objects:
use them to see what’s plugged in and to drive motors. lpf2 also
lets you connect to other LEGO hubs over Bluetooth (hub) or
make your hub pretend to be a LEGO hub (hub_emulation).
Useful lists of numbers live in sub-modules like color
and device_type.
Submodules
Classes
Advanced: battery level calculation for custom battery setups. |
|
Advanced: describes a pretend device — its modes and version numbers. |
|
Connect to another LEGO hub (Powered Up, Control+, BOOST…) over Bluetooth. |
|
Make your hub pretend to be a LEGO hub, so LEGO apps can connect to it. |
|
Advanced: describes one "mode" of a device — one kind of value it can report or accept. |
|
Advanced: the tuning values used for each kind of LEGO motor. |
|
A port where a LEGO motor, sensor or light can be plugged in. |
|
A version number made of Major.Minor.Bugfix.Build. |
Package Contents
- class lpf2.battery[source]
Advanced: battery level calculation for custom battery setups.
For the hub’s own battery use
hub.batteryinstead. Here you can set what voltage counts as full (default 9000 mV) and empty (default 6000 mV); the percentage is worked out between those two. Use it aslpf2.battery.getPercent()— no need to create one.- static readBatteryVoltage() int[source]
Measure the battery now (see
setupAdcDivider()) and return millivolts, or 0 if not set up.
- static setCurrentVoltage(mv: int) None[source]
Manually update the current battery voltage (mV).
Use this if you have your own voltage source (not the built-in ADC divider).
- static setPercentFunc(fn: Callable[[int, int, int], int] | None) None[source]
Use your own function to turn a voltage into a percentage.
fn(mV, vmin, vmax)must return 0..100. PassNoneto go back to the default (a straight line between empty and full).
- static setupAdcDivider(adc_channel: int, adc_unit: int, r_top_ohms: float, r_bottom_ohms: float, vref_mv: int = 3300, samples: int = 8) bool[source]
Advanced: measure a battery through a voltage divider on an analog pin.
adc_channel/adc_unitpick the ESP32 analog input;r_top_ohms/r_bottom_ohmsare the two resistors (battery → middle → ground). ReturnsTrueif it worked.
- class lpf2.device_descriptor[source]
Advanced: describes a pretend device — its modes and version numbers.
Used together with
lpf2.virtual.device.
- class lpf2.hub[source]
Connect to another LEGO hub (Powered Up, Control+, BOOST…) over Bluetooth.
Call
init()to start searching, wait untilisConnected()andinfoReady()areTrue, then usegetPort()to control its motors and sensors — just like your own ports.- getBatteryType() int[source]
What kind of batteries the other hub uses (see
lpf2.battery_type).
- getHubPropStr(prop_id: int) str[source]
One fact about the other hub as text, e.g. its firmware version (see
lpf2.hub_property).
- getHubType() int[source]
What kind of hub it is (see
lpf2.hub_type).
- getPort(port_num: int) port | None[source]
One of the other hub’s ports, or
Noneif it doesn’t exist.Port numbers depend on the hub type — see
lpf2.port_num, e.g.lpf2.port_num.poweredup.A. The port works likehub.ports.A.
- infoReady() bool[source]
Trueonce the other hub has told us all about itself and its ports. Wait for this before usinggetPort().
- init(addr_or_scan: str | int | None = None, scan_duration: int = 10) None[source]
Start searching for a LEGO hub to connect to.
addr_or_scancan be a hub’s Bluetooth address (text like"aa:bb:cc:dd:ee:ff") to connect to that exact hub, a number of seconds to search for, or left out to search forscan_durationseconds.
- setPortMode(port_num: int, mode: int, delta: int, notify: bool = True) int[source]
Advanced: choose which value a port on the other hub reports. Returns 0 if it worked.
- setPortModeCombo(port_num: int, combo_idx: int, nibble_pairs: Sequence[int], deltas_per_mode: Sequence[int]) int[source]
Advanced: have a port on the other hub report several values at once.
Each byte in
nibble_pairsismode * 16 + value number;deltas_per_modelists the minimum change for each, in the same order.
- class lpf2.hub_emulation[source]
Make your hub pretend to be a LEGO hub, so LEGO apps can connect to it.
Set it up first (name, battery…), choose which ports the app may use with
attachPort(), then callstart().- attachPort(port_num: int, p: port) None[source]
Let the app use port
pas port numberport_num(0 = A, 1 = B, …).Keep
pin a variable for as long as the emulation runs.
- setAlert(alert: int, on: bool) None[source]
Turn a warning such as “low battery” on or off in the app (see
lpf2.alerts).
- setBatteryType(type: int) None[source]
The battery type shown to the app (see
lpf2.battery_type).
- setButtonState(state: int) None[source]
Tell the app the hub’s button was pressed or released (see
lpf2.button_state).
- class lpf2.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.
- class lpf2.motor_setting[source]
Advanced: the tuning values used for each kind of LEGO motor.
- technic_large_angular_grey: _motor_settings[source]
Technic Large Angular motor (grey Control+ variant).
- class lpf2.port[source]
A port where a LEGO motor, sensor or light can be plugged in.
hub.ports.Aand friends are ports. AskisDeviceConnected()whether something is plugged in, get it withdevice(), or drive a motor directly withstartPower(),startSpeed()and friends. The hub keeps every port up to date in the background, so you just read the values.Speeds and powers go from -100 to 100: the sign picks the direction.
- device() devices.basic_motor | devices.encoder_motor | devices.color_sensor | devices.distance_sensor | devices.port_expander | devices.hub_led | devices.accelerometer | devices.gyroscope | None[source]
The motor, sensor or light plugged into this port, or
Noneif nothing is plugged in.What you get depends on the device — for example an
encoder_motoror acolor_sensor— each with its own handy functions.
- disable(disable: bool = True) None[source]
Switch the port off (or back on with
disable(False)).While switched off the hub stops talking to the device and frees the port’s pins, so you can use them for something else — for example an LED strip (see the NeoPixel example).
- 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
printwhile exploring.
- getValue(mode: int, dataSet: int) float[source]
Read a value from the device: value number
dataSet(starting at 0) ofmode.Use this for devices that have no special class in
lpf2.devices; otherwise their own functions are easier.
- gotoAbsPosition(absPos: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) None[source]
Turn the motor to position
absPosdegrees, then stop.Position 0 is where it was at start-up or where you last called
presetEncoder().speedis 0..100.endStatesays what the motor does afterwards:0= let it spin freely (coast),127= brake,126= hold its position.
- presetEncoder(pos: int) None[source]
Stop the motor and say “this position is now
posdegrees” (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
useProfileoption 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”).
deltais how much the value must change before an update is sent (0 = always). Returns 0 if it worked. The device classes inlpf2.devicespick modes for you.
- setModeCombo(idx: int, deltas: Sequence[float] = ()) int[source]
Advanced: have the device report several modes at once.
idxpicks one of the device’s built-in combinations;deltasgives 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,
0stops. 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
speedfrom -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.useProfilepicks the speed-up/slow-down times set withsetAccTime()/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
speednegative to turn the other way.endStatesays 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
speedfortimemilliseconds, then stop.endStatesays what the motor does afterwards:0= let it spin freely (coast),127= brake,126= hold its position.