private void comm(Pointer<hid_device> device) { log.debug("starting comm loop"); Pointer<Byte> data = Pointer.allocateBytes(1024); HidapiLibrary.hid_set_nonblocking(device, 1); while (loop && connected) { int result = HidapiLibrary.hid_read(device, data, 1024); if (result > 0) { byte bytes[] = data.getBytes(result); messageQueue.addAll(parse(result, bytes)); } BpmMessage msg = writeQueue.poll(); if (msg != null) { encode(msg, writeBuffer); try { HidapiLibrary.hid_write(device, Pointer.pointerToBytes(writeBuffer), 9); } finally { writeBuffer.rewind(); } } } }
public void run() { while (loop) { Pointer<hid_device> device = connect(); if (device != null) { connected = true; try { comm(device); } catch (RuntimeException e) { log.error("Error communicating with device", e); } finally { HidapiLibrary.hid_close(device); } } } }
private Pointer<hid_device> connect() { log.debug("connecting"); Pointer<hid_device> device = null; while (device == null && loop == true) { device = HidapiLibrary.hid_open((short) 0x10b7, (short) 0x1234, null); if (device == null) { log.debug("no device found"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } else { log.debug("device found"); } } return device; }