60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
import asyncio
|
|
import json
|
|
import matplotlib.pyplot as plt
|
|
|
|
from robot_ble_connection import BleConnection
|
|
|
|
|
|
class RobotDisplay:
|
|
def __init__(self):
|
|
self.ble_connection = BleConnection(self.handle_data)
|
|
self.buffer = ""
|
|
self.arena = {}
|
|
self.closed = False
|
|
self.fig, self.ax = plt.subplots()
|
|
|
|
def handle_close(self, _):
|
|
self.closed = True
|
|
|
|
def handle_data(self, data):
|
|
self.buffer += data.decode("utf-8")
|
|
while "\n" in self.buffer:
|
|
line, self.buffer = self.buffer.split("\n", 1)
|
|
print(f"Received data: {line}")
|
|
try:
|
|
message = json.loads(line)
|
|
except ValueError:
|
|
print("Error parsing JSON")
|
|
return
|
|
if "arena" in message:
|
|
self.arena = message
|
|
|
|
def draw(self):
|
|
self.ax.clear()
|
|
if self.arena:
|
|
for line in self.arena["arena"]:
|
|
self.ax.plot(
|
|
[line[0][0], line[1][0]], [line[0][1], line[1][1]], color="black"
|
|
)
|
|
|
|
async def main(self):
|
|
plt.ion()
|
|
await self.ble_connection.connect()
|
|
try:
|
|
request = json.dumps({"command": "arena"}).encode()
|
|
print(f"Sending request for arena: {request}")
|
|
await self.ble_connection.send_uart_data(request)
|
|
self.fig.canvas.mpl_connect("close_event", self.handle_close)
|
|
|
|
while not self.closed:
|
|
self.draw()
|
|
plt.draw()
|
|
plt.pause(0.05)
|
|
await asyncio.sleep(0.01)
|
|
finally:
|
|
await self.ble_connection.close()
|
|
|
|
|
|
robot_display = RobotDisplay()
|
|
asyncio.run(robot_display.main())
|