This commit is contained in:
BackwardsMonday 2022-10-12 15:17:49 -06:00
commit 91c94a771f
2 changed files with 46 additions and 14 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

View File

@ -1,7 +1,7 @@
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")
@ -114,13 +114,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")
@ -131,16 +156,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()
asyncio.run(main())
if __name__ == "__main__":
asyncio.run(main())