fused currend

This commit is contained in:
leo 2023-06-17 17:17:20 +02:00
parent ab7a2ad503
commit 1246fc2962
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 19 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import myUUIDs
class ble_interface:
CURRENT = "current"
CURRENT_F = "current_f"
VOLTAGE = "volts"
CONFIGURATION = "conf"
RANGE = "range"
@ -29,6 +30,7 @@ class ble_interface:
self.services: Dict[str, BleakGATTService] = {}
self.handlers = {
self.CURRENT: self.__current_meas_handler,
self.CURRENT_F: self.__current_f_meas_handler,
self.VOLTAGE: self.__voltage_meas_handler,
self.RANGE: self.__range_handler,
}
@ -78,7 +80,10 @@ class ble_interface:
s = services[i]
if(s.uuid == normalize_uuid_str(myUUIDs.METROLOGY_SERVICE)):
if(s.characteristics[0].uuid == normalize_uuid_str(myUUIDs.ELECTRIC_CURRENT_CHAR)):
self.services[self.CURRENT] = s
if(len(s.characteristics) == 1):
self.services[self.CURRENT_F] = s
else:
self.services[self.CURRENT] = s
if(s.characteristics[0].uuid == normalize_uuid_str(myUUIDs.VOLTAGE_CHAR)):
self.services[self.VOLTAGE] = s
@ -122,6 +127,12 @@ class ble_interface:
self.call_callback(self.CURRENT, gain, val)
def __current_f_meas_handler(self, char: BleakGATTCharacteristic, data: bytearray):
val = struct.unpack("<I", data)[0]
self.call_callback(self.CURRENT_F, "Fused", val)
def __range_handler(self, char: BleakGATTCharacteristic, data: bytearray):
val = struct.unpack("<i", data)[0]

12
main.py
View File

@ -46,6 +46,7 @@ def clear_plot(sender, app_data):
def on_connect_handler(device: ble_interface):
device.subscribe_to(device.CURRENT)
device.subscribe_to(device.CURRENT_F)
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,7 +65,7 @@ async def update_conf_values():
dpg.set_value("range", range)
def current_meas_handler(device: ble_interface, input_handle: int, newval: int):
def current_meas_handler(device: ble_interface, input_handle: str, newval: int):
value = newval
handle = input_handle
logger.info("%s: %f", handle, value * 1.0e-6)
@ -76,7 +77,7 @@ def current_meas_handler(device: ble_interface, input_handle: int, newval: int):
start_time = time.monotonic_ns()
if(not handle in current_meas):
logger.info("%d %s", handle, current_meas)
logger.info("%s %s", handle, current_meas)
current_meas[handle] = []
dpg.add_line_series([], [], label=f"{handle}", parent="y_axis", tag=f"serie{handle}")
@ -194,9 +195,9 @@ async def init_gui():
# dpg.set_axis_limits_auto("y_axis")
with dpg.window(label="Calibration controls", pos = [0, 0], width = .2 * width, height = -1):
dpg.add_input_text(label="path", tag="cali_save_path")
dpg.add_input_int(label="Voltage", callback=save_cali, on_enter=True, tag="save_cali")
# with dpg.window(label="Calibration controls", pos = [0, 0], width = .2 * width, height = -1):
# dpg.add_input_text(label="path", tag="cali_save_path")
# dpg.add_input_int(label="Voltage", callback=save_cali, on_enter=True, tag="save_cali")
dpg.show_viewport()
while dpg.is_dearpygui_running():
@ -216,6 +217,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.CURRENT_F, current_meas_handler)
device.add_callback(device.VOLTAGE, voltage_meas_handler)
device.add_callback(device.RANGE, range_changed_handler)