109 lines
3.2 KiB
Python
109 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
import argparse
|
|
import asyncio
|
|
import logging
|
|
import struct
|
|
import time
|
|
|
|
from bleak import BleakClient, BleakScanner
|
|
from bleak.backends.characteristic import BleakGATTCharacteristic
|
|
from bleak.uuids import normalize_uuid_str
|
|
|
|
import dearpygui.dearpygui as dpg
|
|
|
|
import myUUIDs
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
loop = None
|
|
|
|
current_meas = {}
|
|
start_time = None
|
|
|
|
def current_meas_handler(characteristic: BleakGATTCharacteristic, data: bytearray):
|
|
value = struct.unpack("<I", data)[0]
|
|
handle = characteristic.handle
|
|
logger.info("%s: %f", handle, value * 1.0e-6)
|
|
|
|
global start_time
|
|
global current_meas
|
|
|
|
if(start_time is None):
|
|
start_time = time.monotonic_ns()
|
|
|
|
start_time += 1
|
|
|
|
if(not handle in current_meas):
|
|
logger.info("%d %s", handle, current_meas)
|
|
current_meas[handle] = []
|
|
dpg.add_line_series([], [], label=f"{handle}", parent="y_axis", tag=f"serie{handle}")
|
|
|
|
current_meas[handle].append(((time.monotonic_ns() - start_time) * 1e-9, value * 1e-6))
|
|
|
|
dpg.set_value(f"serie{handle}", [ [v[0] for v in current_meas[handle]], [v[1] for v in current_meas[handle]] ])
|
|
dpg.fit_axis_data("x_axis")
|
|
dpg.fit_axis_data("y_axis")
|
|
|
|
|
|
async def connect():
|
|
logger.info("starting scan...")
|
|
|
|
device = await BleakScanner.find_device_by_address("84:F7:03:1B:C6:A2", cb=dict(use_bdaddr=False))
|
|
if device is None:
|
|
logger.error("could not find device with address")
|
|
return
|
|
|
|
logger.info("connecting to device...")
|
|
|
|
async with BleakClient(device) as client:
|
|
logger.info("Connected")
|
|
|
|
current_svc_handle = 0
|
|
services = client.services.services
|
|
metrology_svcs = [services[i] for i in services if services[i].uuid == normalize_uuid_str(myUUIDs.METROLOGY_SERVICE)]
|
|
|
|
current_meas_svc = next(s for s in metrology_svcs if s.get_characteristic(myUUIDs.ELECTRIC_CURRENT_CHAR))
|
|
|
|
for c in current_meas_svc.characteristics:
|
|
await client.start_notify(c, current_meas_handler)
|
|
|
|
await asyncio.Event().wait()
|
|
# await client.stop_notify(args.characteristic)
|
|
|
|
def start_connection():
|
|
loop.create_task(connect())
|
|
|
|
async def init_gui():
|
|
dpg.create_context()
|
|
dpg.create_viewport()
|
|
dpg.setup_dearpygui()
|
|
|
|
with dpg.window(label="Controls", pos = [0, 0], width = dpg.get_viewport_width(), height = 100):
|
|
dpg.add_button(label="Connect", callback=start_connection)
|
|
|
|
with dpg.window(label="Graphs", pos = [0, 100]):
|
|
with dpg.plot(label="Current", width = -1, height = -1):
|
|
dpg.add_plot_legend()
|
|
|
|
dpg.add_plot_axis(dpg.mvXAxis, label="x", tag="x_axis")
|
|
dpg.add_plot_axis(dpg.mvYAxis, label="y", tag="y_axis")
|
|
|
|
dpg.set_axis_limits_auto("x_axis")
|
|
dpg.set_axis_limits_auto("y_axis")
|
|
|
|
dpg.show_viewport()
|
|
while dpg.is_dearpygui_running():
|
|
dpg.render_dearpygui_frame()
|
|
await asyncio.sleep(0)
|
|
dpg.destroy_context()
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(
|
|
level="INFO",
|
|
format="%(asctime)-15s %(name)-8s %(levelname)s: %(message)s",
|
|
)
|
|
|
|
loop = asyncio.get_event_loop()
|
|
loop.create_task(init_gui())
|
|
loop.run_forever()
|