Skip to content

Commit 83eb5d9

Browse files
committed
Add class for automatically enumerating USB devices and finding sensor interfaces.
Currently finds only Vernier devices.
1 parent 64a1834 commit 83eb5d9

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

sensor/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<version>2.1.7</version>
2323
<scope>compile</scope>
2424
</dependency>
25+
<dependency>
26+
<groupId>org.usb4java</groupId>
27+
<artifactId>usb4java</artifactId>
28+
<version>1.2.0</version>
29+
</dependency>
2530
</dependencies>
2631

2732
<build>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package org.concord.sensor.device.impl;
2+
3+
import java.util.ArrayList;
4+
import java.util.logging.Logger;
5+
6+
import org.usb4java.Context;
7+
import org.usb4java.Device;
8+
import org.usb4java.DeviceDescriptor;
9+
import org.usb4java.DeviceList;
10+
import org.usb4java.LibUsb;
11+
import org.usb4java.LibUsbException;
12+
13+
public class DeviceFinder {
14+
private static final Logger logger = Logger.getLogger(DeviceFinder.class.getName());
15+
16+
public static int[] getAttachedDeviceTypes() throws LibUsbException {
17+
ArrayList<Integer> foundDevices = new ArrayList<Integer>();
18+
19+
// Create the libusb context
20+
final Context context = new Context();
21+
22+
// Initialize the libusb context
23+
int result = LibUsb.init(context);
24+
if (result < 0) {
25+
throw new LibUsbException("Unable to initialize libusb", result);
26+
}
27+
28+
// Read the USB device list
29+
DeviceList list = new DeviceList();
30+
result = LibUsb.getDeviceList(context, list);
31+
if (result < 0) {
32+
throw new LibUsbException("Unable to get device list", result);
33+
}
34+
35+
try {
36+
logger.fine("There are " + list.getSize() + " devices.");
37+
// Iterate over all devices and dump them
38+
for (Device device : list) {
39+
// Read the device descriptor
40+
DeviceDescriptor descriptor = new DeviceDescriptor();
41+
result = LibUsb.getDeviceDescriptor(device, descriptor);
42+
if (result < 0) {
43+
throw new LibUsbException("Unable to read device descriptor", result);
44+
}
45+
switch (descriptor.idVendor()) {
46+
case 0x08F7: // Vernier
47+
switch (descriptor.idProduct()) {
48+
case 0x0001:
49+
logger.fine("Found a Vernier LabPro!");
50+
foundDevices.add(DeviceID.VERNIER_LAB_PRO);
51+
break;
52+
case 0x0002: // GoTemp
53+
logger.fine("Found a Vernier GoTemp!");
54+
foundDevices.add(DeviceID.VERNIER_GO_LINK_JNA);
55+
break;
56+
case 0x0003: // GoLink
57+
logger.fine("Found a Vernier GoLink!");
58+
foundDevices.add(DeviceID.VERNIER_GO_LINK_JNA);
59+
break;
60+
case 0x0004: // GoMotion
61+
logger.fine("Found a Vernier GoMotion!");
62+
foundDevices.add(DeviceID.VERNIER_GO_LINK_JNA);
63+
break;
64+
case 0x0005: // LabQuest
65+
logger.fine("Found a Vernier LabQuest!");
66+
foundDevices.add(DeviceID.VERNIER_LAB_QUEST);
67+
break;
68+
case 0x0008: // LabQuest Mini
69+
logger.fine("Found a Vernier LabQuest Mini!");
70+
foundDevices.add(DeviceID.VERNIER_LAB_QUEST);
71+
break;
72+
}
73+
break;
74+
case 0x0000: // Pasco
75+
// Right now, we don't differentiate between Pasco USB
76+
// devices, so no need to check product ids
77+
logger.fine("Found a Pasco!");
78+
foundDevices.add(DeviceID.PASCO_USB);
79+
break;
80+
default:
81+
break;
82+
}
83+
}
84+
} finally {
85+
// Ensure the allocated device list is freed
86+
LibUsb.freeDeviceList(list, false);
87+
}
88+
89+
// Deinitialize the libusb context
90+
LibUsb.exit(context);
91+
92+
if (foundDevices.size() > 0) {
93+
int[] foundIDs = new int[foundDevices.size()];
94+
for (int i = 0; i < foundIDs.length; i++) {
95+
foundIDs[i] = foundDevices.get(i);
96+
}
97+
return foundIDs;
98+
} else {
99+
return new int[] {};
100+
}
101+
}
102+
103+
public static String getDeviceName(int deviceID) {
104+
switch (deviceID) {
105+
case DeviceID.PSEUDO_DEVICE:
106+
case DeviceID.PSEUDO_DEVICE_VARIABLE_TIME:
107+
return "Pseudo Sensor Device";
108+
case DeviceID.VERNIER_LAB_PRO:
109+
return "Vernier LabPro";
110+
case DeviceID.VERNIER_LAB_QUEST:
111+
return "Vernier LabQuest";
112+
case DeviceID.VERNIER_GO_LINK:
113+
case DeviceID.VERNIER_GO_LINK_JNA:
114+
case DeviceID.VERNIER_GO_LINK_NATIVE:
115+
return "Vernier GoLink!";
116+
case DeviceID.TI_CONNECT:
117+
return "TI Connect"; // ???
118+
case DeviceID.FOURIER:
119+
return "Fourier EcoLog"; // ???
120+
case DeviceID.DATA_HARVEST_USB:
121+
case DeviceID.DATA_HARVEST_CF:
122+
case DeviceID.DATA_HARVEST_ADVANCED:
123+
case DeviceID.DATA_HARVEST_QADVANCED:
124+
return "Data Harvest"; // ???
125+
case DeviceID.IMAGIWORKS_SERIAL:
126+
case DeviceID.IMAGIWORKS_SD:
127+
return "Imagiworks"; // ???
128+
case DeviceID.PASCO_SERIAL:
129+
case DeviceID.PASCO_AIRLINK:
130+
case DeviceID.PASCO_USB:
131+
return "Pasco";
132+
case DeviceID.CCPROBE_VERSION_0:
133+
case DeviceID.CCPROBE_VERSION_1:
134+
case DeviceID.CCPROBE_VERSION_2:
135+
return "CC Probe";
136+
case DeviceID.COACH:
137+
return "Coach"; // ???
138+
default:
139+
return "Unknown Device!";
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)