No Description
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.

lux.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. def usb_init():
  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. return dev
  22. def read_val(dev, req, w, a=0, b=0):
  23. for attempts in range(0, max_comm_retries):
  24. try:
  25. r = dev.ctrl_transfer(usb.util.CTRL_TYPE_VENDOR | usb.util.CTRL_IN, req, a, b, w)
  26. return int.from_bytes(r, "little")
  27. except usb.core.USBError as e:
  28. if attempts >= (max_comm_retries - 1):
  29. raise e
  30. def check_connection(dev):
  31. # check for proper connectivity
  32. if read_val(dev, CUSTOM_RQ_ECHO, 4, 42, 23) != (42 | (23 << 16)):
  33. raise ValueError("invalid echo response")
  34. def read_brightness(dev):
  35. return read_val(dev, CUSTOM_RQ_GET, 2)
  36. if __name__ == "__main__":
  37. dev = usb_init()
  38. check_connection(dev)
  39. # repeatedly print value
  40. while True:
  41. v = read_brightness(dev)
  42. print(time.time(), ";", v, flush=True)
  43. time.sleep(0.25)