Refactoring Code to mesh with the architecture set by https://github.com/GammaGames/kano_wand

Overall - moving from bluez to bleak will give us universal usage, and the rest of the ML side of this project can go on without a hitch.
This commit is contained in:
Lucas Oskorep
2020-02-11 16:29:03 -06:00
parent e91c870eca
commit 5df2dd6b47
7 changed files with 1587 additions and 64 deletions
+65
View File
@@ -0,0 +1,65 @@
from shop import Shop
from wand import Wand
from constants import *
import asyncio
import sys
# Custom wand class extending the default wand
class MyWand(Wand):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.colors = ["#a333c8", "2185d0", "0x21ba45", "#fbbd08", "#f2711c", "#db2828"]
self.position_id = None
# Do some functions after connecting
def post_connect(self):
print("Connected to {}".format(self.name))
# # Vibrate the wand and set its color to red
# self.set_led(self.colors.pop())
# # Subscribe to notifications
# self.subscribe_button()
# Button callback, automatically called after connecting to wand
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())
# Disconnect if we run out of colors
if len(self.colors) == 0:
self.disconnect()
async def main():
# Create a new wand scanner
shop = Shop(wand_class=MyWand, debug=True)
wands = []
try:
# While we don't have any wands
while len(wands) == 0:
# Scan for wands and automatically connect
print("Scanning...")
wands = await shop.scan(connect=True)
# For each wand (Only tested with one)
for wand in wands:
# Vibrate the wand and set its color to red
wand.vibrate(PATTERN.BURST)
# Callback for position
# def onPos(x, y, pitch, roll):
# pitch = "Pitch: {}".format(pitch).ljust(16)
# roll = "Roll: {}".format(roll).ljust(16)
# print("{}{}(x, y): ({}, {})".format(pitch, roll, x, y))
# Add the event callback to the wand
# wand.position_id = wand.on("position", onPos)
# Detect keyboard interrupt disconnect
except KeyboardInterrupt as e:
for wand in wands:
wand.disconnect()
if __name__ == "__main__":
asyncio.run(main())