mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-07-28 04:07:46 +00:00
This adds a new BLE class that is used for connectionless broadcasting/ observing on hubs with built-in Bluetooth Low Energy. Also see https://github.com/pybricks/pybricks-micropython/pull/158.
25 lines
761 B
Python
25 lines
761 B
Python
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
|
|
from pybricks.hubs import ThisHub
|
|
from pybricks.pupdevices import Motor
|
|
from pybricks.parameters import Port
|
|
from pybricks.tools import wait
|
|
|
|
# Initialize the hub.
|
|
hub = ThisHub(broadcast_channel=1)
|
|
|
|
# Initialize the motors.
|
|
left_motor = Motor(Port.A)
|
|
right_motor = Motor(Port.B)
|
|
|
|
while True:
|
|
# Read the motor angles to be sent to the other hub.
|
|
left_angle = left_motor.angle()
|
|
right_angle = right_motor.angle()
|
|
|
|
# Set the broadcast data and start broadcasting if not already doing so.
|
|
hub.ble.broadcast(left_angle, right_angle)
|
|
|
|
# Broadcasts are only sent every 100 milliseconds, so there is no reason
|
|
# to call the broadcast() method more often than that.
|
|
wait(100)
|