Merge pull request #1 from hornetfighter515/feature/bluetooth-discovery-improvements

feature/bluetooth-discovery-improvements
This commit is contained in:
BackwardsMonday 2022-10-12 15:13:03 -06:00 committed by GitHub
commit 0150f7a939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 14 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

View File

@ -1,12 +1,13 @@
import asyncio import asyncio
from time import sleep from time import sleep
from bleak import BleakScanner, BleakClient from bleak import BleakScanner, BleakClient, BleakError
import pickle
class Droid(): class Droid():
def __init__(self, profile): def __init__(self, profile):
print("Initializing") print("Initializing")
self.disabledLeds = 0x00 self.disabledLeds = 0x00
self.profile = profile self.profile = profile
async def connect(self): async def connect(self):
timeout=0.0 timeout=0.0
print("Connecting") print("Connecting")
@ -68,6 +69,7 @@ class Droid():
def move (self ): def move (self ):
pass pass
async def play_sound(self, sound_id=None, bank_id=None, cycle=False, volume=None): async def play_sound(self, sound_id=None, bank_id=None, cycle=False, volume=None):
if volume: if volume:
self.set_volume(volume) self.set_volume(volume)
@ -100,13 +102,38 @@ def findDroid(candidate, data):
else: else:
return False return False
async def main(): async def discoverDroid(retry=False):
myDroid = await BleakScanner.find_device_by_filter(findDroid) myDroid = None
print (myDroid)
arms = Droid(myDroid) while retry and myDroid is None:
await arms.connect()
sleep (3)
try: try:
myDroid = await BleakScanner.find_device_by_filter(findDroid)
if myDroid is None:
if not retry:
print("Droid discovery timed out.")
return
else:
print("Droid discovery timed out. Retrying...")
continue
except BleakError as err:
print("Droid discovery failed. Retrying...")
continue
print (f"Astromech successfully discovered: [ {myDroid} ]")
d = Droid(myDroid)
return d
async def main():
d = await discoverDroid(retry=True)
try:
await d.connect()
sleep (3)
# await arms.run_routine("05") # await arms.run_routine("05")
# sleep (5) # sleep (5)
# await arms.set_soundbank("05") # await arms.set_soundbank("05")
@ -117,16 +144,22 @@ async def main():
# sleep(5) # sleep(5)
# await arms.play_sound("00", "00") # await arms.play_sound("00", "00")
# sleep(8) # sleep(8)
await arms.led_disable_sound("01") await d.led_disable_sound("01")
await arms.play_sound("00", "00") await d.play_sound("00", "00")
sleep(10) sleep(10)
await arms.led_on("1f") await d.led_on("1f")
sleep(10) sleep(10)
await arms.led_off("1f") await d.led_off("1f")
await arms.play_sound("00", "00") await d.play_sound("00", "00")
sleep(10) sleep(10)
except OSError as err:
print(f"Discovery failed due to operating system: {err}")
except BleakError as err:
print(f"Discovery failed due to Bleak: {err}")
finally: finally:
await arms.disconnect() await d.disconnect()
if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())