From f9c9b9fc8c909920643b9f82df2f315fd7da32b1 Mon Sep 17 00:00:00 2001 From: Lucas Oskorep Date: Fri, 6 Mar 2020 10:32:43 -0600 Subject: [PATCH] giving loop to other classes may keep the other loops from crashing/becoming un-useable. --- kano-wand-2/shop.py | 12 +++--------- kano-wand-2/wand.py | 24 ++++++++++++++++-------- kano-wand-test.py | 15 ++++++--------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/kano-wand-2/shop.py b/kano-wand-2/shop.py index 2e3be51..d17d507 100644 --- a/kano-wand-2/shop.py +++ b/kano-wand-2/shop.py @@ -4,13 +4,14 @@ from wand import Wand class Shop(object): """A scanner class to connect to wands """ - def __init__(self, wand_class=Wand, debug=False): + def __init__(self, shop_loop, wand_class=Wand, debug=False): """Create a new scanner Keyword Arguments: wand_class {class} -- Class to use when connecting to wand (default: {Wand}) debug {bool} -- Print debug messages (default: {False}) """ + self.shop_loop = shop_loop self.wand_class = wand_class self.debug = debug self._name = None @@ -39,27 +40,20 @@ class Shop(object): except AssertionError as e: print("Either a name, prefix, or mac address must be provided to find a wand") raise e - - if prefix is not None: self._prefix = prefix elif mac is not None: self._mac = mac - self.wands = [] - devices = await discover(timeout= timeout) - print(devices) - if self._prefix: devices = list(filter(lambda x : x.name.startswith(self._prefix), devices)) if self._mac: devices = list(filter(lambda x : x.address == self.mac, devices)) - print(devices) - self.wands = [Wand(d.address, d.name) for d in devices] + self.wands = [Wand(d.address, d.name, self.shop_loop) for d in devices] print(self.wands) if connect: for wand in self.wands: diff --git a/kano-wand-2/wand.py b/kano-wand-2/wand.py index 6b410d7..db58f97 100644 --- a/kano-wand-2/wand.py +++ b/kano-wand-2/wand.py @@ -10,7 +10,7 @@ class Wand(object): """A wand class to interact with the Kano wand """ - def __init__(self, device_addr, name, debug=True): + def __init__(self, device_addr, name, bot_loop, debug=True): """Create a new wand Arguments: @@ -23,6 +23,7 @@ class Wand(object): self.debug = debug self._dev = BleakClient(device_addr) self.name = name + self.bot_loop = bot_loop if debug: print("Wand: {}\n\rWand Mac: {}".format(self.name, self._dev.address)) @@ -532,7 +533,7 @@ class Wand(object): callback(val) - def on_button(self, value): + async def on_button(self, value): """Function called on button notification Arguments: @@ -566,7 +567,7 @@ class Wand(object): pass - def _on_battery(self, data): + async def _on_battery(self, data): """Private function for battery notification Arguments: @@ -577,12 +578,12 @@ class Wand(object): if self.debug: print("Battery: {}".format(val)) - self.on_battery(val) + await self.on_battery(val) for callback in self._battery_callbacks.values(): - callback(val) + await callback(val) - def on_battery(self, value): + async def on_battery(self, value): """Function called on battery notification Arguments: @@ -590,7 +591,6 @@ class Wand(object): """ pass - def handle_notification(self, sender, data): """Handle notifications subscribed to @@ -598,11 +598,19 @@ class Wand(object): cHandle {int} -- Handle of notification data {bytes} -- Data from device """ + import asyncio + future = None + print("Handle notification") if sender == SENSOR.QUATERNIONS_CHAR.value: self._on_position(data) elif sender == IO.USER_BUTTON_CHAR.value: - self._on_button(data) + # self._on_button(data) + future = asyncio.run_coroutine_threadsafe(self._on_button(data), self.bot_loop) + elif sender == SENSOR.TEMP_CHAR.value: self._on_temperature(data) elif sender == IO.BATTERY_CHAR.value: self._on_battery(data) + + if future != None: + print("future result is - ", future.result()) diff --git a/kano-wand-test.py b/kano-wand-test.py index 18dc3c1..e88ebb3 100644 --- a/kano-wand-test.py +++ b/kano-wand-test.py @@ -17,24 +17,21 @@ class MyWand(Wand): # # Vibrate the wand and set its color to red # self.set_led(self.colors.pop()) # # Subscribe to notifications - # self.subscribe_button() + self.subscribe_button() # Button callback, automatically called after connecting to wand - def on_button(self, pressed): + async def on_button(self, pressed): if pressed: # Unsubscribe from the position callback - # NOTE You could pass `continue_notifications=True` - # to continue using the wand's `on_position` function - - self.set_led(self.colors.pop()) + await self.set_led(self.colors.pop()) # Disconnect if we run out of colors if len(self.colors) == 0: - self.disconnect() + await self.disconnect() async def main(): # Create a new wand scanner - shop = Shop(wand_class=MyWand, debug=True) + shop = Shop(asyncio.get_running_loop(), wand_class=MyWand, debug=True) wands = [] try: # While we don't have any wands @@ -45,7 +42,7 @@ async def main(): # For each wand (Only tested with one) for wand in wands: # Vibrate the wand and set its color to red - wand.vibrate(PATTERN.BURST) + await wand.vibrate(PATTERN.BURST) # Callback for position # def onPos(x, y, pitch, roll):