This commit is contained in:
2020-08-12 18:44:23 +02:00
parent ee7d7c5559
commit 96bb8a1fb1
14 changed files with 252 additions and 692 deletions

15
zei/BatteryLevelChar.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from bluepy import btle
import ZeiCharBase
# pylint: disable=E1101
class BatteryLevelChar(ZeiCharBase.ZeiCharBase):
svcUUID = btle.AssignedNumbers.battery_service
charUUID = btle.AssignedNumbers.battery_level
def __init__(self, periph):
super.__init__(self, periph)

8
zei/Log.py Normal file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import logging
_log = logging.getLogger(__name__)
_log.addHandler(logging.StreamHandler())
_log.setLevel(logging.INFO)

17
zei/Zei.py Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from bluepy import btle
from . import ZeiOrientationChar
from . import ZeiDelegate
class Zei(btle.Peripheral):
def __init__(self, *args, **kwargs):
btle.Peripheral.__init__(self, *args, **kwargs)
self.withDelegate(ZeiDelegate.ZeiDelegate(self))
# activate notifications about turn
self.orientation = ZeiOrientationChar.ZeiOrientationChar(self)
self.orientation.enable()

24
zei/ZeiCharBase.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from bluepy import btle
import struct
class ZeiCharBase(object):
def __init__(self, periph):
self.periph = periph
self.hndl = None
#self.svcUUID = None
#self.charUUID = None
# pylint: disable=E1101
def enable(self):
_svc = self.periph.getServiceByUUID(self.svcUUID)
_chr = _svc.getCharacteristics(self.charUUID)[0]
self.hndl = _chr.getHandle()
# this is uint16_t - see: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
_cccd = _chr.getDescriptors(btle.AssignedNumbers.client_characteristic_configuration)[0]
_cccd.write(struct.pack("<H", 2), withResponse=True)

20
zei/ZeiDelegate.py Normal file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import struct
from bluepy import btle
from .Log import _log
class ZeiDelegate(btle.DefaultDelegate):
def __init__(self, periph):
btle.DefaultDelegate.__init__(self)
self.parent = periph
def handleNotification(self, cHandle, data):
if cHandle == 38:
side = struct.unpack('B', data)[0]
_log.info("Current side up is %s", side )
else:
_log.info("Notification from hndl: %s - %r", cHandle, data)

23
zei/ZeiDiscovery.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from bluepy import btle
class ZeiDiscovery(btle.Scanner):
def __init__(self, periph=None, **kwargs):
self.zei = periph
btle.Scanner.__init__(self, **kwargs)
#self.withDelegate(ZeiDiscoveryDelegate(self, self.zei))
#self.stop_scanning = False
def reconnect(self):
self.iface=self.zei.iface
self.clear()
self.start()
while self.zei.addr not in self.scanned:
self.process(timeout=2)
self.stop()
self.zei.connect(self.scanned[self.zei.addr])

View File

@ -0,0 +1,33 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from bluepy import btle
from Log import _log
class ZeiDiscoveryDelegate(btle.DefaultDelegate):
def __init__(self, scanner, periph):
btle.DefaultDelegate.__init__(self)
self.scanner = scanner
self.periph = periph
def handleDiscovery(self, dev, isNewDev, isNewData):
if not dev.addr == 'f1:05:a5:9c:2e:9b':
return
_log.info("Device %s (%s), RSSI=%d dB", dev.addr, dev.addrType, dev.rssi)
for (_, desc, value) in dev.getScanData():
_log.info(" %s = %s", desc, value)
# reconnect
# bluepy can only do one thing at a time, so stop scanning while trying to connect
# this is not supported by bluepy
#self.scanner.stop()
try:
self.periph.connect(dev)
self.scanner.stop_scanning = True
except:
# re
self.scanner.start()
pass

15
zei/ZeiOrientationChar.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from . import ZeiCharBase
def _ZEI_UUID(short_uuid):
return 'c7e7%04X-c847-11e6-8175-8c89a55d403c' % (short_uuid)
class ZeiOrientationChar(ZeiCharBase.ZeiCharBase):
svcUUID = _ZEI_UUID(0x0010)
charUUID = _ZEI_UUID(0x0012)
def __init__(self, periph):
ZeiCharBase.ZeiCharBase.__init__(self, periph)

1
zei/__init__.py Normal file
View File

@ -0,0 +1 @@
# Empty