Hub emulation

hub_emulation is a BLE server that impersonates a LEGO hub. It advertises the LWP GATT service, answers hub-property and port- info requests, and forwards port I/O to the port objects you attach.

Typical setup

import lpf2, hub

emu = lpf2.hub_emulation()
emu.setUseBuiltInDevices(False)   # opt-in to only your ports
emu.setName("MyHub")

fw = lpf2.version(); fw.Major = 1
hw = lpf2.version(); hw.Major = 1
emu.setFirmwareVersion(fw)
emu.setHardwareVersion(hw)

emu.setBatteryLevel(80)
emu.setBatteryType(0)             # see lpf2.battery_type

# Expose the physical ports A..D to the app:
emu.attachPort(0, hub.ports.A)
emu.attachPort(1, hub.ports.B)
emu.attachPort(2, hub.ports.C)
emu.attachPort(3, hub.ports.D)

emu.start()

Warning

Call hub_emulation.setUseBuiltInDevices() before start(). It has no effect once the server is running.

Runtime updates

emu.setBatteryLevel(72)
emu.setButtonState(lpf2.button_state.PRESSED)
emu.setAlert(lpf2.alerts.LOW_VOLTAGE, True)

Stop

emu.stop()

Lifetime

hub_emulation does not take ownership of the ports passed to attachPort(). Keep them alive for the lifetime of the emulator.

Reference: lpf2.hub_emulation, lpf2.alerts, lpf2.button_state, lpf2.battery_type.