mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-12 01:24:17 +00:00
pybricks.common: Add new BLE class.
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.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
# 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)
|
||||
@@ -0,0 +1,39 @@
|
||||
# ThisHub = MoveHub CityHub TechnicHub PrimeHub EssentialHub
|
||||
from pybricks.hubs import ThisHub
|
||||
from pybricks.pupdevices import Motor
|
||||
from pybricks.parameters import Color, Port
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Initialize the hub.
|
||||
hub = ThisHub(observe_channels=[1])
|
||||
|
||||
# Initialize the motors.
|
||||
left_motor = Motor(Port.A)
|
||||
right_motor = Motor(Port.B)
|
||||
|
||||
while True:
|
||||
# Receive broadcast from the other hub.
|
||||
|
||||
data = hub.ble.observe(1)
|
||||
|
||||
if data is None:
|
||||
# No data has been received in the last 1 second.
|
||||
hub.light.on(Color.RED)
|
||||
else:
|
||||
# Data was received and is less that one second old.
|
||||
hub.light.on(Color.GREEN)
|
||||
|
||||
# *data* contains the same values in the same order
|
||||
# that were passed to hub.ble.broadcast() on the
|
||||
# other hub.
|
||||
left_angle = data[0]
|
||||
right_angle = data[1]
|
||||
|
||||
# Make the motors on this hub mirror the position of the
|
||||
# motors on the other hub.
|
||||
left_motor.track_target(left_angle)
|
||||
right_motor.track_target(right_angle)
|
||||
|
||||
# Broadcasts are only sent every 100 milliseconds, so there is
|
||||
# no reason to call the observe() method more often than that.
|
||||
wait(100)
|
||||
Reference in New Issue
Block a user