From 4e4adeb581e2a3153c02be440d2dae586426162c Mon Sep 17 00:00:00 2001 From: Tempest Date: Tue, 15 Jul 2025 20:06:42 +0700 Subject: [PATCH] Added test script for controlling Billboard over BLE --- src/controllerSoftware/run.py | 97 +++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/controllerSoftware/run.py diff --git a/src/controllerSoftware/run.py b/src/controllerSoftware/run.py new file mode 100644 index 0000000..d3e76f8 --- /dev/null +++ b/src/controllerSoftware/run.py @@ -0,0 +1,97 @@ +import asyncio +import time +from bleak import BleakScanner, BleakClient + +lampAmount = 25 +Center = [0] +Up = [1,2,8,9,10,11,12,13,23,24] +Down = [4,5,6,15,16,17,18,19,20,21] +Left = [6,3,8,9,19,20,21,22,23,12] +Right = [2,7,4,11,24,13,14,15,16,17] +Inner = [1,2,3,4,5,6,7,8] +Outer = [9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] + +colorArray = ["500000","005000","000050"] +colorArrayTest = ["050000","000500"] + +def sectionInitializer(): + result = [0] * lampAmount + return result + +def sectionSerializer(section): + result = sectionInitializer() + sectionResult = Center + section + for pos in sectionResult: + result[pos] = 1 + return result + +def posColorByte(pos, section, color): + intColor = int(color, 16) + result = intColor * section[pos] + print(result) + return result.to_bytes(3) + +async def setLampToColor(bleClient, bleChars, section, color): + sectionSerial = sectionSerializer(section) + for char in bleChars: + lampPos = bleChars.index(char) + print(f"Setting Lamp number {lampPos}") + value = posColorByte(lampPos, sectionSerial, color) + await bleClient.write_gatt_char(char.uuid, value) + time.sleep(10) + +async def connect_to_ble_device(device_name): + print(f"Scanning for device: {device_name}...") + devices = await BleakScanner.discover() + + target_device = None + for device in devices: + if device.name == device_name: + target_device = device + break + + if target_device: + print(f"Found device: {target_device.name} ({target_device.address})") + async with BleakClient(target_device.address) as client: + if client.is_connected: + print(f"Connected to {target_device.name}") + # Now you can interact with services and characteristics + # Example: Read a characteristic (replace with actual UUID) + # battery_level = await client.read_gatt_char("00002a19-0000-1000-8000-00805f9b34fb") + # print(f"Battery Level: {int.from_bytes(battery_level, 'little')}%") + else: + print(f"Failed to connect to {target_device.name}") + exit(1) + services = client.services + print("\n--- Services and Characteristics ---") + for service in services: + print(f"\nService: {service.uuid} (Handle: {service.handle}) - {service.description}") + for char in service.characteristics: + print(f" Characteristic: {char.uuid} (Handle: {char.handle})") + print(f" Description: {char.description}") + print(f" Properties: {char.properties}") + + if "read" in char.properties: + try: + await client.write_gatt_char(char.uuid, bytes.fromhex("000005")) + time.sleep(1) + value = await client.read_gatt_char(char.uuid) + print(f" Value: {value.hex()} (raw bytes)") + except Exception as e: + print(f" Could not read characteristic {char.uuid}: {e}") + else: + print(" (Read not supported)") + for color in colorArray: + await setLampToColor(client, service.characteristics, Up, color) + await setLampToColor(client, service.characteristics, Down, color) + await setLampToColor(client, service.characteristics, Left, color) + await setLampToColor(client, service.characteristics, Right, color) + await setLampToColor(client, service.characteristics, Inner, color) + await setLampToColor(client, service.characteristics, Outer, color) + + else: + print(f"Device '{device_name}' not found.") + +if __name__ == "__main__": + # Replace 'YourDeviceName' with the actual name of your BLE device + asyncio.run(connect_to_ble_device("Pupilometer LED Billboard")) \ No newline at end of file