Ports
hub.ports is a namespace of lpf2.local.port objects
plus a few virtual ports for the built-in devices.
Members
Attribute |
Type |
What it wraps |
|---|---|---|
|
External socket A |
|
|
External socket B |
|
|
External socket C |
|
|
External socket D |
|
|
Built-in RGB LED |
|
|
On-board accelerometer |
|
|
On-board gyroscope |
Lifecycle
The C firmware polls update() on every port each
tick — Python code never has to call it. Inside update the C++
side polls the LPF2 UART, handles LWP protocol messages, and — once
a device attaches — builds the typed device wrapper that
device() returns. See Automatic polling.
import hub, time
p = hub.ports.A
dev = p.device()
print(type(dev).__name__, dev.name())
Pausing a port
Use disable() to pause polling without discarding
the device wrapper (e.g. while another driver takes the pins):
p.disable(True)
# ... do something else
p.disable(False)
Reading sensor values
For LPF2-typed sensors, use the device-specific getters when available (see Sensors). Otherwise the generic path is:
p.setMode(0) # select mode 0
val = p.getValue(mode=0, dataSet=0)
Every value goes through the mode descriptor (lpf2.mode)
that the device advertised — the format byte tells the parser
whether the payload is int8, int16, int32 or float.
Wrapped built-in ports
hub.ports.LED, hub.ports.accelerometer and hub.ports.gyro
present the built-in peripherals as LPF2 ports so that the same code
that talks to external motors/sensors can also talk to them (in
particular, lpf2.hub_emulation exposes them to LEGO apps
this way).
For everyday scripting prefer the direct wrappers:
hub.led, hub.accelerometer, hub.gyro.