Skip to content

Commit

Permalink
Critical fix for Python 3 use of pygatt backend.
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
hbldh committed Sep 14, 2016
1 parent c4b6bf6 commit 3deb542
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
5 changes: 2 additions & 3 deletions examples/accelerometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
print("New client created: {0}".format(c))



def acc_callback(data):
"""Handle a (epoch, (x,y,z)) accelerometer tuple."""
print("Epoch time: [{0}] - X: {1}, Y: {2}, Z: {3}".format(data[0], *data[1]))


print("Write accelerometer settings...")
c.accelerometer.set_settings(data_rate=200.0, data_range=8.0)
c.accelerometer.set_settings(data_rate=100.0, data_range=4.0)
print("Subscribing to accelerometer signal notifications...")
c.accelerometer.high_frequency_stream = True
c.accelerometer.high_frequency_stream = False
c.accelerometer.notifications(acc_callback)

time.sleep(20.0)
Expand Down
4 changes: 2 additions & 2 deletions pymetawear/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from pymetawear.utils import IS_64_BIT

# Version information.
__version__ = '0.5.0a2'
__version__ = '0.5.0a3'
version = __version__ # backwards compatibility name
version_info = (0, 5, 0, 'a2')
version_info = (0, 5, 0, 'a3')

if os.environ.get('METAWEAR_LIB_SO_NAME') is not None:
libmetawear = cdll.LoadLibrary(os.environ["METAWEAR_LIB_SO_NAME"])
Expand Down
23 changes: 22 additions & 1 deletion pymetawear/backends/pygatt/gatttool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import pygatt
from pygatt import exceptions
from pygatt.backends.gatttool.gatttool import log, GATTToolBLEDevice
from pygatt.backends.gatttool.gatttool import log, GATTToolBLEDevice, \
DEFAULT_CONNECT_TIMEOUT_S, NotificationTimeout, NotConnectedError

from pymetawear.utils import string_types

Expand Down Expand Up @@ -71,6 +72,26 @@ class PyMetaWearGATTToolBackend(pygatt.backends.GATTToolBackend):
def __init__(self, *args, **kwargs):
super(PyMetaWearGATTToolBackend, self).__init__(*args, **kwargs)

def connect(self, address, timeout=DEFAULT_CONNECT_TIMEOUT_S,
address_type='public'):
log.info('Connecting with timeout=%s', timeout)
self.sendline('sec-level low')
self._address = address

try:
cmd = 'connect {0} {1}'.format(self._address, address_type)
with self._receiver.event("connect", timeout):
self.sendline(cmd)
except NotificationTimeout:
message = "Timed out connecting to {0} after {1} seconds.".format(
self._address, timeout
)
log.error(message)
raise NotConnectedError(message)

self._connected_device = PyMetaWearGATTToolBLEDevice(address, self)
return self._connected_device

def _handle_notification_string(self, event):
msg = event["after"]
if not msg:
Expand Down
30 changes: 15 additions & 15 deletions pymetawear/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
from pymetawear import modules
try:
from pymetawear.backends.pygatt import PyGattBackend
except ImportError:
PyGattBackend = None
except ImportError as e:
PyGattBackend = e
try:
from pymetawear.backends.pybluez import PyBluezBackend
except ImportError:
PyBluezBackend = None
except ImportError as e:
PyBluezBackend = e
try:
from pymetawear.backends.bluepy import BluepyBackend
except ImportError:
BluepyBackend = None
except ImportError as e:
BluepyBackend = e

PYMETAWEAR_TIMEOUT = os.environ.get('PYMETAWEAR_TIMEOUT', )

Expand Down Expand Up @@ -110,25 +110,25 @@ def __init__(self, address, backend='pygatt',
print("Creating MetaWearClient for {0}...".format(address))

if backend == 'pygatt':
if PyGattBackend is None:
if isinstance(PyGattBackend, Exception):
raise PyMetaWearException(
"Need to install pygatt[GATTTOOL] package to use this backend.")
"pygatt[GATTTOOL] package error :{0}".format(PyGattBackend))
self._backend = PyGattBackend(
self._address, interface=interface,
self._address, interface=interface,
timeout=PYMETAWEAR_TIMEOUT if timeout is None else timeout, debug=debug)
elif backend == 'pybluez':
if PyBluezBackend is None:
if isinstance(PyBluezBackend, Exception):
raise PyMetaWearException(
"Need to install pybluez[ble] package to use this backend.")
"pybluez[ble] package error: {0}".format(PyBluezBackend))
self._backend = PyBluezBackend(
self._address, interface=interface,
self._address, interface=interface,
timeout=PYMETAWEAR_TIMEOUT if timeout is None else timeout, debug=debug)
elif backend == 'bluepy':
if BluepyBackend is None:
if isinstance(BluepyBackend, Exception):
raise PyMetaWearException(
"Need to install bluepy package to use this backend.")
"bluepy package error: {0}".format(BluepyBackend))
self._backend = BluepyBackend(
self._address, interface=interface,
self._address, interface=interface,
timeout=PYMETAWEAR_TIMEOUT if timeout is None else timeout, debug=debug)
else:
raise PyMetaWearException("Unknown backend: {0}".format(backend))
Expand Down

0 comments on commit 3deb542

Please sign in to comment.