Reputation: 23
I would like to ask if there is a way to detect the state of a switch, connected through a usb. The switch has 2 states, on and off. Possibly with Python, on Windows.
Or, am I able to implement a script that will consider the switch to be a keyboard extension.
Thank you in advance!
EDIT
#import usb.core
#import usb.util
import usb
# find our device
#dev = usb.core.find(find_all=True)
busses = usb.busses()
# was it found?
#if dev is None:
# raise ValueError('Device not found')
for bus in busses:
devices = bus.devices
for dev in devices:
try:
_name = usb.util.get_string(dev.dev, 19, 1)
except:
continue
#dev.set_configuration()
#cfg = dev.get_active_configuration()
#interface_number = cfg[(0,0)].bInterfaceNumber
#5alternate_settting = usb.control.get_interface(interface_number)
print "Device name:",_name
print "Device:", dev.filename
print " idVendor:",hex(dev.idVendor)
print " idProduct:",hex(dev.idProduct)
for config in dev.configurations:
print " Configuration:", config.value
print " Total length:", config.totalLength
print " selfPowered:", config.selfPowered
print " remoteWakeup:", config.remoteWakeup
print " maxPower:", config.maxPower
print
Upvotes: 1
Views: 4122
Reputation: 23
Ok, I have the solution now, I will post it, but I have a question...when I run the code, sometimes it says that the device is busy and it will generate an error and when it works... it will wait for an interrupt, however if you move the mouse, it will stay static on the screen but it will produce an interrupt. The same can be said for clicking a button, it will produce an interrupt, but the mouse will stay static and to use it again, you need to take it out from the usb and put it in again.
import usb.core
import usb.util
#import usb
# find our device
dev = usb.core.find(find_all=True)
#the second device it finds is my mouse
device = dev[2]
#print device
#physical device call: 5
#usb HID call: 3
_name = usb.util.get_string(device, 19, 1)
print _name
#we take the first configuration of the device
device.set_configuration()
print "Config set..."
#we access the configuration we've found
cfg = device.get_active_configuration()
#we access the intherface with number 0 and alternate setting with number 0
interface_number = cfg[(0,0)].bInterfaceNumber
alternate_setting = usb.control.get_interface(device,interface_number)
#we find the alterng settings for interface_number and altering_setting
intf = usb.util.find_descriptor(cfg, bInterfaceNumber = interface_number,\
bAlternateSetting = alternate_setting)
#Finds the first IN endpoint
ep = usb.util.find_descriptor(
intf,
# match the first IN endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN
)
#inorder for you to detect a state from the device, it has to be(for mouse, moved,
#clicked)
#otherwise it generates error
#make use of the error, if the mouse isn't pushed, do nothing and wait, if pushed...
#print the state
#and exit from the loop
print "Waiting for signal..."
#device.detach_kernel_driver(0)
#click of the scroll button has array('B', [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#0, 0]) signal
while True:
try:
print ep.read(16)
print "Received!"
break
except:
continue
#assert ep is not 0
#_name=device.ctrl_transfer(bmRequestType=33, bRequest=11, wValue=0x0300)
#print _name
So I guess it removes the drivers of the mouse first, then it talks with the device, then interrupt is produced by me clicking a button, then...how can I say, use your drivers again and end program...because it is inconvinient to reinput the usb mouse every time.
Upvotes: 0
Reputation: 2629
Have you taken a look at PyUSB? See http://pyusb.sourceforge.net/docs/1.0/tutorial.html for a tutorial on the usage of PyUSB. The source of that library would help you if you want to implement something closer to the hardware as well.
http://libhid.alioth.debian.org/ looks like another decent library written in C with Python bindings.
In response to your attempted code, it looks like you're using the legacy PyUSB interface. If you print(dev), you'll either find it shows up like <usb.legacy.Device object at 0x1dac210>
or you'll find that you're using an older version of the library (<usb.Device object at 0x13e6810>
). Make sure you have 1.0 and make sure that you're using the newer methods to access the devices. It will be something like <usb.core.Device object at 0x1e0c3d0>
For example, usb.core.find()
will give you a device back that does indeed have a set_configuration()
. Try working through the tutorial again.
Upvotes: 1