Skip to content

Instantly share code, notes, and snippets.

@keionbis
Forked from madhephaestus/hidApiTest.groovy
Last active May 15, 2018 16:32
Show Gist options
  • Save keionbis/660853e74e87b61988d557406d95610a to your computer and use it in GitHub Desktop.
Save keionbis/660853e74e87b61988d557406d95610a to your computer and use it in GitHub Desktop.
Adding new file from BowlerStudio
@Grab(group='org.hid4java', module='hid4java', version='0.5.0')
import org.hid4java.*
import org.hid4java.event.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
class PacketProcessor{
HidDevice myHidDevice;
ByteOrder be =ByteOrder.LITTLE_ENDIAN;
int packetSize = 64
int numFloats =(packetSize/4)-1
byte[] message = new byte[packetSize];
PacketProcessor(HidDevice hidDevice){
myHidDevice=hidDevice;
}
float[] command(int idOfCommand, float []values){
ByteBuffer.wrap(message).order(be).putInt(0,idOfCommand).array();
for(int i=0;i<numFloats && i< values.length;i++){
int baseIndex = (4*i)+4;
ByteBuffer.wrap(message).order(be).putFloat(baseIndex,values[i]).array();
}
float[] returnValues = new float[ numFloats]
//println "Writing packet"
int val = myHidDevice.write(message, packetSize, (byte) 0);
if(val>0){
//println "Reading packet"
int read = myHidDevice.read(message, 1000);
if(read>0){
//println "Parsing packet"
for(int i=0;i<numFloats;i++){
int baseIndex = (4*i)+4;
returnValues[i] = ByteBuffer.wrap(message).order(be).getFloat(baseIndex)
}
}else{
println "Read failed"
}
}else{
println "Writing failed"
}
return returnValues
}
}
float []returnValues = 0;
HidServices hidServices = HidManager.getHidServices();
while(1){
// Provide a list of attached devices
for (HidDevice hidDevice : hidServices.getAttachedHidDevices()) {
if(hidDevice.isVidPidSerial(0x3742, 0x7, null)){
System.out.println("Found! "+hidDevice);
hidDevice=hidDevice;
hidDevice.open();
System.out.println("Opened! "+hidDevice);
//byte[] bytes;
//float f = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat();
//byte[] data = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(value).array();
// Send the Initialise message
PacketProcessor processor= new PacketProcessor(hidDevice)
// command index
long sum
int min =100000;
int max =0;
float []values=new float[9]
float sinWaveInc =10
float seconds = 0.01;
float range = 400
values[(0)+0] = (Math.sin(((float)1)/sinWaveInc*Math.PI*2)* range) -range
//values[(j*3)+1] = ((Math.cos(((float)i)/sinWaveInc*Math.PI*2)* range)/seconds)
values[(0)+1] =0
values[(0)+2] = 3
returnValues = processor.command(37,values)
System.out.println("sent data " + values +" returned "+ returnValues);
Thread.sleep((long)(1000.0*seconds))
hidDevice.close();
}
}
for (HidDevice hidDevice_2 : hidServices.getAttachedHidDevices()) {
if(hidDevice_2.isVidPidSerial(0x3742, 0x7, null)){
System.out.println("Found! "+hidDevice_2);
hidDevice_2.open();
System.out.println("Opened! "+hidDevice_2);
PacketProcessor processor= new PacketProcessor(hidDevice_2)
float seconds = 0.01;
float []Values = processor.command(37,returnValues)
System.out.println("sent data2 " + returnValues +" returned2 "+ Values);
Thread.sleep((long)(1000.0*seconds))
}
hidDevice_2.close();
}
}
// Clean shutdown
hidServices.shutdown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment