### Connect to Move Hub and print peripherals (Python) Source: https://github.com/undera/pylgbst/blob/master/README.md A basic Python example demonstrating how to instantiate a MoveHub object and iterate through its connected peripherals. This requires the pylgbst library and a compatible Bluetooth backend to be installed. ```python from pylgbst.hub import MoveHub hub = MoveHub() for device in hub.peripherals: print(device) ``` -------------------------------- ### Install pylgbst with BLEAK backend Source: https://github.com/undera/pylgbst/blob/master/README.md Installs the pylgbst library along with the BLEAK Bluetooth backend. BLEAK is recommended for cross-platform compatibility (Linux, Windows, MacOS). ```bash pip install -U pylgbst[bleak] ``` -------------------------------- ### Select Bluetooth Connection Backend with Python Source: https://context7.com/undera/pylgbst/llms.txt Illustrates how to select specific Bluetooth backends for connecting to LEGO hubs in Python. It covers auto-detection and specific backends like bleak, bluepy, and bluegiga. This is useful for platform-specific optimizations or troubleshooting connection issues. Ensure the necessary backend libraries are installed. ```python from pylgbst.hub import MoveHub from pylgbst import ( get_connection_auto, # Auto-detect best backend get_connection_bleak, # Cross-platform (recommended) get_connection_bluepy, # Linux/Raspberry Pi get_connection_bluegiga, # BlueGiga BLED112 dongle get_connection_gatt, # Linux gatt library get_connection_gattool, # Linux gattool get_connection_gattlib, # Linux gattlib ) # Auto-detect connection (tries each backend) conn = get_connection_auto() # Bleak backend (recommended for cross-platform) conn = get_connection_bleak(hub_mac='AA:BB:CC:DD:EE:FF', hub_name='LEGO Move Hub') # Bluepy backend for Linux/Raspberry Pi conn = get_connection_bluepy(controller='hci0', hub_mac='AA:BB:CC:DD:EE:FF') # Create hub with specific connection hub = MoveHub(conn) try: # Use hub... print("Connected with specific backend") finally: hub.disconnect() ``` -------------------------------- ### Subscribe to Color and Distance Sensor Data (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/VisionSensor.md Demonstrates how to subscribe to the Vision Sensor for color and distance readings using the COLOR_DISTANCE_FLOAT mode. This example sets up a callback function to process the detected color and distance, then waits for 60 seconds before unsubscribing. ```python from pylgbst.hub import MoveHub, VisionSensor import time def callback(color, distance): print("Color: %s / Distance: %s" % (color, distance)) hub = MoveHub() hub.vision_sensor.subscribe(callback, mode=VisionSensor.COLOR_DISTANCE_FLOAT) time.sleep(60) # play with sensor while it waits hub.vision_sensor.unsubscribe(callback) ``` -------------------------------- ### Subscribe to Tilt Sensor Data (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/TiltSensor.md Demonstrates how to subscribe to tilt sensor data from a LEGO Move Hub using the pylgbst library. It sets up a callback function to process 3-axis accelerometer data and then unsubscribes after a specified duration. This requires the 'pylgbst' library to be installed. ```python from pylgbst.hub import MoveHub, TiltSensor import time def callback(roll, pitch, yaw): print("Roll: %s / Pitch: %s / Yaw: %s" % (roll, pitch, yaw)) hub = MoveHub() hub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_3AXIS_ACCEL) time.sleep(60) # turn MoveHub block in different ways hub.tilt_sensor.unsubscribe(callback) ``` -------------------------------- ### Get MoveHub LED Sensor Data (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/LED.md Shows how to retrieve the current LED color data from a MoveHub in both indexed and RGB modes. This functionality is noted as experimental and may not produce meaningful results. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import LEDRGB hub = MoveHub() print("Current color - mode INDEX", hub.led.get_sensor_data(LEDRGB.MODE_INDEX)) print("Current color - mode RGB", hub.led.get_sensor_data(LEDRGB.MODE_RGB)) ``` -------------------------------- ### Motor Control - Continuous Speed Source: https://context7.com/undera/pylgbst/llms.txt Starts LEGO hub motors running at a continuous speed until explicitly stopped. Supports individual, paired, and external motors, enabling applications that require sustained motion or speed control based on real-time feedback. ```python from pylgbst.hub import MoveHub import time hub = MoveHub() try: # Start external motor at 20% speed if hub.motor_external: hub.motor_external.start_speed(0.2) time.sleep(2) # Run for 2 seconds hub.motor_external.stop() # Start both motors at different speeds hub.motor_AB.start_speed(0.5, 0.3) time.sleep(3) hub.motor_AB.stop() # Start motor with power control hub.motor_A.start_power(0.7) time.sleep(1) hub.motor_A.stop() finally: hub.disconnect() ``` -------------------------------- ### Retrieve Battery Temperature from Move Hub (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/Temperature.md This Python code snippet demonstrates how to connect to a LEGO Move Hub and retrieve the battery temperature. It iterates through the hub's peripherals and prints the temperature if a Temperature sensor is found. Ensure the pylgbst library is installed and a Move Hub is discoverable via Bluetooth. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import Temperature hub = MoveHub() for port, device in hub.peripherals.items(): if isinstance(device, Temperature): print("Battery temperature:", device.temperature) ``` -------------------------------- ### Get Battery Current from MoveHub Source: https://github.com/undera/pylgbst/blob/master/docs/VoltageCurrent.md Retrieves the battery current status from a MoveHub. The `MoveHub` class has a `current` field that provides the battery current in milliamperes (mA) as a float. This example shows direct access to the current value. ```python from pylgbst.hub import MoveHub hub = MoveHub() print ("Battery current: %s" % hub.current.current) ``` -------------------------------- ### Control Headlight Brightness with Pylgbst Source: https://github.com/undera/pylgbst/blob/master/docs/Headlight.md Set the brightness of the LED headlights using the `set_brightness` method of the `LEDLight` class in pylgbst. Values range from 0 to 100%. This example shows how to make the headlights blink by toggling the brightness. ```python import time from pylgbst.hub import MoveHub hub = MoveHub() # Blink forever while True: # Headlight is on port D; set its brightness to 100% hub.port_D.set_brightness(100) time.sleep(1) # Shutdown the ligth hub.port_D.set_brightness(0) ``` -------------------------------- ### Control Motors with pylgbst Source: https://github.com/undera/pylgbst/blob/master/docs/Motor.md Demonstrates various methods for controlling motors, including timed rotation, angled rotation, starting at a specific speed, and stopping. It covers single motor and motor group operations, as well as blocking and non-blocking calls. ```python from pylgbst.hub import MoveHub import time hub = MoveHub() hub.motor_A.timed(0.5, 0.8) hub.motor_A.timed(0.5, -0.8) hub.motor_B.angled(90, 0.8) hub.motor_B.angled(-90, 0.8) hub.motor_AB.timed(1.5, 0.8, -0.8) hub.motor_AB.angled(90, 0.8, -0.8) hub.motor_external.start_speed(0.2) time.sleep(2) hub.motor_external.stop() ``` ```python from pylgbst.hub import MoveHub hub = MoveHub() hub.motor_A.timed(0.5, 0.8, wait_complete=False) hub.motor_B.angled(90, 0.8, wait_complete=False) hub.motor_A.wait_complete() hub.motor_B.wait_complete() ``` -------------------------------- ### Get Battery Voltage from MoveHub Source: https://github.com/undera/pylgbst/blob/master/docs/VoltageCurrent.md Accesses the battery voltage status of a MoveHub. The `MoveHub` class provides a `voltage` field. Callbacks receive the voltage in Volts as a float. Direct access to the voltage and specific voltage sensor data (L or S) is also demonstrated. ```python from pylgbst.hub import MoveHub, Voltage def callback(value): print("Voltage: %s" % value) hub = MoveHub() print ("Battery voltage: %s" % hub.voltage.voltage) # or print ("Value L: %s" % hub.voltage.get_sensor_data(Voltage.VOLTAGE_L)) print ("Value S: %s" % hub.voltage.get_sensor_data(Voltage.VOLTAGE_S)) ``` -------------------------------- ### Python: Establish Bluetooth Connection with MoveHub Source: https://github.com/undera/pylgbst/blob/master/README.md Demonstrates how to establish a Bluetooth connection to a LEGO MoveHub using a specific backend (GATT) and how to pass the connection object to the MoveHub constructor. This requires the 'gatt' library and a valid MAC address for the hub. ```python from pylgbst.hub import MoveHub from pylgbst import get_connection_gatt conn = get_connection_gatt(hub_mac='AA:BB:CC:DD:EE:FF') hub = MoveHub(conn) ``` -------------------------------- ### Connect to LEGO Move Hub and access peripherals Source: https://context7.com/undera/pylgbst/llms.txt Demonstrates connecting to a LEGO Move Hub using pylgbst. It shows auto-detection, listing connected peripherals, and explicit connection using a specific MAC address and backend. Ensure to disconnect when finished. ```python from pylgbst.hub import MoveHub from pylgbst import get_connection_bleak, get_connection_auto # Basic connection - auto-detect and connect hub = MoveHub() # List all detected peripherals for device in hub.peripherals: print(device) # Connection with specific MAC address conn = get_connection_bleak(hub_mac='AA:BB:CC:DD:EE:FF') hub = MoveHub(conn) # Always disconnect when done try: # Your code here print("Connected to hub") finally: hub.disconnect() ``` -------------------------------- ### Connect SmartHub and Remote Handset with Python Source: https://context7.com/undera/pylgbst/llms.txt Demonstrates connecting to LEGO PoweredUp SmartHub (88009) and Remote Handset controllers using Python. It shows how to control the SmartHub's LED, access peripherals on its ports, and subscribe to button events from the Remote Handset. Ensure Bluetooth is enabled and the hubs are discoverable. ```python from pylgbst.hub import SmartHub, RemoteHandset from pylgbst import get_connection_auto import time # Connect to SmartHub conn = get_connection_auto(hub_name="Smart Hub") smart_hub = SmartHub(conn) try: # SmartHub has ports A, B, LED, current, and voltage print("SmartHub connected") # Control LED from pylgbst.peripherals import COLOR_BLUE smart_hub.led.set_color(COLOR_BLUE) # Access peripherals on ports if smart_hub.port_A: print(f"Device on port A: {smart_hub.port_A}") if smart_hub.port_B: print(f"Device on port B: {smart_hub.port_B}") finally: smart_hub.connection.disconnect() # Connect to Remote Handset handset_conn = get_connection_auto(hub_name="Remote Handset") handset = RemoteHandset(handset_conn) try: # Subscribe to button events def remote_button_callback(button, side): print(f"Remote button: {button} on {side} side") handset.port_A.subscribe(remote_button_callback) handset.port_B.subscribe(remote_button_callback) print("Press remote buttons...") time.sleep(30) handset.port_A.unsubscribe(remote_button_callback) handset.port_B.unsubscribe(remote_button_callback) finally: handset.connection.disconnect() ``` -------------------------------- ### Monitor Hub Battery Voltage and Current Source: https://context7.com/undera/pylgbst/llms.txt Monitors the hub's battery status, providing real-time voltage and current draw. Values can be read directly or subscribed to for continuous updates. Useful for power management and diagnostics. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import Voltage, Current import time hub = MoveHub() try: # Direct voltage reading print(f"Battery voltage: {hub.voltage.voltage:.2f} V") # Direct current reading print(f"Battery current: {hub.current.current:.2f} mA") # Subscribe to voltage changes def voltage_callback(volts): print(f"Voltage: {volts:.2f} V") hub.voltage.subscribe(voltage_callback, mode=Voltage.VOLTAGE_L, granularity=1) # Subscribe to current changes def current_callback(milliamps): print(f"Current: {milliamps:.2f} mA") hub.current.subscribe(current_callback, mode=Current.CURRENT_L, granularity=1) # Run a motor to see current change print("Running motor to observe current draw...") hub.motor_A.timed(3, 0.8) time.sleep(2) hub.voltage.unsubscribe(voltage_callback) hub.current.unsubscribe(current_callback) finally: hub.disconnect() ``` -------------------------------- ### Handle Hub Push Button Events Source: https://context7.com/undera/pylgbst/llms.txt Subscribes to the hub's push button press and release events. The button near the LED acts as an input device, triggering callbacks when pressed or released. Useful for user interaction. ```python from pylgbst.hub import MoveHub import time hub = MoveHub() try: def button_callback(is_pressed): if is_pressed: print("Button pressed!") else: print("Button released!") hub.button.subscribe(button_callback) print("Press the hub button (waiting 30 seconds)...") time.sleep(30) hub.button.unsubscribe(button_callback) finally: hub.disconnect() ``` -------------------------------- ### Control External Headlight LED Lights Source: https://context7.com/undera/pylgbst/llms.txt Controls external LED lights, such as the LEGO LED Light peripheral (headlight kit), connected to the hub ports. Allows setting brightness from 0 to 100% and implementing effects like blinking. Assumes LEDLight is connected to port D. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import LEDLight import time hub = MoveHub() try: # Check if headlight is connected to port D if isinstance(hub.port_D, LEDLight): headlight = hub.port_D # Set brightness to 100% headlight.set_brightness(100) time.sleep(1) # Dim to 50% headlight.brightness = 50 time.sleep(1) # Blink effect for _ in range(5): headlight.set_brightness(100) time.sleep(0.5) headlight.set_brightness(0) time.sleep(0.5) # Read current brightness print(f"Current brightness: {headlight.brightness}%") else: print("No headlight on port D") finally: hub.disconnect() ``` -------------------------------- ### Subscribe to MoveHub LED Color Changes (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/LED.md Illustrates how to subscribe to and unsubscribe from LED color change events on a MoveHub. It includes setting various colors and iterating through available color constants, with a callback function to print the color changes. ```python from pylgbst.hub import MoveHub, COLORS, COLOR_NONE, COLOR_RED import time def callback(clr): print("Color has changed: %s" % clr) hub = MoveHub() hub.led.subscribe(callback) hub.led.set_color(COLOR_RED) for color in COLORS: hub.led.set_color(color) time.sleep(0.5) hub.led.set_color(COLOR_NONE) hub.led.unsubscribe(callback) ``` -------------------------------- ### Motor Control - Timed Movement Source: https://context7.com/undera/pylgbst/llms.txt Controls LEGO hub motors for a specified duration and speed. Supports individual motors (A, B), paired motors (AB), and external motors, allowing for precise timed movements in forward or reverse directions. ```python from pylgbst.hub import MoveHub import time hub = MoveHub() try: # Run motor A forward at 80% speed for 0.5 seconds hub.motor_A.timed(0.5, 0.8) # Run motor B backward at 80% speed for 0.5 seconds hub.motor_B.timed(0.5, -0.8) # Run both motors together - different speeds for turning # Motor A at 80%, Motor B at -80% (opposite directions = spin) hub.motor_AB.timed(1.5, 0.8, -0.8) # Run both motors same direction at full speed hub.motor_AB.timed(0.5, 1.0) # External motor on port C or D (if attached) if hub.motor_external: hub.motor_external.timed(1.0, 0.5) finally: hub.disconnect() ``` -------------------------------- ### Control Hub LED Color with pylgbst Source: https://context7.com/undera/pylgbst/llms.txt Change the color of the hub's RGB LED using predefined color constants or custom RGB values. The LED can display 12 distinct colors. Requires the pylgbst library and a MoveHub. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import ( COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_ORANGE, COLOR_PURPLE, COLOR_CYAN, COLOR_WHITE, COLOR_PINK, COLOR_BLACK, COLORS ) import time hub = MoveHub() try: # Set LED to red using constant hub.led.set_color(COLOR_RED) time.sleep(1) # Set LED using property syntax hub.led.color = COLOR_GREEN time.sleep(1) # Set LED using RGB tuple (0-255 for each channel) hub.led.color = (255, 128, 0) # Orange via RGB time.sleep(1) # Cycle through all available colors for color_code in COLORS.keys(): color_name = COLORS[color_code] print(f"Setting LED to: {color_name}") hub.led.set_color(color_code) time.sleep(0.5) # Turn LED off hub.led.set_color(COLOR_BLACK) finally: hub.disconnect() ``` -------------------------------- ### Subscribe to Move Hub Button Press Events in Python Source: https://github.com/undera/pylgbst/blob/master/docs/MoveHub.md This Python code demonstrates how to subscribe to button press and release events from a MoveHub. It defines a callback function to print the button's state and then instantiates a MoveHub to attach the callback to its button field. The button state can be 0 (not released), 1 (pressed), or 2 (pressed). ```python from pylgbst.hub import MoveHub def callback(is_pressed): print("Btn pressed: %s" % is_pressed) hub = MoveHub() hub.button.subscribe(callback) ``` -------------------------------- ### Access Color and Distance Sensor Attributes (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/VisionSensor.md Shows how to directly access the current readings from the Vision Sensor's attributes. This includes retrieving the detected color, distance in inches, reflected light intensity, ambient light luminosity, detection count, and RGB color channels. ```python from pylgbst.hub import MoveHub hub = MoveHub() print("Color:", hub.vision_sensor.color) print("Distance:", hub.vision_sensor.distance) print("Reflected light:", hub.vision_sensor.reflected_light) print("Luminosity:", hub.vision_sensor.luminosity) print("Detection count:", hub.vision_sensor.detection_count) print("RGB channels:", hub.vision_sensor.rgb_color) ``` -------------------------------- ### Read Tilt Sensor Data with pylgbst Source: https://context7.com/undera/pylgbst/llms.txt Read orientation data from the hub's built-in tilt sensor. Supports various modes including simple state detection, 2-axis and 3-axis angle measurements, and bump detection. Requires the pylgbst library and a MoveHub. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import TiltSensor import time hub = MoveHub() try: # 3-axis simple state detection def simple_callback(state): state_name = TiltSensor.TRI_STATES.get(state, "UNKNOWN") print(f"Orientation: {state_name}") hub.tilt_sensor.subscribe(simple_callback, mode=TiltSensor.MODE_3AXIS_SIMPLE) print("Tilt the hub in different directions...") time.sleep(10) hub.tilt_sensor.unsubscribe(simple_callback) # 3-axis precise angle measurement def precise_callback(roll, pitch, yaw): print(f"Roll: {roll}, Pitch: {pitch}, Yaw: {yaw}") hub.tilt_sensor.subscribe(precise_callback, mode=TiltSensor.MODE_3AXIS_ACCEL) time.sleep(10) hub.tilt_sensor.unsubscribe(precise_callback) # 2-axis angle measurement def dual_axis_callback(roll, pitch): print(f"Roll: {roll}, Pitch: {pitch}") hub.tilt_sensor.subscribe(dual_axis_callback, mode=TiltSensor.MODE_2AXIS_ANGLE) time.sleep(10) hub.tilt_sensor.unsubscribe(dual_axis_callback) # Bump detection def bump_callback(count): print(f"Bump count: {count}") hub.tilt_sensor.subscribe(bump_callback, mode=TiltSensor.MODE_IMPACT_COUNT) print("Tap the hub to register bumps...") time.sleep(10) hub.tilt_sensor.unsubscribe(bump_callback) finally: hub.disconnect() ``` -------------------------------- ### Read Vision Sensor Color and Distance Data Source: https://context7.com/undera/pylgbst/llms.txt Reads color and distance data from the external vision sensor. Supports combined color/distance mode, RGB color detection, and direct property access for one-shot readings. Requires the vision sensor to be connected to port C or D. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import VisionSensor, COLORS import time hub = MoveHub() try: if hub.vision_sensor: # Combined color and distance mode (default) def color_distance_callback(color, distance): color_name = COLORS.get(color, "UNKNOWN") print(f"Color: {color_name}, Distance: {distance:.1f} inches") hub.vision_sensor.subscribe(color_distance_callback, mode=VisionSensor.COLOR_DISTANCE_FLOAT) print("Wave objects in front of sensor...") time.sleep(10) hub.vision_sensor.unsubscribe(color_distance_callback) # RGB color detection def rgb_callback(red, green, blue): print(f"RGB: ({red}, {green}, {blue})") hub.vision_sensor.subscribe(rgb_callback, mode=VisionSensor.COLOR_RGB) time.sleep(5) hub.vision_sensor.unsubscribe(rgb_callback) # Direct property access (one-shot readings) print(f"Color index: {hub.vision_sensor.color}") print(f"Distance: {hub.vision_sensor.distance} inches") print(f"Reflected light: {hub.vision_sensor.reflected_light}") print(f"Ambient light: {hub.vision_sensor.luminosity}") print(f"RGB color: {hub.vision_sensor.rgb_color}") print(f"Detection count: {hub.vision_sensor.detection_count}") # Set the sensor's built-in LED color from pylgbst.peripherals import COLOR_GREEN hub.vision_sensor.set_color(COLOR_GREEN) else: print("No vision sensor detected") finally: hub.disconnect() ``` -------------------------------- ### Subscribe to Motor Rotation Sensors with pylgbst Source: https://github.com/undera/pylgbst/blob/master/docs/Motor.md Shows how to subscribe to a motor's rotation sensor to receive real-time updates on its angle or speed. Includes setting up a callback function, subscribing, waiting for sensor data, and unsubscribing. ```python from pylgbst.hub import MoveHub, EncodedMotor import time def callback(angle): print("Angle: %s" % angle) hub = MoveHub() hub.motor_A.subscribe(callback, mode=EncodedMotor.SENSOR_ANGLE) time.sleep(60) # rotate motor A hub.motor_A.unsubscribe(callback) ``` -------------------------------- ### Motor Control - Angled Movement Source: https://context7.com/undera/pylgbst/llms.txt Rotates LEGO hub motors to precise angles. Supports individual, paired, and external motors, allowing movement in specified degrees (positive or negative) and optionally waiting for completion. Useful for robotics requiring exact positioning. ```python from pylgbst.hub import MoveHub hub = MoveHub() try: # Rotate motor B by 90 degrees at 80% speed hub.motor_B.angled(90, 0.8) # Rotate motor B by 90 degrees in reverse hub.motor_B.angled(-90, 0.8) # Rotate motor A by 360 degrees (full rotation) hub.motor_A.angled(360, 0.5) # Rotate both motors - opposite directions for turning hub.motor_AB.angled(360, 1.0, -1.0) # Non-blocking parallel motor control hub.motor_A.angled(180, 0.8, wait_complete=False) hub.motor_B.angled(90, 0.8, wait_complete=False) hub.motor_A.wait_complete() hub.motor_B.wait_complete() finally: hub.disconnect() ``` -------------------------------- ### Disconnect Hub in finally block (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/GenericHub.md Ensures the disconnect() method is called on the connection object after the program finishes to clear the Bluetooth subsystem. This is best implemented using a try...finally clause in Python. It requires the pylgbst library for connection and hub management. ```python from pylgbst import get_connection_auto from pylgbst.hub import Hub conn = get_connection_auto() # ! don't put this into `try` block try: hub = Hub(conn) finally: conn.disconnect() ``` -------------------------------- ### Monitor Motor Rotation with pylgbst Source: https://context7.com/undera/pylgbst/llms.txt Subscribe to motor rotation angle and speed data from EncodedMotor peripherals. This allows for feedback control and position tracking by continuously receiving updates as the motor shaft rotates. Requires the pylgbst library and a MoveHub with encoders. ```python from pylgbst.hub import MoveHub from pylgbst.peripherals import EncodedMotor import time hub = MoveHub() try: # Track rotation angle def angle_callback(angle): print(f"Motor A angle: {angle} degrees") hub.motor_A.subscribe(angle_callback, mode=EncodedMotor.SENSOR_ANGLE) # Manually rotate motor A for 10 seconds to see angle changes print("Rotate motor A manually...") time.sleep(10) hub.motor_A.unsubscribe(angle_callback) # Track rotation speed def speed_callback(speed): print(f"Motor B speed: {speed}") hub.motor_B.subscribe(speed_callback, mode=EncodedMotor.SENSOR_SPEED) hub.motor_B.timed(2, 0.5) # Run motor while monitoring speed hub.motor_B.unsubscribe(speed_callback) finally: hub.disconnect() ``` -------------------------------- ### Set MoveHub LED Color (Python) Source: https://github.com/undera/pylgbst/blob/master/docs/LED.md Demonstrates how to set the LED color of a MoveHub using predefined color constants or RGB values. It imports the necessary MoveHub class and color constants from the pylgbst library. ```python from pylgbst.hub import MoveHub, COLOR_RED hub = MoveHub() print("Set LED color to red:") hub.led.color = COLOR_RED # or hub.led.set_color(COLOR_RED) print("Set LED color to red, via the RGB value:") hub.led.color = (255, 0, 0) ``` -------------------------------- ### Advanced Button Handling in Python with pylgbst Source: https://github.com/undera/pylgbst/blob/master/examples/advancedbutton/README.md This Python code snippet utilizes the pylgbst library to add advanced functionality to the Move Hub's button. It defines separate callbacks for 'click', 'double_click', and 'long_press' events, allowing for custom actions based on user interaction. The timings for double and long presses can be adjusted using constants. ```python from pylgbst.hub import MoveHub from advancedbutton import AdvancedButton import time hub = MoveHub() b = AdvancedButton(hub) def clicked(): print("button clicked") def pressed(): print("button pressed") def doubleclicked(): print("button double clicked") b.click.subscribe(clicked) b.double_click.subscribe(doubleclicked) b.long_press.subscribe(pressed) time.sleep(120) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.