graph
This commit is contained in:
parent
f03dced25c
commit
cd7c5401b5
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
__pycache__
|
||||
client-venv
|
62
main.py
62
main.py
@ -3,21 +3,49 @@ 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]
|
||||
logger.info("%s: %f", characteristic.description, value * 1.0e-6)
|
||||
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 main():
|
||||
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))
|
||||
@ -42,6 +70,32 @@ async def main():
|
||||
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(
|
||||
@ -50,5 +104,5 @@ if __name__ == "__main__":
|
||||
)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(main())
|
||||
loop.run_forever()
|
||||
loop.create_task(init_gui())
|
||||
loop.run_forever()
|
||||
|
Loading…
x
Reference in New Issue
Block a user