Skip to content

Instantly share code, notes, and snippets.

@martijnvandijk
Forked from jonasas/pyusb_mouse_read.py
Created June 7, 2016 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijnvandijk/57ceffa6a76eb352c119d346753400c4 to your computer and use it in GitHub Desktop.
Save martijnvandijk/57ceffa6a76eb352c119d346753400c4 to your computer and use it in GitHub Desktop.
Python code for mouse reading under PyUSB. - ported to python 3
import array
import sys
import usb.core
import usb.util
VID = 0x03f0
PID = 0x134a
DATA_SIZE = 4
# printina modulio vidurius :for i in dir(usb.util): print i
# try to find Logiceh USB mouse
device = usb.core.find(idVendor = VID, idProduct = PID)
if device is None:
sys.exit("Could not find Logitech USB mouse.")
# make sure the hiddev kernel driver is not active
if device.is_kernel_driver_active(0):
try:
device.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit("Could not detatch kernel driver: %s" % str(e))
# set configuration
try:
device.reset()
device.set_configuration()
except usb.core.USBError as e:
sys.exit("Could not set configuration: %s" % str(e))
endpoint = device[0][(0,0)][0]
print (endpoint.wMaxPacketSize)
data = array.array('B',(0,)*4)
while data[0] != 3:
try:
data = device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
print (data)
except usb.core.USBError as e:
if e.args == ('Operation timed out',):
print ("timeoutas")
continue
print( "baigiau! :)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment