I created two small classes that do all the work. The first is the WiiNXT_Proxy class that runs on a PC or notebook equipped with Bluetooth and wiiusej.
WiiNXT_Proxy.java:
- Code: Select all
import lejos.pc.comm.*;
import java.io.*;
import wiiusej.*;
import wiiusej.wiiusejevents.physicalevents.*;
import wiiusej.wiiusejevents.utils.*;
import wiiusej.wiiusejevents.wiiuseapievents.*;
public class WiiNXT_Proxy implements WiimoteListener {
private static int intPitch = 0;
private static int intRoll = 0;
private void ConnectWii() {
Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true, WiiUseApiManager.WIIUSE_STACK_MS);
Wiimote wiimoteTilt = wiimotes[0];
wiimoteTilt.activateMotionSensing();
wiimoteTilt.setTimeout((short)100, (short)100);
wiimoteTilt.addWiiMoteEventListeners(this);
System.out.println("Connected to Wii");
}
private void ConnectNXT(String[] args) {
if (args.length != 2) {
System.out.println("Usage: WiiNXT_Proxy name address");
System.exit(1);
}
NXTComm nxtComm = null;
try {
nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
} catch (NXTCommException e) {
System.out.println("Failed to load Bluetooth driver");
System.exit(1);
}
NXTInfo[] nxtInfo = new NXTInfo[1];
nxtInfo[0] = new NXTInfo(args[0],args[1]);
System.out.println("Connecting to " + nxtInfo[0].btResourceString);
boolean opened = false;
try {
opened = nxtComm.open(nxtInfo[0]);
} catch (NXTCommException e) {
System.out.println("Failed to open " + nxtInfo[0].name);
System.exit(1);
}
if (!opened) {
System.out.println("Failed to open " + nxtInfo[0].name);
System.exit(1);
}
System.out.println("Connected to " + nxtInfo[0].btResourceString);
InputStream is = nxtComm.getInputStream();
OutputStream os = nxtComm.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
while (true) {
try {
if ( dis.readInt() == 10 ) {
dos.writeInt(intPitch);
dos.flush();
dos.writeInt(intRoll);
dos.flush();
//System.out.println("Sent Data: Pitch= " + intPitch + ", Roll=" + intRoll);
}
}catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
public void onMotionSensingEvent(MotionSensingEvent arg0) {
intPitch = (int)(arg0.getOrientation().getPitch());
intRoll = (int)(arg0.getOrientation().getRoll());
}
public void onButtonsEvent(WiimoteButtonsEvent arg0) {}
public void onDisconnectionEvent(DisconnectionEvent arg0) {}
public void onIrEvent(IREvent arg0) {}
public void onExpansionEvent(ExpansionEvent arg0) {}
public void onStatusEvent(StatusEvent arg0) {}
public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {}
public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {}
public static void main(String[] args) {
WiiNXT_Proxy p = new WiiNXT_Proxy();
p.ConnectWii();
p.ConnectNXT(args);
}
}
The second class is called Wii.java and runs on the NXT and is used by a tilt behavior class.
Wii.java:
- Code: Select all
import lejos.nxt.comm.*;
import java.io.*;
public final class Wii {
private static Wii _instance = null;
private static final int GETPITCHROLL = 10;
private static BTConnection btc = null;
private static DataInputStream dis = null;
private static DataOutputStream dos = null;
private Wii() {
try {
LCDBuffer.getHandle().DisplayMessage("Wii: Waiting...");
btc = Bluetooth.waitForConnection();
dis = btc.openDataInputStream();
dos = btc.openDataOutputStream();
LCDBuffer.getHandle().DisplayMessage("Wii: Connected");
}catch(Exception e) {
LCDBuffer.getHandle().DisplayMessage("Wii: ConErr");
}
}
public synchronized static Wii getInstance() {
if (_instance == null ) {
_instance = new Wii();
}
return _instance;
}
public synchronized int[] getPitchRoll() {
int[] Readings = {0,0};
try {
dos.writeInt(GETPITCHROLL);
dos.flush();
Readings[0] = dis.readInt();
Readings[1] = dis.readInt();
}catch(Exception e) {
LCDBuffer.getHandle().DisplayMessage("Wii: ComErr");
}
return Readings;
}
public void shutdown() throws Exception {
dis.close();
dos.close();
Thread.sleep(100);
btc.close();
}
}
That's really all there is to it. All the hard work was done by the folks that created LeJOS and wiiusej.