control.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. import usb.core as core
  3. import usb.util as util
  4. from usb.core import USBError
  5. from remotes import tv
  6. # Use usb-devices to find the Arduino
  7. dev = core.find(idVendor=0x2341, idProduct=0x0043)
  8. detached = False
  9. if dev is None:
  10. raise ValueError("Device Not Found!")
  11. #if dev.is_kernel_driver_active(0):
  12. # try:
  13. # dev.detach_kernel_driver(0)
  14. # detached = True
  15. # print("Kernel detached")
  16. # except USBError as e:
  17. # exit("Failed detaching kernel driver!")
  18. #else:
  19. # print("No Kernel Attached!")
  20. #try:
  21. # util.claim_interface(dev, 0)
  22. # print("Claimed Device!")
  23. #except:
  24. # endpoint = dev[0][(0,0)][0]
  25. # data = dev.read(endpoint.bEndpointAddress, 0x0040)
  26. # print(type(data))
  27. # print(data)
  28. #if detached:
  29. # dev.attach_kernel_driver(0)
  30. # print("Kernel attached")
  31. #exit()
  32. #print(type(dev))
  33. #print(dev)
  34. # Setup the device
  35. # https://github.com/pyusb/pyusb/issues/357
  36. #dev.set_configuration()
  37. # From https://stackoverflow.com/questions/29345325/raspberry-pyusb-gets-resource-busy
  38. dev.reset()
  39. # Get the config
  40. cfg = dev.get_active_configuration()
  41. #print(cfg)
  42. # Index the config for interfaces
  43. intf = cfg[(0, 0)]
  44. print(intf)
  45. # If you don't know the endpoint
  46. # Obatains the endpoint out
  47. ep = util.find_descriptor(
  48. intf,
  49. custom_match = \
  50. lambda e: \
  51. util.endpoint_direction(e.bEndpointAddress) == \
  52. util.ENDPOINT_IN)
  53. assert ep is not None
  54. # Now we timeout here. :(
  55. ep.write(b'0x57E3E817,32,1\n')