Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fetch.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. import usb.core
  3. import usb.util
  4. import time
  5. CUSTOM_RQ_ECHO = 0 # send back wValue and wIndex, for testing comms reliability
  6. CUSTOM_RQ_RESET = 1 # reset to bootloader
  7. CUSTOM_RQ_GET = 2 # get ldr value
  8. max_comm_retries = 5;
  9. def is_target_device(dev):
  10. if dev.manufacturer == "xythobuz.de" and dev.product == "AutoBrightness":
  11. return True
  12. return False
  13. # find our device
  14. dev = usb.core.find(idVendor=0x16c0, idProduct=0x05dc, custom_match=is_target_device)
  15. # was it found?
  16. if dev is None:
  17. raise ValueError('Device not found')
  18. # configure the device
  19. dev.set_configuration()
  20. def read_val(req, w, a=0, b=0):
  21. for attempts in range(0, max_comm_retries):
  22. try:
  23. r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, req, a, b, w)
  24. return int.from_bytes(r, "little")
  25. except usb.core.USBError as e:
  26. if attempts >= (max_comm_retries - 1):
  27. raise e
  28. # check for proper connectivity
  29. if read_val(CUSTOM_RQ_ECHO, 4, 42, 23) != (42 | (23 << 16)):
  30. raise ValueError("invalid echo response")
  31. # repeatedly print value
  32. while True:
  33. v = read_val(CUSTOM_RQ_GET, 2)
  34. print(time.time(), ";", v, flush=True)
  35. time.sleep(0.25)