Further ways to simplify code

This commit is contained in:
Danny Staple 2022-11-03 22:13:48 +00:00
parent 0a8a86960d
commit 8e8783d9b2
3 changed files with 11 additions and 16 deletions

View File

@ -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)

View File

@ -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())

View File

@ -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)