Geen omschrijving
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 1020B

1234567891011121314151617181920212223242526272829303132333435363738
  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. def is_target_device(dev):
  9. if dev.manufacturer == "xythobuz.de" and dev.product == "AutoBrightness":
  10. return True
  11. return False
  12. # find our device
  13. dev = usb.core.find(idVendor=0x16c0, idProduct=0x05dc, custom_match=is_target_device)
  14. # was it found?
  15. if dev is None:
  16. raise ValueError('Device not found')
  17. # configure the device
  18. dev.set_configuration()
  19. def read_u32(req, a=0, b=0):
  20. r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, req, a, b, 4)
  21. return int.from_bytes(r, "little")
  22. # check for proper connectivity
  23. if read_u32(CUSTOM_RQ_ECHO, 42, 23) != (42 | (23 << 16)):
  24. raise ValueError("invalid echo response")
  25. # repeatedly print value
  26. while True:
  27. r = read_u32(CUSTOM_RQ_GET)
  28. print(time.time(), ";", r, flush=True)
  29. time.sleep(0.25)