66 lines
1.8 KiB
Python
66 lines
1.8 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")
|
|
|
|
# 2 imu 4 adc 6 ppg
|
|
async def connect(device):
|
|
async with BleakClient(device) as client:
|
|
#await client.write_gatt_char(TX_UUID, b'2', response=True)
|
|
await client.write_gatt_char(TX_UUID, b'4', response=True)
|
|
#await client.write_gatt_char(TX_UUID, b'6', response=True)
|
|
await client.stop_notify(RX_UUID)
|
|
await client.start_notify(RX_UUID, cb)
|
|
await asyncio.sleep(600)
|
|
#await client.write_gatt_char(TX_UUID, b'2', response=True)
|
|
#await client.write_gatt_char(TX_UUID, b'S', response=True)
|
|
await client.stop_notify(RX_UUID)
|
|
#await asyncio.sleep(5)
|
|
|
|
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()
|
|
start = time.time()
|
|
while(True):
|
|
data = await queue.get()
|
|
if len(cons) == 0:
|
|
size = int.from_bytes(data[:4], byteorder = 'little', signed = False)
|
|
cons = cons + data
|
|
if len(cons) == size + 4:
|
|
read_and_process(types, cons[4 : size + 4], size)
|
|
cons = bytes()
|
|
start = time.time()
|
|
if len(cons) > size + 4:
|
|
print("Misaligned")
|
|
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())
|
|
|