Source code for lpf2.devices

"""Typed wrappers for LPF2 devices attached to a :class:`lpf2.port`.

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

from __future__ import annotations
from lpf2 import port as _port

[docs] def registerDefault() -> None: """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. """ ...
[docs] class basic_motor: """Simple DC motor with no encoder (train motor, PF-M motor)."""
[docs] def startPower(self, pw: int) -> None: """Set motor power (-100..100). Negative = CCW, positive = CW. Special values: ``LPF2_POWER_FLOAT`` = float, ``LPF2_POWER_BRAKE`` = brake. """ ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type (see :mod:`lpf2.device_type`).""" ...
[docs] def name(self) -> str: """Human-readable device name (``"DC Motor (dumb)"``).""" ...
[docs] class encoder_motor: """Motor with rotary encoder + position/speed control loops."""
[docs] def startPower(self, pw: int) -> None: """Set motor power (-100..100). Same semantics as :meth:`basic_motor.startPower`.""" ...
[docs] def setAccTime(self, time: int, profile: int) -> None: """Set acceleration ramp time (ms) and profile index.""" ...
[docs] def setDecTime(self, time: int, profile: int) -> None: """Set deceleration ramp time (ms) and profile index.""" ...
[docs] def startSpeed(self, speed: int = 100, maxPower: int = 100, useProfile: int = 0) -> None: """Run continuously at ``speed`` (-100..100), power capped at ``maxPower``.""" ...
[docs] def startSpeedForTime(self, time: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) -> None: """Run at ``speed`` for ``time`` ms then apply ``endState`` (BrakingStyle).""" ...
[docs] def startSpeedForDegrees(self, degrees: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) -> None: """Rotate by ``degrees`` (positive; direction from ``speed`` sign) then apply ``endState``.""" ...
[docs] def gotoAbsPosition(self, absPos: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) -> None: """Move to absolute encoder position ``absPos`` at ``speed`` then apply ``endState``.""" ...
[docs] def presetEncoder(self, pos: int) -> None: """Reset the encoder counter to ``pos`` (also stops the motor).""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Motor with Encoder"``).""" ...
[docs] class color_sensor: """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. """
[docs] MODE_COLOR: int = 0
"""Detected colour index (:mod:`lpf2.color`)."""
[docs] MODE_REFLT: int = 1
"""Reflected-light intensity (0..100 PCT)."""
[docs] MODE_AMBI: int = 2
"""Ambient-light intensity (0..100 PCT)."""
[docs] MODE_LIGHT: int = 3
"""On-board LED output control (write-only)."""
[docs] MODE_RGB: int = 5
"""Raw RGB channels + intensity."""
[docs] MODE_HSV: int = 6
"""HSV reading (H 0..360, S 0..100, V 0..360)."""
[docs] def getColorIdx(self) -> int: """Detected colour as :mod:`lpf2.color` index.""" ...
[docs] def getReflectivity(self) -> float: """Reflected-light percentage (0..100).""" ...
[docs] def getRGB(self) -> tuple[int, int, int, int]: """``(r, g, b, intensity)`` raw channels.""" ...
[docs] def getHSV(self) -> tuple[int, int, int]: """``(h, s, v)`` — H 0..360, S 0..100, V 0..360.""" ...
[docs] def setLight(self, l1: int, l2: int, l3: int) -> None: """Drive the on-board light channels (each 0..100 PCT).""" ...
[docs] def setMode(self, modeNum: int, delta: float = 1.0) -> None: """Switch active mode. ``delta`` = change threshold for callbacks (0 = every update).""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Technic Color Sensor"``).""" ...
[docs] class distance_sensor: """Technic Distance Sensor (time-of-flight)."""
[docs] MODE_LIGHT: int = 5
"""On-board light output control (write-only)."""
[docs] def setLight(self, l1: int, l2: int, l3: int, l4: int) -> None: """Drive the four on-board light segments (each 0..100 PCT).""" ...
[docs] def getDistance(self) -> float: """Distance in centimetres.""" ...
[docs] def setMode(self, modeNum: int, delta: float = 1.0) -> None: """Switch active mode; ``delta`` = change threshold for callbacks.""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Technic Distance Sensor"``).""" ...
[docs] class color_distance_sensor: """Boost Color & Distance Sensor (legacy, WeDo/Boost)."""
[docs] MODE_COLOR: int = 0
"""Detected colour index."""
[docs] MODE_DIST: int = 1
"""Measured distance (cm)."""
[docs] MODE_REFLT: int = 3
"""Reflected-light percentage."""
[docs] MODE_AMBI: int = 4
"""Ambient-light percentage."""
[docs] MODE_LED: int = 5
"""On-board LED colour control (write-only)."""
[docs] MODE_RGB: int = 6
"""Raw RGB channels."""
[docs] MODE_IR: int = 7
"""LEGO PF IR transmitter (write-only)."""
[docs] def getColorIdx(self) -> int: """Detected colour as :mod:`lpf2.color` index.""" ...
[docs] def getDistance(self) -> float: """Distance in centimetres.""" ...
[docs] def getReflectedLight(self) -> int: """Reflected-light percentage (0..100).""" ...
[docs] def getAmbientLight(self) -> int: """Ambient-light percentage (0..100).""" ...
[docs] def getRgb(self) -> tuple[int, int, int]: """Raw RGB channels.""" ...
[docs] def setIrTx(self, value: int) -> None: """Emit a value on the LEGO Power Functions RC IR channel.""" ...
[docs] def setLedColor(self, color: int) -> None: """Set the on-board LED colour (:mod:`lpf2.color` index).""" ...
[docs] def setMode(self, modeNum: int, delta: float = 1.0) -> None: """Switch active mode; ``delta`` = change threshold for callbacks.""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Color Distance Sensor"``).""" ...
[docs] class hub_led: """RGB LED attached to a hub's built-in LED port."""
[docs] def setColorIdx(self, color: int) -> None: """Set colour by :mod:`lpf2.color` index.""" ...
[docs] def setColor(self, r: int, g: int, b: int) -> None: """Set colour by explicit RGB (each 0..255).""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Hub LED"``).""" ...
[docs] class accelerometer: """Built-in hub accelerometer (mode 0 = GRV, unit: mG)."""
[docs] def getX(self) -> float: """X-axis acceleration in mG.""" ...
[docs] def getY(self) -> float: """Y-axis acceleration in mG.""" ...
[docs] def getZ(self) -> float: """Z-axis acceleration in mG.""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Hub Accelerometer"``).""" ...
[docs] class gyroscope: """Built-in hub gyroscope (mode 0 = ROT, unit: dps)."""
[docs] def getX(self) -> float: """X-axis angular velocity in dps.""" ...
[docs] def getY(self) -> float: """Y-axis angular velocity in dps.""" ...
[docs] def getZ(self) -> float: """Z-axis angular velocity in dps.""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Hub Gyroscope"``).""" ...
[docs] class port_expander: """LPF2 Port Expander (extension module giving four sub-ports on one port)."""
[docs] def getPort(self, port_num: int) -> _port: """Get the sub-port ``port_num`` (0..3, see :mod:`lpf2.port_expander.port_num`). Returns the sub-port as a :class:`lpf2.port`.""" ...
[docs] def getDeviceType(self) -> int: """Reported LPF2 device type.""" ...
[docs] def name(self) -> str: """Human-readable device name (``"Port Expander"``).""" ...