Files
chest_strap/code/l452_code/packet_parser_ble.py
T
2026-05-08 17:08:07 -05:00

56 lines
1.3 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
import time
import asyncio
from bleak import BleakScanner, BleakClient
from packet_parser_helpers import *
async def scan():
return await BleakScanner.find_device_by_name("XX-STM32")
async def connect(device):
async with BleakClient(device) as client:
value = bytes([0x01])
#await client.write_gatt_char(TX_UUID, value, response=True)
await client.start_notify(RX_UUID, cb)
await asyncio.sleep(10000)
RX_UUID = "00000001-8E22-4541-9D4C-21EDAE82ED19"
TX_UUID = "00000000-8E22-4541-9D4C-21EDAE82ED19"
queue = asyncio.Queue()
def cb(sender, data):
queue.put_nowait(data)
async def update_with_data():
types = get_type_list(packet_definitions)
cons = bytes()
while(True):
data = await queue.get()
if len(cons) == 0:
size = int.from_bytes(data[:4], byteorder = 'little', signed = False)
cons = cons + data
assert len(cons) <= size
if len(cons) == size:
read_and_process(types, cons, size)
cons = bytes()
async def main():
device = await scan()
if not device:
print("Device not found")
return
consumer_task = asyncio.create_task(update_with_data())
await connect(device)
asyncio.run(main())