giving loop to other classes may keep the other loops from crashing/becoming un-useable.
This commit is contained in:
+3
-9
@@ -4,13 +4,14 @@ from wand import Wand
|
|||||||
class Shop(object):
|
class Shop(object):
|
||||||
"""A scanner class to connect to wands
|
"""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
|
"""Create a new scanner
|
||||||
|
|
||||||
Keyword Arguments:
|
Keyword Arguments:
|
||||||
wand_class {class} -- Class to use when connecting to wand (default: {Wand})
|
wand_class {class} -- Class to use when connecting to wand (default: {Wand})
|
||||||
debug {bool} -- Print debug messages (default: {False})
|
debug {bool} -- Print debug messages (default: {False})
|
||||||
"""
|
"""
|
||||||
|
self.shop_loop = shop_loop
|
||||||
self.wand_class = wand_class
|
self.wand_class = wand_class
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
self._name = None
|
self._name = None
|
||||||
@@ -39,27 +40,20 @@ class Shop(object):
|
|||||||
except AssertionError as e:
|
except AssertionError as e:
|
||||||
print("Either a name, prefix, or mac address must be provided to find a wand")
|
print("Either a name, prefix, or mac address must be provided to find a wand")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
if prefix is not None:
|
if prefix is not None:
|
||||||
self._prefix = prefix
|
self._prefix = prefix
|
||||||
elif mac is not None:
|
elif mac is not None:
|
||||||
self._mac = mac
|
self._mac = mac
|
||||||
|
|
||||||
self.wands = []
|
self.wands = []
|
||||||
|
|
||||||
devices = await discover(timeout= timeout)
|
devices = await discover(timeout= timeout)
|
||||||
|
|
||||||
print(devices)
|
print(devices)
|
||||||
|
|
||||||
if self._prefix:
|
if self._prefix:
|
||||||
devices = list(filter(lambda x : x.name.startswith(self._prefix), devices))
|
devices = list(filter(lambda x : x.name.startswith(self._prefix), devices))
|
||||||
if self._mac:
|
if self._mac:
|
||||||
devices = list(filter(lambda x : x.address == self.mac, devices))
|
devices = list(filter(lambda x : x.address == self.mac, devices))
|
||||||
|
|
||||||
print(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)
|
print(self.wands)
|
||||||
if connect:
|
if connect:
|
||||||
for wand in self.wands:
|
for wand in self.wands:
|
||||||
|
|||||||
+16
-8
@@ -10,7 +10,7 @@ class Wand(object):
|
|||||||
"""A wand class to interact with the Kano wand
|
"""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
|
"""Create a new wand
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@@ -23,6 +23,7 @@ class Wand(object):
|
|||||||
self.debug = debug
|
self.debug = debug
|
||||||
self._dev = BleakClient(device_addr)
|
self._dev = BleakClient(device_addr)
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.bot_loop = bot_loop
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print("Wand: {}\n\rWand Mac: {}".format(self.name, self._dev.address))
|
print("Wand: {}\n\rWand Mac: {}".format(self.name, self._dev.address))
|
||||||
@@ -532,7 +533,7 @@ class Wand(object):
|
|||||||
callback(val)
|
callback(val)
|
||||||
|
|
||||||
|
|
||||||
def on_button(self, value):
|
async def on_button(self, value):
|
||||||
"""Function called on button notification
|
"""Function called on button notification
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@@ -566,7 +567,7 @@ class Wand(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _on_battery(self, data):
|
async def _on_battery(self, data):
|
||||||
"""Private function for battery notification
|
"""Private function for battery notification
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@@ -577,12 +578,12 @@ class Wand(object):
|
|||||||
if self.debug:
|
if self.debug:
|
||||||
print("Battery: {}".format(val))
|
print("Battery: {}".format(val))
|
||||||
|
|
||||||
self.on_battery(val)
|
await self.on_battery(val)
|
||||||
for callback in self._battery_callbacks.values():
|
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
|
"""Function called on battery notification
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@@ -590,7 +591,6 @@ class Wand(object):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def handle_notification(self, sender, data):
|
def handle_notification(self, sender, data):
|
||||||
"""Handle notifications subscribed to
|
"""Handle notifications subscribed to
|
||||||
|
|
||||||
@@ -598,11 +598,19 @@ class Wand(object):
|
|||||||
cHandle {int} -- Handle of notification
|
cHandle {int} -- Handle of notification
|
||||||
data {bytes} -- Data from device
|
data {bytes} -- Data from device
|
||||||
"""
|
"""
|
||||||
|
import asyncio
|
||||||
|
future = None
|
||||||
|
print("Handle notification")
|
||||||
if sender == SENSOR.QUATERNIONS_CHAR.value:
|
if sender == SENSOR.QUATERNIONS_CHAR.value:
|
||||||
self._on_position(data)
|
self._on_position(data)
|
||||||
elif sender == IO.USER_BUTTON_CHAR.value:
|
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:
|
elif sender == SENSOR.TEMP_CHAR.value:
|
||||||
self._on_temperature(data)
|
self._on_temperature(data)
|
||||||
elif sender == IO.BATTERY_CHAR.value:
|
elif sender == IO.BATTERY_CHAR.value:
|
||||||
self._on_battery(data)
|
self._on_battery(data)
|
||||||
|
|
||||||
|
if future != None:
|
||||||
|
print("future result is - ", future.result())
|
||||||
|
|||||||
+6
-9
@@ -17,24 +17,21 @@ class MyWand(Wand):
|
|||||||
# # Vibrate the wand and set its color to red
|
# # Vibrate the wand and set its color to red
|
||||||
# self.set_led(self.colors.pop())
|
# self.set_led(self.colors.pop())
|
||||||
# # Subscribe to notifications
|
# # Subscribe to notifications
|
||||||
# self.subscribe_button()
|
self.subscribe_button()
|
||||||
|
|
||||||
# Button callback, automatically called after connecting to wand
|
# Button callback, automatically called after connecting to wand
|
||||||
def on_button(self, pressed):
|
async def on_button(self, pressed):
|
||||||
if pressed:
|
if pressed:
|
||||||
# Unsubscribe from the position callback
|
# Unsubscribe from the position callback
|
||||||
# NOTE You could pass `continue_notifications=True`
|
await self.set_led(self.colors.pop())
|
||||||
# to continue using the wand's `on_position` function
|
|
||||||
|
|
||||||
self.set_led(self.colors.pop())
|
|
||||||
# Disconnect if we run out of colors
|
# Disconnect if we run out of colors
|
||||||
if len(self.colors) == 0:
|
if len(self.colors) == 0:
|
||||||
self.disconnect()
|
await self.disconnect()
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
# Create a new wand scanner
|
# Create a new wand scanner
|
||||||
shop = Shop(wand_class=MyWand, debug=True)
|
shop = Shop(asyncio.get_running_loop(), wand_class=MyWand, debug=True)
|
||||||
wands = []
|
wands = []
|
||||||
try:
|
try:
|
||||||
# While we don't have any wands
|
# While we don't have any wands
|
||||||
@@ -45,7 +42,7 @@ async def main():
|
|||||||
# For each wand (Only tested with one)
|
# For each wand (Only tested with one)
|
||||||
for wand in wands:
|
for wand in wands:
|
||||||
# Vibrate the wand and set its color to red
|
# Vibrate the wand and set its color to red
|
||||||
wand.vibrate(PATTERN.BURST)
|
await wand.vibrate(PATTERN.BURST)
|
||||||
|
|
||||||
# Callback for position
|
# Callback for position
|
||||||
# def onPos(x, y, pitch, roll):
|
# def onPos(x, y, pitch, roll):
|
||||||
|
|||||||
Reference in New Issue
Block a user