forked from usb4java/usb4java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissileLauncher.java
More file actions
298 lines (262 loc) · 9.08 KB
/
MissileLauncher.java
File metadata and controls
298 lines (262 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (C) 2014 Klaus Reimer <k@ailis.de>
* See LICENSE.txt for licensing information.
*/
package org.usb4java.examples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import org.usb4java.Device;
import org.usb4java.DeviceDescriptor;
import org.usb4java.DeviceHandle;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
import org.usb4java.LibUsbException;
/**
* Controls a USB missile launcher (Only compatible with Vendor/Product
* 1130:0202).
*
* @author Klaus Reimer <k@ailis.de>
*/
public class MissileLauncher
{
/** The vendor ID of the missile launcher. */
private static final short VENDOR_ID = 0x1130;
/** The product ID of the missile launcher. */
private static final short PRODUCT_ID = 0x0202;
/** The USB communication timeout. */
private static final int TIMEOUT = 1000;
/** First init packet to send to the missile launcher. */
private static final byte[] INIT_A = new byte[] { 85, 83, 66, 67, 0, 0, 4,
0 };
/** Second init packet to send to the missile launcher. */
private static final byte[] INIT_B = new byte[] { 85, 83, 66, 67, 0, 64, 2,
0 };
/** Command to rotate the launcher up. */
private static final int CMD_UP = 0x01;
/** Command to rotate the launcher down. */
private static final int CMD_DOWN = 0x02;
/** Command to rotate the launcher to the left. */
private static final int CMD_LEFT = 0x04;
/** Command to rotate the launcher to the right. */
private static final int CMD_RIGHT = 0x08;
/** Command to fire a missile. */
private static final int CMD_FIRE = 0x10;
/**
* Searches for the missile launcher device and returns it. If there are
* multiple missile launchers attached then this simple demo only returns
* the first one.
*
* @return The missile launcher USB device or null if not found.
*/
public static Device findMissileLauncher()
{
// Read the USB device list
DeviceList list = new DeviceList();
int result = LibUsb.getDeviceList(null, list);
if (result < 0)
{
throw new RuntimeException(
"Unable to get device list. Result=" + result);
}
try
{
// Iterate over all devices and scan for the missile launcher
for (Device device: list)
{
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, descriptor);
if (result < 0)
{
throw new RuntimeException(
"Unable to read device descriptor. Result=" + result);
}
if (descriptor.idVendor() == VENDOR_ID
&& descriptor.idProduct() == PRODUCT_ID) return device;
}
}
finally
{
// Ensure the allocated device list is freed
LibUsb.freeDeviceList(list, true);
}
// No missile launcher found
return null;
}
/**
* Sends a message to the missile launcher.
*
* @param handle
* The USB device handle.
* @param message
* The message to send.
*/
public static void sendMessage(DeviceHandle handle, byte[] message)
{
ByteBuffer buffer = ByteBuffer.allocateDirect(message.length);
buffer.put(message);
buffer.rewind();
int transfered = LibUsb.controlTransfer(handle,
(byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
(byte) 0x09, (short) 2, (short) 1, buffer, TIMEOUT);
if (transfered < 0)
throw new LibUsbException("Control transfer failed", transfered);
if (transfered != message.length)
throw new RuntimeException("Not all data was sent to device");
}
/**
* Sends a command to the missile launcher.
*
* @param handle
* The USB device handle.
* @param command
* The command to send.
*/
public static void sendCommand(DeviceHandle handle, int command)
{
byte[] message = new byte[64];
message[1] = (byte) ((command & CMD_LEFT) > 0 ? 1 : 0);
message[2] = (byte) ((command & CMD_RIGHT) > 0 ? 1 : 0);
message[3] = (byte) ((command & CMD_UP) > 0 ? 1 : 0);
message[4] = (byte) ((command & CMD_DOWN) > 0 ? 1 : 0);
message[5] = (byte) ((command & CMD_FIRE) > 0 ? 1 : 0);
message[6] = 8;
message[7] = 8;
sendMessage(handle, INIT_A);
sendMessage(handle, INIT_B);
sendMessage(handle, message);
}
/**
* Read a key from stdin and returns it.
*
* @return The read key.
*/
public static char readKey()
{
try
{
String line =
new BufferedReader(new InputStreamReader(System.in)).readLine();
if (line.length() > 0) return line.charAt(0);
return 0;
}
catch (IOException e)
{
throw new RuntimeException("Unable to read key", e);
}
}
/**
* Main method.
*
* @param args
* Command-line arguments (Ignored)
*/
public static void main(String[] args)
{
// Initialize the libusb context
int result = LibUsb.init(null);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to initialize libusb", result);
}
// Search for the missile launcher USB device and stop when not found
Device device = findMissileLauncher();
if (device == null)
{
System.err.println("Missile launcher not found.");
System.exit(1);
}
// Open the device
DeviceHandle handle = new DeviceHandle();
result = LibUsb.open(device, handle);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to open USB device", result);
}
try
{
// Check if kernel driver is attached to the interface
int attached = LibUsb.kernelDriverActive(handle, 1);
if (attached < 0)
{
throw new LibUsbException(
"Unable to check kernel driver active", result);
}
// Detach kernel driver from interface 0 and 1. This can fail if
// kernel is not attached to the device or operating system
// doesn't support this operation. These cases are ignored here.
result = LibUsb.detachKernelDriver(handle, 1);
if (result != LibUsb.SUCCESS &&
result != LibUsb.ERROR_NOT_SUPPORTED &&
result != LibUsb.ERROR_NOT_FOUND)
{
throw new LibUsbException("Unable to detach kernel driver",
result);
}
// Claim interface
result = LibUsb.claimInterface(handle, 1);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to claim interface", result);
}
// Read commands and execute them
System.out.println("WADX = Move, S = Stop, F = Fire, Q = Exit");
boolean exit = false;
while (!exit)
{
System.out.print("> ");
char key = readKey();
switch (key)
{
case 'w':
sendCommand(handle, CMD_UP);
break;
case 'x':
sendCommand(handle, CMD_DOWN);
break;
case 'a':
sendCommand(handle, CMD_LEFT);
break;
case 'd':
sendCommand(handle, CMD_RIGHT);
break;
case 'f':
sendCommand(handle, CMD_FIRE);
break;
case 's':
sendCommand(handle, 0);
break;
case 'q':
exit = true;
break;
default:
}
}
// Release the interface
result = LibUsb.releaseInterface(handle, 1);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to release interface",
result);
}
// Re-attach kernel driver if needed
if (attached == 1)
{
LibUsb.attachKernelDriver(handle, 1);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException(
"Unable to re-attach kernel driver", result);
}
}
System.out.println("Exiting");
}
finally
{
LibUsb.close(handle);
}
// Deinitialize the libusb context
LibUsb.exit(null);
}
}