Guest User

Untitled

a guest
Oct 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. public static void sendCommand(String command){
  2. final TransferCallback callback = (Transfer transfer) -> {
  3. LibUsb.freeTransfer(transfer);
  4. };
  5.  
  6. write(handle, command, callback);
  7. }
  8.  
  9. public static void sendMultipleCommands(String COMMAND_ONE, String COMMAND_TWO, String COMMAND_THREE, String COMMAND_FOUR, String COMMAND_FIVE, String COMMAND_SIX){
  10. // This callback is called after all commands have been sent
  11. // The asynchronous transfer chain ends here.
  12. final TransferCallback seven = (Transfer transfer) -> {
  13. System.out.println("Cut complete.");
  14. LibUsb.freeTransfer(transfer);
  15. };
  16. // This callback is called after command 6 is sent
  17. final TransferCallback six = (Transfer transfer) -> {
  18. System.out.println("Sending command: 6");
  19. write(handle, COMMAND_SIX, seven);
  20. LibUsb.freeTransfer(transfer);
  21. };
  22. // This callback is called after command 5 is sent
  23. final TransferCallback five = (Transfer transfer) -> {
  24. System.out.println("Sending command: 5");
  25. write(handle, COMMAND_FIVE, six);
  26. LibUsb.freeTransfer(transfer);
  27. };
  28. // This callback is called after command 4 is sent
  29. final TransferCallback four = (Transfer transfer) -> {
  30. System.out.println("Sending command: 4");
  31. write(handle, COMMAND_FOUR, five);
  32. LibUsb.freeTransfer(transfer);
  33. };
  34. // This callback is called after command 3 is sent
  35. final TransferCallback three = (Transfer transfer) -> {
  36. System.out.println("Sending command: 3");
  37. write(handle, COMMAND_THREE, four);
  38. LibUsb.freeTransfer(transfer);
  39. };
  40. // This callback is called after command 2 is sent
  41. final TransferCallback two = (Transfer transfer) -> {
  42. System.out.println("Sending command: 2");
  43. write(handle, COMMAND_TWO, three);
  44. LibUsb.freeTransfer(transfer);
  45. };
  46.  
  47. // Sending commands asynchronously.
  48. // The rest of the communication is handled by the callbacks defined above.
  49. System.out.println("nSending command: 1");
  50. write(handle, COMMAND_ONE, two);
  51. }
  52.  
  53. /**
  54. * Asynchronously writes some data to the device.
  55. *
  56. * @param handle
  57. * The device handle.
  58. * @param data
  59. * The data to send to the device.
  60. * @param callback
  61. * The callback to execute when data has been transfered.
  62. */
  63. public static void write(DeviceHandle handle, String data, TransferCallback callback){
  64. ByteBuffer buffer = BufferUtils.allocateByteBuffer(data.getBytes().length);
  65. buffer.put(data.getBytes());
  66. Transfer transfer = LibUsb.allocTransfer();
  67. LibUsb.fillBulkTransfer(transfer, handle, OUT_ENDPOINT, buffer, callback, null, TIMEOUT);
  68. // System.out.println("Sending " + data.length + " bytes to devicen");
  69. result = LibUsb.submitTransfer(transfer);
  70. if (result != LibUsb.SUCCESS){
  71. throw new LibUsbException("Unable to submit transfern", result);
  72. }
  73. }
Add Comment
Please, Sign In to add comment