measure gain

This commit is contained in:
leo 2023-06-16 17:34:43 +02:00
parent fe30533868
commit e862db5bb2
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 27 additions and 3 deletions

View File

@ -1,6 +1,6 @@
from asyncio import AbstractEventLoop
import asyncio
from typing import Dict
from typing import Dict, dataclass_transform
from bleak import BleakClient, BleakScanner
from bleak.backends.characteristic import BleakGATTCharacteristic
@ -31,6 +31,7 @@ class ble_interface:
self.CURRENT: self.__current_meas_handler,
self.RANGE: self.__range_handler,
}
self.characteristics_gain: Dict[str, Dict[int, int]] = {} # for every service, links handle with gain
async def __connect(self):
device = await BleakScanner.find_device_by_address(self.address, cb=dict(use_bdaddr=False))
@ -48,8 +49,28 @@ class ble_interface:
self.parse_services(services)
await self.get_associated_gains(self.CURRENT)
await asyncio.Event().wait()
async def get_associated_gains(self, id: str):
s = self.services[id]
for c in s.characteristics:
self.log.info(c.descriptors)
descr = c.get_descriptor(normalize_uuid_str(myUUIDs.SOURCE_GAIN_DESCR))
if(not descr):
self.log.info("descr not found")
continue
data = await self.client.read_gatt_descriptor(descr.handle);
gain = struct.unpack("<I", data)[0]
self.log.info("descr %d", gain)
if not self.characteristics_gain:
self.characteristics_gain[id] = {}
self.characteristics_gain[id][c.handle] = gain
def parse_services(self, services: Dict[int, BleakGATTService]):
for i in services:
s = services[i]
@ -95,9 +116,9 @@ class ble_interface:
def __current_meas_handler(self, char: BleakGATTCharacteristic, data: bytearray):
val = struct.unpack("<I", data)[0]
handle = next(i for i,c in enumerate(self.services[self.CURRENT].characteristics) if c.handle == char.handle)
gain = self.characteristics_gain[self.CURRENT][char.handle]
self.call_callback(self.CURRENT, handle, val)
self.call_callback(self.CURRENT, gain, val)
def __range_handler(self, char: BleakGATTCharacteristic, data: bytearray):
val = struct.unpack("<i", data)[0]

View File

@ -22,3 +22,6 @@ UNITLESS_UNIT_UUID = "2700"
NSP_DESC_MAIN = "0106"
NSP_DESC_INSIDE = "010B"
NSP_DESC_OUTSIDE = "010C"
SOURCE_GAIN_DESCR = "2920"