raw voltage graph

This commit is contained in:
leo 2023-06-16 18:13:31 +02:00
parent e862db5bb2
commit dd01087bf7
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 75 additions and 10 deletions

View File

@ -29,6 +29,7 @@ class ble_interface:
self.services: Dict[str, BleakGATTService] = {}
self.handlers = {
self.CURRENT: self.__current_meas_handler,
self.VOLTAGE: self.__voltage_meas_handler,
self.RANGE: self.__range_handler,
}
self.characteristics_gain: Dict[str, Dict[int, int]] = {} # for every service, links handle with gain
@ -43,13 +44,14 @@ class ble_interface:
self.log.info("Connected")
self.client = client
self.call_callback(self.ON_CONNECT)
services = client.services.services
self.parse_services(services)
await self.get_associated_gains(self.CURRENT)
await self.get_associated_gains(self.VOLTAGE)
self.call_callback(self.ON_CONNECT)
await asyncio.Event().wait()
@ -67,7 +69,7 @@ class ble_interface:
self.log.info("descr %d", gain)
if not self.characteristics_gain:
if not self.characteristics_gain.get(id):
self.characteristics_gain[id] = {}
self.characteristics_gain[id][c.handle] = gain
@ -125,6 +127,12 @@ class ble_interface:
self.call_callback(self.RANGE, val)
def __voltage_meas_handler(self, char: BleakGATTCharacteristic, data: bytearray):
val = struct.unpack("<I", data)[0]
gain = self.characteristics_gain[self.VOLTAGE][char.handle]
self.call_callback(self.VOLTAGE, gain, val)
async def get_refresh_delay(self) -> int:
s = self.services[self.CONFIGURATION]
c = s.get_characteristic(myUUIDs.SAMPLING_RATE_CHAR);

71
main.py
View File

@ -24,18 +24,29 @@ current_meas = {}
max_current = 0
start_time = None
voltage_meas = {}
max_voltage = 0
def clear_plot(sender, app_data):
logger.info("clear")
global current_meas
global start_time
start_time = None
global current_meas
global max_current
for d in current_meas:
current_meas[d] = []
start_time = None
max_current = 0
global voltage_meas
global max_voltage
for d in voltage_meas:
voltage_meas[d] = []
max_voltage = 0
def on_connect_handler(device: ble_interface):
device.subscribe_to(device.CURRENT)
device.subscribe_to(device.VOLTAGE)
device.subscribe_to(device.RANGE, normalize_uuid_str(myUUIDs.ELECTRIC_CURRENT_RANGE_CHAR))
loop.create_task(update_conf_values())
@ -64,8 +75,6 @@ def current_meas_handler(device: ble_interface, input_handle: int, newval: int):
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] = []
@ -88,6 +97,39 @@ def current_meas_handler(device: ble_interface, input_handle: int, newval: int):
txt += f"{h} : {current_meas[h][-1][1]} | "
dpg.set_value("last_current_values", txt)
def voltage_meas_handler(device: ble_interface, input_handle: int, newval: int):
value = newval
handle = input_handle
logger.info("%s: %f", handle, value * 1.0e-6)
global start_time
global voltage_meas
if(start_time is None):
start_time = time.monotonic_ns()
if(not handle in voltage_meas):
logger.info("%d %s", handle, voltage_meas)
voltage_meas[handle] = []
dpg.add_line_series([], [], label=f"{handle}", parent="y_axis_volts", tag=f"volt_serie{handle}")
voltage_meas[handle].append(((time.monotonic_ns() - start_time) * 1e-9, value))
dpg.set_value(f"volt_serie{handle}", [ [v[0] for v in voltage_meas[handle]], [v[1] for v in voltage_meas[handle]] ])
dpg.fit_axis_data("x_axis_volts")
# dpg.fit_axis_data("y_axis")
global max_voltage
max_voltage = max(max_voltage, newval)
dpg.set_axis_limits("y_axis_volts", 0., 1.1 * max_voltage)
txt = ""
for h in voltage_meas:
if(len(voltage_meas[h]) == 0):
continue
txt += f"{h} : {voltage_meas[h][-1][1]} | "
dpg.set_value("last_voltage_values", txt)
def range_changed_handler(device: ble_interface, val: int):
dpg.set_value("range", val)
@ -112,7 +154,10 @@ async def init_gui():
dpg.create_viewport()
dpg.setup_dearpygui()
with dpg.window(label="Controls", pos = [0, 0], width = 200, height = dpg.get_viewport_height()):
width = dpg.get_viewport_width()
height = dpg.get_viewport_height()
with dpg.window(label="Controls", pos = [0, 0], width = .2 * width, height = dpg.get_viewport_height()):
dpg.add_button(label="Connect", callback=device.connect)
dpg.add_input_int(label="Refresh Delay", callback=update_refresh_rate, on_enter=True, tag="refresh delay")
dpg.add_button(label="Zero", callback=device.zero_cali)
@ -121,10 +166,10 @@ async def init_gui():
dpg.add_checkbox(label="Auto-range", callback=set_auto_range, tag="auto_range")
dpg.add_input_int(label="Range", callback=set_range, on_enter=True, tag="range")
with dpg.window(label="Graphs", pos = [200, 0], width = dpg.get_viewport_width()-200, height = dpg.get_viewport_height()):
with dpg.window(label="Graphs", pos = [.2 * width, 0], width = .8 * width, height = dpg.get_viewport_height()):
dpg.add_button(label="Clear", callback=clear_plot)
dpg.add_text(label="Current", tag="last_current_values")
with dpg.plot(label="Current", width = -1, height = -1):
with dpg.plot(label="Current", width = -1, height = .5, tag="current_plot"):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis, label="x", tag="x_axis")
@ -133,6 +178,17 @@ async def init_gui():
dpg.set_axis_limits_auto("x_axis")
# dpg.set_axis_limits_auto("y_axis")
dpg.add_text(label="Voltage", tag="last_voltage_values")
with dpg.plot(label="Raw voltage", width = -1, height = .5, tag="voltage_plot"):
dpg.add_plot_legend()
dpg.add_plot_axis(dpg.mvXAxis, label="x", tag="x_axis_volts")
dpg.add_plot_axis(dpg.mvYAxis, label="y", tag="y_axis_volts")
dpg.set_axis_limits_auto("x_axis_volts")
# dpg.set_axis_limits_auto("y_axis")
dpg.show_viewport()
while dpg.is_dearpygui_running():
dpg.render_dearpygui_frame()
@ -151,6 +207,7 @@ if __name__ == "__main__":
device = ble_interface("84:F7:03:1B:C6:A2", loop, logger)
device.add_callback(device.ON_CONNECT, on_connect_handler)
device.add_callback(device.CURRENT, current_meas_handler)
device.add_callback(device.VOLTAGE, voltage_meas_handler)
device.add_callback(device.RANGE, range_changed_handler)
loop.create_task(init_gui())