Cider/drivers/helpers/listen.py

47 lines
No EOL
1.1 KiB
Python

import hid
import time
import sys
import threading
# Vendor ID and Product ID for the iBridge device
TOUCHBAR_VENDOR_ID = 0x05ac
TOUCHBAR_PRODUCT_ID = 0x8600
exit_flag = False
def listen_to_touchbar(device):
global exit_flag
print("Listening for incoming data from the Touch Bar...")
while not exit_flag:
try:
data = device.read(64)
if data:
print(f"Received data: {data}")
except Exception as e:
print(f"Error reading data: {e}")
break
device = hid.device()
try:
device.open(TOUCHBAR_VENDOR_ID, TOUCHBAR_PRODUCT_ID)
print("Device opened successfully")
listener_thread = threading.Thread(target=listen_to_touchbar, args=(device,))
listener_thread.daemon = True
listener_thread.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
print("\nInterrupted by user. Exiting...")
exit_flag = True
listener_thread.join()
break
except OSError as e:
print(f"Failed to open device: {e}")
sys.exit(1)
finally:
device.close()
print("Device closed")