droidDepotPythonControl/droid.py

60 lines
2.1 KiB
Python
Raw Normal View History

2022-09-26 16:43:31 +00:00
import asyncio
from time import sleep
from bleak import BleakScanner, BleakClient
2022-09-27 03:23:17 +00:00
class Droid():
def __init__(self, profile):
print("Initializing")
self.profile = profile
2022-09-26 16:43:31 +00:00
async def connect(self):
2022-09-26 23:25:13 +00:00
timeout=0.0
print("Connecting")
2022-09-27 03:23:17 +00:00
self.droid = BleakClient(self.profile)
await self.droid.connect()
while not self.droid.is_connected and timeout < 10:
sleep (.1)
timeout += .1
print ("Connected!")
connectCode = bytearray.fromhex("222001")
await self.droid.write_gatt_char(0x000d, connectCode, False)
await self.droid.write_gatt_char(0x000d, connectCode, False)
print("Locked")
2022-09-27 03:56:10 +00:00
light_blink = bytearray.fromhex("2C000449020001ff01ff0aff00")
await self.droid.write_gatt_char(0x000d, light_blink)
2022-09-27 03:23:17 +00:00
soundSelection = bytearray.fromhex("27420f4444001800")
await self.droid.write_gatt_char(0x000d, soundSelection)
2022-09-27 03:56:10 +00:00
connect_sound = bytearray.fromhex("25000c421102")
await self.droid.write_gatt_char(0x000d, connect_sound)
2022-09-27 03:23:17 +00:00
async def disconnect(self):
print ("Disconnecting")
try:
soundBank = bytearray.fromhex("27420f4444001f08")
await self.droid.write_gatt_char(0x000d, soundBank)
soundSelection = bytearray.fromhex("27420f4444001808")
await self.droid.write_gatt_char(0x000d, soundSelection)
finally:
await self.droid.disconnect()
print("Disconnected")
2022-09-27 03:39:20 +00:00
async def run_routine(self, routineId):
full_id = bytearray.fromhex("25000c42{}02".format(routineId))
await self.droid.write_gatt_char(0x000d, full_id)
2022-09-26 23:25:13 +00:00
def findDroid(candidate, data):
if candidate.name == "DROID":
return True
else:
return False
2022-09-26 16:43:31 +00:00
2022-09-26 23:25:13 +00:00
async def main():
myDroid = await BleakScanner.find_device_by_filter(findDroid)
print (myDroid)
2022-09-27 03:23:17 +00:00
arms = Droid(myDroid)
2022-09-26 23:25:13 +00:00
await arms.connect()
2022-09-27 03:23:17 +00:00
sleep (3)
2022-09-27 03:39:20 +00:00
try:
await arms.run_routine("05")
2022-09-27 03:56:10 +00:00
sleep (5)
2022-09-27 03:39:20 +00:00
finally:
await arms.disconnect()
2022-09-26 23:25:13 +00:00
asyncio.run(main())