diff --git a/extras/ProcessingRawHID/HID_stuff.pde b/extras/ProcessingRawHID/HID_stuff.pde new file mode 100644 index 00000000..2482e16d --- /dev/null +++ b/extras/ProcessingRawHID/HID_stuff.pde @@ -0,0 +1,109 @@ +import com.codeminders.hidapi.HIDDeviceInfo; +import com.codeminders.hidapi.HIDManager; +import java.util.*; + +HIDManager hid_mgr = null; + +boolean thisIsWindows = false; //windows require a '0' report ID + +public int init_HID() { + + String OS = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH); + if (OS.indexOf("win") >= 0) { + thisIsWindows = true; + } + + try { + ClassPathLibraryLoader.loadNativeHIDLibrary(); + hid_mgr = HIDManager.getInstance(); + } + catch (Exception ex) { + ex.printStackTrace(); + return 0; + } + return 1; +} + +public int print_HID_devices() { + if (hid_mgr==null) return 0; + try { + for (HIDDeviceInfo info : hid_mgr.listDevices ()) { + //println(info.getProduct_string()+" "+info.getVendor_id()+" "+info.getProduct_id()); + println(info); + } + } + catch (Exception ex) { + ex.printStackTrace(); + return 0; + } + return 1; +} + +public HIDDeviceInfo[] filter_HID_ID_usage(int vid, int pid, int usage_page, int usage) { + if (hid_mgr==null) return null; + List dev_list = new ArrayList(); + try { + for (HIDDeviceInfo info : hid_mgr.listDevices ()) { + if (info.getVendor_id()==vid && info.getProduct_id()==pid && info.getUsage_page()==usage_page && info.getUsage()==usage) { + try { + //println(info); + dev_list.add(info); + } + catch (Exception ex) { + } + } + } + } + catch (Exception ex) { + ex.printStackTrace(); + return null; + } + + HIDDeviceInfo[] dev_list_arr = new HIDDeviceInfo[ dev_list.size() ]; + dev_list.toArray( dev_list_arr ); + if (dev_list.size() > 0) println("Found " + dev_list.size() + " devices"); + return dev_list_arr; +} + +public void HID_write_to_device (byte data[], HIDDevice device) { + try { + if (device!=null) { + if (thisIsWindows) { + byte dataWithReportID[] = new byte[data.length+1]; + dataWithReportID[0] = 0; + System.arraycopy(data, 0, dataWithReportID, 1, data.length); + device.write(dataWithReportID); + } else { + device.write(data); + } + } + } + catch (Exception ex) { + ex.printStackTrace(); + target_device=null; + } +} +public byte[] HID_read_from_device (HIDDevice device, int timeout) { + byte buf[]=new byte[256]; + try { + if (device!=null) { + int returnedBytes = device.readTimeout(buf, timeout); + byte retBuf[]; + retBuf=Arrays.copyOf(buf, returnedBytes); + return retBuf; + } + } + catch (Exception ex) { + ex.printStackTrace(); + } + return null; +} + +void delay_ms(int ms) { + try { + Thread.sleep(ms); + } + catch(Exception e) { + } +} + diff --git a/extras/ProcessingRawHID/ProcessingRawHID.pde b/extras/ProcessingRawHID/ProcessingRawHID.pde new file mode 100644 index 00000000..5283c057 --- /dev/null +++ b/extras/ProcessingRawHID/ProcessingRawHID.pde @@ -0,0 +1,65 @@ + /* +Copyright (c) 2016 Deqing Sun + +This is a simple Processing sketch that talks to Arduino with RawHID firmware. + +"javahidapi" from "Codeminders" is used for this sketch to do lower level communication. +javahidapi is available on https://code.google.com/p/javahidapi/ +*/ +HIDDevice target_device=null; + +void init_device() { + //print_HID_devices(); + //This funtion uses Arduino Micro VID/PID pair, change it if you use other Arduino + HIDDeviceInfo[] dev_list=filter_HID_ID_usage(0x2341, 0x8037, 65472, 3072); //VID PID usage_page usage + if (dev_list!=null && dev_list.length>0) { + //println(dev_list); + try { + target_device=dev_list[0].open(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } else { + //println("DEV not found"); + } +} + +void setup() { + init_HID(); + //print_HID_devices(); + + init_device(); + frameRate(30); +} + +void draw() { + background(0); + if (target_device!=null) { + byte buf[]=HID_read_from_device (target_device, 10); + if (buf!=null) { + if (buf.length>0) { + println(buf); + } + }else{ + target_device=null; + } + } else { + init_device(); //this part handles hot plugging + } +} + +void keyPressed() { + if (key == ' ') { + if (target_device!=null) { + byte buf[]=new byte[64]; + buf[0]=(byte)1; + buf[1]=(byte)2; + buf[2]=(byte)3; + buf[3]=(byte)4; + buf[62]=(byte)63; + buf[63]=(byte)64; + HID_write_to_device(buf, target_device); + } + } +} \ No newline at end of file diff --git a/extras/ProcessingRawHID/code/hidapi-1.1.jar b/extras/ProcessingRawHID/code/hidapi-1.1.jar new file mode 100644 index 00000000..d8ceb77b Binary files /dev/null and b/extras/ProcessingRawHID/code/hidapi-1.1.jar differ