-
1Step 1
Code to find device
import usb.core dev = usb.core.find(idVendor=0x03eb, idProduct=0x2402) print dev
Control Endpoint Read (Slide 94)
import usb.core dev = usb.core.find(idVendor=0x03eb, idProduct=0x2402) dev.set_configuration() data = dev.ctrl_transfer(0b10100001, 0x01, 3<<8, 0, 4) print data
Sending Output Report
import usb.core dev = usb.core.find(idVendor=0x03eb, idProduct=0x2402) print dev dev.set_configuration() data = [ord('1'), ord('1'), 0, 0, 0, 0, 0, 0] dev.write(0x02, data)
Getting Input Report (Press "user" button for things to happen)
import usb.core dev = usb.core.find(idVendor=0x03eb, idProduct=0x2402) print dev dev.set_configuration() for i in range(0, 10): while True: try: test = dev.read(0x81, 8, timeout=50) break except usb.core.USBError, e: if str(e).find("timeout") >= 0: pass else: raise IOError("USB Error: %s"%str(e)) print test
Full-on GUI
#Public domain - simple USB GUI Example by Colin O'Flynn from PySide.QtCore import * from PySide.QtGui import * import usb.core import sys class USBForm(QDialog): def __init__(self, parent=None): super(USBForm, self).__init__(parent) self.setWindowTitle("SuperCon 2015 Demo") layout = QVBoxLayout() self.setLayout(layout) self.pbConnect = QPushButton("Connect") self.pbConnect.clicked.connect(self.con) self.isConnected = False self.pbLED = QPushButton("LED Blinking") self.pbLED.setCheckable(True) self.pbLED.clicked.connect(self.changeLED) self.pbLED.setEnabled(False) layout.addWidget(self.pbConnect) layout.addWidget(self.pbLED) self.swStatus = QLineEdit() self.swStatus.setReadOnly(True) layout.addWidget(self.swStatus) self.butTimer = QTimer(self) self.butTimer.timeout.connect(self.pollButton) def con(self): if self.isConnected == False: #Do USB Connect Here self.dev = usb.core.find(idVendor=0x03eb, idProduct=0x2402) self.dev.set_configuration() #Sync changeLED self.changeLED() self.isConnected = True self.pbConnect.setText("Disconnect") self.pbLED.setEnabled(True) self.butTimer.start(100) else: self.isConnected = False self.pbConnect.setText("Connect") self.pbLED.setEnabled(False) self.butTimer.stop() def changeLED(self): if self.pbLED.isChecked(): #Send command to make LED on self.dev.write(0x02, [ord('1'), ord('1'), 0, 0, 0, 0, 0, 0]) self.pbLED.setText("LED On") else: #Send command to make LED blink self.dev.write(0x02, [ord('0'), ord('1'), 0, 0, 0, 0, 0, 0]) self.pbLED.setText("LED Blinking") def pollButton(self): try: data = self.dev.read(0x81, 8, timeout=50) if data[0]: self.swStatus.setText("Button Pressed") else: self.swStatus.setText("Button Released") except usb.core.USBError, e: if str(e).find("timeout") >= 0: pass else: raise IOError("USB Error: %s"%str(e)) if __name__ == "__main__": app = QApplication(sys.argv) form = USBForm() form.show() sys.exit(app.exec_())
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.