diff --git a/ch-13/1-modelling-space/computer/display_arena_from_robot.py b/ch-13/1-modelling-space/computer/display_arena_from_robot.py index 8c36ceb..548a4dd 100644 --- a/ch-13/1-modelling-space/computer/display_arena_from_robot.py +++ b/ch-13/1-modelling-space/computer/display_arena_from_robot.py @@ -8,14 +8,11 @@ from robot_ble_connection import BleConnection class RobotDisplay: def __init__(self): - self.fig, self.ax = plt.subplots() self.ble_connection = BleConnection(self.handle_data) + self.line = "" self.arena = None self.display_closed = False - # handle closed event - self.fig.canvas.mpl_connect("close_event", self.handle_close) - self.line = "" def handle_close(self, _): self.display_closed = True @@ -33,13 +30,13 @@ class RobotDisplay: def update(self, arena): self.arena = arena - self.ax.clear() + plt.gca().clear() for line in arena["arena"]: - self.ax.plot( + plt.gca().plot( [line[0][0], line[1][0]], [line[0][1], line[1][1]], color="black" ) for line in arena["target_zone"]: - self.ax.plot( + plt.gca().plot( [line[0][0], line[1][0]], [line[0][1], line[1][1]], color="red" ) @@ -48,9 +45,10 @@ class RobotDisplay: try: await self.ble_connection.connect() - request = json.dumps({"type": "arena"}).encode() + request = json.dumps({"command": "arena"}).encode() print(f"Sending request for arena: {request}") - self.ble_connection.send_uart_data(request) + await self.ble_connection.send_uart_data(request) + plt.gcf().canvas.mpl_connect("close_event", self.handle_close) while not self.display_closed: plt.pause(0.05) diff --git a/ch-13/1-modelling-space/computer/find_devices.py b/ch-13/1-modelling-space/computer/find_devices.py index 829f62c..de4035e 100644 --- a/ch-13/1-modelling-space/computer/find_devices.py +++ b/ch-13/1-modelling-space/computer/find_devices.py @@ -8,6 +8,6 @@ async def run(): print(f"Found {len(devices)} devices") print([device.name for device in devices]) ble_device_info = [device for device in devices if device.name==ble_name][0] - print("Found robot {}...".format(ble_device_info.name)) + print(f"Found robot {ble_device_info.name}...") asyncio.run(run()) diff --git a/ch-13/1-modelling-space/computer/robot_ble_connection.py b/ch-13/1-modelling-space/computer/robot_ble_connection.py index 9ea514c..88b25b5 100644 --- a/ch-13/1-modelling-space/computer/robot_ble_connection.py +++ b/ch-13/1-modelling-space/computer/robot_ble_connection.py @@ -24,7 +24,7 @@ class BleConnection: print(f"Found {len(devices)} devices") print([device.name for device in devices]) ble_device_info = [device for device in devices if device.name==self.ble_name][0] - print("Connecting to {}...".format(ble_device_info.name)) + print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() print("Connected to {}".format(ble_device_info.name)) @@ -35,8 +35,5 @@ class BleConnection: async def close(self): await self.ble_client.disconnect() - def send_uart_data(self, data: bytes): - if self.ble_client: - asyncio.create_task( - self.ble_client.write_gatt_char(self.adafruit_tx_uuid, data) - ) + async def send_uart_data(self, data: bytes): + await self.ble_client.write_gatt_char(self.adafruit_tx_uuid, data)