Skip to content

Instantly share code, notes, and snippets.

@madhephaestus
Last active June 17, 2020 03:35
Show Gist options
  • Save madhephaestus/2f03c9588113d58e22d23a30e1eaaecc to your computer and use it in GitHub Desktop.
Save madhephaestus/2f03c9588113d58e22d23a30e1eaaecc to your computer and use it in GitHub Desktop.
HIDTest.groovy
@Grab(group='org.hid4java', module='hid4java', version='0.5.0')
import org.hid4java.HidDevice;
import org.hid4java.HidManager;
import org.hid4java.HidServices;
public int getPid() {
//return 0xffff804d; // arduino zero RawHID
//return 0xffff8033 // pybadge
return 0x486; // teensy
}
public int getVid() {
//return 0x2341;// Arduino Zero RawHID
return 0x16c0; // teensy
//return 0x239a; // pybadge
}
def hidServices = HidManager.getHidServices();
def hidDevice = null;
int foundInterface = Integer.MAX_VALUE;
for (HidDevice h : hidServices.getAttachedHidDevices()) {
if (h.isVidPidSerial(getVid(), getPid(), null)) {
System.out.println("Found! interface " + h.getInterfaceNumber() + " " + h);
if (h.getInterfaceNumber() < foundInterface && !h.isOpen() ) {
hidDevice = h;
foundInterface = h.getInterfaceNumber();
break;
}
}else{
println " Wrong device: "+h.getProduct() +" VID "+String.format("0x%04X",h.getVendorId())+" PID "+String.format("0x%04X",h.getProductId())
}
}
if(hidDevice!=null){
boolean opened =hidDevice.open();
byte[] message=[37]
int length=1
hidDevice.write(message, length, (byte) 0);
if(hidDevice.read(message, 1000)){// read with 1 second timeout
println "Byte returned "+message
}else{
println "ERROR no response"
}
hidDevice.close();
hidServices.shutdown();
}else
println "FAIL no device found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment