Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

fetch.py 1.4KB

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