measure gain
This commit is contained in:
parent
fe30533868
commit
e862db5bb2
@ -1,6 +1,6 @@
|
|||||||
from asyncio import AbstractEventLoop
|
from asyncio import AbstractEventLoop
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import Dict
|
from typing import Dict, dataclass_transform
|
||||||
|
|
||||||
from bleak import BleakClient, BleakScanner
|
from bleak import BleakClient, BleakScanner
|
||||||
from bleak.backends.characteristic import BleakGATTCharacteristic
|
from bleak.backends.characteristic import BleakGATTCharacteristic
|
||||||
@ -31,6 +31,7 @@ class ble_interface:
|
|||||||
self.CURRENT: self.__current_meas_handler,
|
self.CURRENT: self.__current_meas_handler,
|
||||||
self.RANGE: self.__range_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):
|
async def __connect(self):
|
||||||
device = await BleakScanner.find_device_by_address(self.address, cb=dict(use_bdaddr=False))
|
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)
|
self.parse_services(services)
|
||||||
|
|
||||||
|
await self.get_associated_gains(self.CURRENT)
|
||||||
|
|
||||||
await asyncio.Event().wait()
|
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]):
|
def parse_services(self, services: Dict[int, BleakGATTService]):
|
||||||
for i in services:
|
for i in services:
|
||||||
s = services[i]
|
s = services[i]
|
||||||
@ -95,9 +116,9 @@ class ble_interface:
|
|||||||
|
|
||||||
def __current_meas_handler(self, char: BleakGATTCharacteristic, data: bytearray):
|
def __current_meas_handler(self, char: BleakGATTCharacteristic, data: bytearray):
|
||||||
val = struct.unpack("<I", data)[0]
|
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):
|
def __range_handler(self, char: BleakGATTCharacteristic, data: bytearray):
|
||||||
val = struct.unpack("<i", data)[0]
|
val = struct.unpack("<i", data)[0]
|
||||||
|
@ -22,3 +22,6 @@ UNITLESS_UNIT_UUID = "2700"
|
|||||||
NSP_DESC_MAIN = "0106"
|
NSP_DESC_MAIN = "0106"
|
||||||
NSP_DESC_INSIDE = "010B"
|
NSP_DESC_INSIDE = "010B"
|
||||||
NSP_DESC_OUTSIDE = "010C"
|
NSP_DESC_OUTSIDE = "010C"
|
||||||
|
|
||||||
|
SOURCE_GAIN_DESCR = "2920"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user