From 2ccf76c022c02e43e36f2a348e5061d41eda0b56 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Fri, 24 Nov 2023 20:46:14 +0100 Subject: [PATCH] tutorials/wireless/hub-to-device/pc-communications: Update for 3.3.0. --- .../hub-to-device/pc-communication/demo.py | 29 +++++++++++++------ .../hub-to-device/pc-communication/main.py | 3 -- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/tutorials/wireless/hub-to-device/pc-communication/demo.py b/tutorials/wireless/hub-to-device/pc-communication/demo.py index 34843ac..d39ad1d 100644 --- a/tutorials/wireless/hub-to-device/pc-communication/demo.py +++ b/tutorials/wireless/hub-to-device/pc-communication/demo.py @@ -2,13 +2,17 @@ # Copyright (c) 2020 Henrik Blidh # Copyright (c) 2022-2023 The Pybricks Authors +""" +Example program for computer-to-hub communication. + +Requires Pybricks firmware >= 3.3.0. +""" + import asyncio from contextlib import suppress from bleak import BleakScanner, BleakClient -UART_SERVICE_UUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" -UART_RX_CHAR_UUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" -UART_TX_CHAR_UUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" +PYBRICKS_COMMAND_EVENT_CHAR_UUID = "c5f50002-8280-46da-89f4-6d8051e4aeef" # Replace this with the name of your hub if you changed # it when installing the Pybricks firmware. @@ -30,10 +34,13 @@ async def main(): ready_event = asyncio.Event() def handle_rx(_, data: bytearray): - if data == b"rdy": - ready_event.set() - else: - print("Received:", data) + if data[0] == 0x01: # "write stdout" event (0x01) + payload = data[1:] + + if payload == b"rdy": + ready_event.set() + else: + print("Received:", payload) # Do a Bluetooth scan to find the hub. device = await BleakScanner.find_device_by_name(HUB_NAME) @@ -46,7 +53,7 @@ async def main(): async with BleakClient(device, handle_disconnect) as client: # Subscribe to notifications from the hub. - await client.start_notify(UART_TX_CHAR_UUID, handle_rx) + await client.start_notify(PYBRICKS_COMMAND_EVENT_CHAR_UUID, handle_rx) # Shorthand for sending some data to the hub. async def send(data): @@ -55,7 +62,11 @@ async def main(): # Prepare for the next ready event. ready_event.clear() # Send the data to the hub. - await client.write_gatt_char(UART_RX_CHAR_UUID, data) + await client.write_gatt_char( + PYBRICKS_COMMAND_EVENT_CHAR_UUID, + b"\x06" + data, # prepend "write stdin" command (0x06) + response=True + ) # Tell user to start program on the hub. print("Start the program on the hub now with the button.") diff --git a/tutorials/wireless/hub-to-device/pc-communication/main.py b/tutorials/wireless/hub-to-device/pc-communication/main.py index c8473ac..bc44c2c 100644 --- a/tutorials/wireless/hub-to-device/pc-communication/main.py +++ b/tutorials/wireless/hub-to-device/pc-communication/main.py @@ -1,6 +1,3 @@ -# NOTE: Run this program with the latest -# firmware provided via https://beta.pybricks.com/ - from pybricks.pupdevices import Motor from pybricks.parameters import Port from pybricks.tools import wait