Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple Processing RawHID example #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions extras/ProcessingRawHID/HID_stuff.pde
Original file line number Diff line number Diff line change
@@ -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<HIDDeviceInfo> dev_list = new ArrayList<HIDDeviceInfo>();
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) {
}
}

65 changes: 65 additions & 0 deletions extras/ProcessingRawHID/ProcessingRawHID.pde
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Binary file added extras/ProcessingRawHID/code/hidapi-1.1.jar
Binary file not shown.