added dlls : wiiuse.dll(always necessary).dll,libwiiuseJ.dll(the one created with the WiiUseJC project)
Libwiiusej must be replaced by the new one you could make modifying WiiUseJC project. Added new stuufs for Architecture. It's getting easier and easier to use. Now Wiimotes are manipulated through the object Wiimote. You get Wiimotes instances callic the Static metho getWiimotes in the class WiiuUseApiManager. improved disconnection handling. git-svn-id: http://wiiusej.googlecode.com/svn/trunk@17 ae48ae66-6a45-0410-b38e-211266189506
This commit is contained in:
@@ -7,6 +7,8 @@ import wiiusej.Point2DInteger;
|
||||
import wiiusej.WiiMoteEvent;
|
||||
import wiiusej.WiiUseApiListener;
|
||||
import wiiusej.WiiUseApiManager;
|
||||
import wiiusej.Wiimote;
|
||||
import wiiusej.WiimoteListener;
|
||||
|
||||
/**
|
||||
* This class used to test this API.
|
||||
@@ -14,7 +16,7 @@ import wiiusej.WiiUseApiManager;
|
||||
* @author gduche
|
||||
*
|
||||
*/
|
||||
public class Tests implements WiiUseApiListener {
|
||||
public class Tests implements WiimoteListener {
|
||||
|
||||
Robot robot;
|
||||
|
||||
@@ -23,10 +25,14 @@ public class Tests implements WiiUseApiListener {
|
||||
private static int MOVE_MOUSE = 3;
|
||||
private static int ORIENT_THRESH_CONT = 4;
|
||||
private static int TEST_LEDS = 5;
|
||||
|
||||
private Wiimote wiimote;
|
||||
|
||||
int dump = ORIENT_THRESH_CONT;
|
||||
int dump = DUMP;
|
||||
|
||||
public Tests() {
|
||||
public Tests(Wiimote wim) {
|
||||
wiimote = wim;
|
||||
wiimote.addWiiMoteEventListeners(this);
|
||||
try {
|
||||
robot = new Robot();
|
||||
} catch (AWTException e) {
|
||||
@@ -37,11 +43,6 @@ public class Tests implements WiiUseApiListener {
|
||||
@Override
|
||||
public void wiimoteEvent(WiiMoteEvent e) {
|
||||
|
||||
/* leave if nothing is connected */
|
||||
if (WiiUseApiManager.getInstance().getNbConnectedWiimotes() == 0) {
|
||||
WiiUseApiManager.getInstance().shutdown();
|
||||
}
|
||||
|
||||
if (dump == DISPLAY_EACH_VALUE) {
|
||||
if (e.isConnected()) {
|
||||
// System.out.println("*********** WIIMOTE ID : "+
|
||||
@@ -169,37 +170,37 @@ public class Tests implements WiiUseApiListener {
|
||||
|
||||
/* get status */
|
||||
if (e.isButtonMinusJustPressed() && e.isButtonPlusJustPressed()) {
|
||||
WiiUseApiManager.getInstance().getStatus(1);
|
||||
wiimote.getStatus();
|
||||
}
|
||||
|
||||
/* Activate rumble */
|
||||
if (e.isButtonOneJustPressed()) {
|
||||
System.out.println("Rumble Activated");
|
||||
WiiUseApiManager.getInstance().activateRumble(1);
|
||||
wiimote.activateRumble();
|
||||
}
|
||||
if (e.isButtonTwoJustPressed()) {
|
||||
System.out.println("Rumble Deactivated");
|
||||
WiiUseApiManager.getInstance().deactivateRumble(1);
|
||||
wiimote.deactivateRumble();
|
||||
}
|
||||
|
||||
/* Activate IR Tracking */
|
||||
if (e.isButtonAJustPressed()) {
|
||||
System.out.println("IR Activated");
|
||||
WiiUseApiManager.getInstance().activateIRTRacking(1);
|
||||
wiimote.activateIRTRacking();
|
||||
}
|
||||
if (e.isButtonBJustPressed()) {
|
||||
System.out.println("IR Deactivated");
|
||||
WiiUseApiManager.getInstance().deactivateIRTRacking(1);
|
||||
wiimote.deactivateIRTRacking();
|
||||
}
|
||||
|
||||
/* Activate Motion sensing */
|
||||
if (e.isButtonPlusJustPressed()) {
|
||||
System.out.println("Motion sensing Activated");
|
||||
WiiUseApiManager.getInstance().activateMotionSensing(1);
|
||||
wiimote.activateMotionSensing();
|
||||
}
|
||||
if (e.isButtonMinusJustPressed()) {
|
||||
System.out.println("Motion sensing Deactivated");
|
||||
WiiUseApiManager.getInstance().deactivateMotionSensing(1);
|
||||
wiimote.deactivateMotionSensing();
|
||||
}
|
||||
|
||||
/* display status */
|
||||
@@ -239,46 +240,43 @@ public class Tests implements WiiUseApiListener {
|
||||
/* leave test */
|
||||
if (e.isButtonHomeJustPressed()) {
|
||||
System.out.println("LEAVING TEST");
|
||||
WiiUseApiManager.getInstance().closeConnection(1);
|
||||
wiimote.disconnect();
|
||||
}
|
||||
} else {
|
||||
System.out.println(" WIIMOTE ID : " + e.getWiimoteId()
|
||||
+ " DISCONNECTED !!!!!");
|
||||
WiiUseApiManager.getInstance().closeConnection(1);
|
||||
wiimote.disconnect();
|
||||
}
|
||||
} else if (dump == DUMP) {
|
||||
System.out.println(e);
|
||||
/* Activate all */
|
||||
if (e.isButtonAJustPressed()) {
|
||||
System.out.println("IR Activated");
|
||||
WiiUseApiManager.getInstance().activateIRTRacking(1);
|
||||
WiiUseApiManager.getInstance().activateMotionSensing(1);
|
||||
WiiUseApiManager.getInstance().activateRumble(1);
|
||||
wiimote.activateIRTRacking();
|
||||
wiimote.activateMotionSensing();
|
||||
wiimote.activateRumble();
|
||||
}
|
||||
if (e.isButtonBJustPressed()) {
|
||||
System.out.println("IR Deactivated");
|
||||
WiiUseApiManager.getInstance().deactivateIRTRacking(1);
|
||||
WiiUseApiManager.getInstance().deactivateMotionSensing(1);
|
||||
WiiUseApiManager.getInstance().deactivateRumble(1);
|
||||
wiimote.deactivateIRTRacking();
|
||||
wiimote.deactivateMotionSensing();
|
||||
wiimote.deactivateRumble();
|
||||
}
|
||||
|
||||
/* leave test */
|
||||
if (e.isButtonHomeJustPressed()) {
|
||||
System.out.println("LEAVING TEST");
|
||||
WiiUseApiManager.getInstance().closeConnection(1);
|
||||
if (WiiUseApiManager.getInstance().getNbConnectedWiimotes() == 0) {
|
||||
WiiUseApiManager.getInstance().shutdown();
|
||||
}
|
||||
wiimote.disconnect();
|
||||
}
|
||||
} else if (dump == MOVE_MOUSE) {
|
||||
/* Activate IR Tracking */
|
||||
if (e.isButtonOneJustPressed()) {
|
||||
System.out.println("IR Activated");
|
||||
WiiUseApiManager.getInstance().activateIRTRacking(1);
|
||||
wiimote.activateIRTRacking();
|
||||
}
|
||||
if (e.isButtonTwoJustPressed()) {
|
||||
System.out.println("IR Deactivated");
|
||||
WiiUseApiManager.getInstance().deactivateIRTRacking(1);
|
||||
wiimote.deactivateIRTRacking();
|
||||
}
|
||||
|
||||
/* button A */
|
||||
@@ -311,65 +309,60 @@ public class Tests implements WiiUseApiListener {
|
||||
/* leave test */
|
||||
if (e.isButtonHomeJustPressed()) {
|
||||
System.out.println("LEAVING TEST");
|
||||
WiiUseApiManager.getInstance().closeConnection(1);
|
||||
wiimote.disconnect();
|
||||
}
|
||||
} else if (dump == ORIENT_THRESH_CONT) {
|
||||
WiiUseApiManager.getInstance().activateMotionSensing(1);
|
||||
wiimote.activateMotionSensing();
|
||||
if (e.isButtonOneJustPressed()) {
|
||||
System.out.println("Continous activated");
|
||||
WiiUseApiManager.getInstance().activateContinuous(1);
|
||||
wiimote.activateContinuous();
|
||||
}
|
||||
if (e.isButtonTwoJustPressed()) {
|
||||
System.out.println("Continous deactivated");
|
||||
WiiUseApiManager.getInstance().deactivateContinuous(1);
|
||||
wiimote.deactivateContinuous();
|
||||
}
|
||||
if (e.isButtonAJustPressed()) {
|
||||
System.out.println("Smoothing activated");
|
||||
WiiUseApiManager.getInstance().activateSmoothing(1);
|
||||
wiimote.activateSmoothing();
|
||||
}
|
||||
if (e.isButtonBJustPressed()) {
|
||||
System.out.println("Smoothing deactivated");
|
||||
WiiUseApiManager.getInstance().deactivateSmoothing(1);
|
||||
wiimote.deactivateSmoothing();
|
||||
}
|
||||
if (e.isButtonPlusJustPressed()) {
|
||||
System.out.println("Threshold orientation 10 degrees");
|
||||
WiiUseApiManager.getInstance().setOrientationThreshold(1, 10);
|
||||
wiimote.setOrientationThreshold(10);
|
||||
}
|
||||
if (e.isButtonMinusJustPressed()) {
|
||||
System.out.println("Threshold orientation 0.5 degrees");
|
||||
WiiUseApiManager.getInstance().setOrientationThreshold(1,
|
||||
(float) 0.5);
|
||||
wiimote.setOrientationThreshold((float) 0.5);
|
||||
}
|
||||
System.out.println(e);
|
||||
|
||||
/* leave test */
|
||||
if (e.isButtonHomeJustPressed()) {
|
||||
System.out.println("LEAVING TEST");
|
||||
WiiUseApiManager.getInstance().closeConnection(1);
|
||||
wiimote.disconnect();
|
||||
}
|
||||
} else if (dump == TEST_LEDS) {
|
||||
WiiUseApiManager.getInstance().activateMotionSensing(1);
|
||||
wiimote.activateMotionSensing();
|
||||
if (e.isButtonUpJustPressed()) {
|
||||
WiiUseApiManager.getInstance().setLeds(1, true, false, false,
|
||||
false);
|
||||
wiimote.setLeds(true, false, false, false);
|
||||
}
|
||||
if (e.isButtonDownJustPressed()) {
|
||||
WiiUseApiManager.getInstance().setLeds(1, false, true, false,
|
||||
false);
|
||||
wiimote.setLeds(false, true, false, false);
|
||||
}
|
||||
if (e.isButtonLeftJustPressed()) {
|
||||
WiiUseApiManager.getInstance().setLeds(1, false, false, true,
|
||||
false);
|
||||
wiimote.setLeds(false, false, true, false);
|
||||
}
|
||||
if (e.isButtonRightJustPressed()) {
|
||||
WiiUseApiManager.getInstance().setLeds(1, false, false, false,
|
||||
true);
|
||||
wiimote.setLeds(false, false, false, true);
|
||||
}
|
||||
|
||||
/* leave test */
|
||||
if (e.isButtonHomeJustPressed()) {
|
||||
System.out.println("LEAVING TEST");
|
||||
WiiUseApiManager.getInstance().closeConnection(1);
|
||||
wiimote.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -378,12 +371,10 @@ public class Tests implements WiiUseApiListener {
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Tests tests = new Tests();
|
||||
WiiUseApiManager manager = WiiUseApiManager.getInstance();
|
||||
manager.addWiiUseApiListener(tests);
|
||||
manager.loadLibrary();
|
||||
manager.connectWiimotes();
|
||||
manager.start();
|
||||
Wiimote[] wiimotes = WiiUseApiManager.getWiimotes();
|
||||
System.out.println(wiimotes[0]);
|
||||
Tests tests = new Tests(wiimotes[0]);
|
||||
|
||||
// java.util.Timer timer = new java.util.Timer();
|
||||
// timer.scheduleAtFixedRate(new LedsTask(), 0, 100);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package wiiusej;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@@ -17,19 +18,53 @@ public class WiiUseApiManager extends Thread {
|
||||
|
||||
private final EventListenerList listeners = new EventListenerList();
|
||||
|
||||
private Wiimote[] wiimotes;
|
||||
|
||||
private WiiUseApi wiiuse = WiiUseApi.getInstance();
|
||||
|
||||
private boolean loaded = false;
|
||||
|
||||
private int connected = 0;
|
||||
private int connected = -1;
|
||||
|
||||
private AtomicBoolean running = new AtomicBoolean(false);
|
||||
|
||||
private ConcurrentLinkedQueue<WiiUseApiRequest> requests = new ConcurrentLinkedQueue<WiiUseApiRequest>();
|
||||
private ConcurrentLinkedQueue<WiiUseApiRequest> requests = new ConcurrentLinkedQueue<WiiUseApiRequest>();
|
||||
|
||||
public static WiiUseApiManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get wiimotes.
|
||||
* Load library if necessary.
|
||||
* Connect to wiimotes if necessary.
|
||||
* Start polling if necessary.
|
||||
* Return an array with the connected wiimotes.
|
||||
* @return an array with connected wiimotes or NULL.
|
||||
*/
|
||||
public static Wiimote[] getWiimotes() {
|
||||
WiiUseApiManager manager = getInstance();
|
||||
if (!manager.loaded){
|
||||
manager.loadLibrary();
|
||||
}
|
||||
if (manager.connected<0){
|
||||
int nbWiimotes = manager.connectWiimotes();
|
||||
manager.wiimotes = new Wiimote[nbWiimotes];
|
||||
for (int i=0; i<nbWiimotes; i++){
|
||||
Wiimote wim = new Wiimote(i,manager);
|
||||
manager.wiimotes[i] = wim;
|
||||
manager.addWiiUseApiListener(wim);
|
||||
}
|
||||
}
|
||||
|
||||
if (manager.connected == 0){
|
||||
return new Wiimote[0];
|
||||
}
|
||||
|
||||
if (!manager.isAlive()) manager.start();
|
||||
|
||||
return manager.wiimotes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the wiimote library.
|
||||
@@ -53,15 +88,15 @@ public class WiiUseApiManager extends Thread {
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect wiimote and get the nu ber of wiimotes connected. Supposed to be
|
||||
* Connect wiimote and get the number of wiimotes connected. Supposed to be
|
||||
* used once.
|
||||
*
|
||||
* @return 0 if nothing connected or the number of wiimotes connected.
|
||||
*/
|
||||
public int connectWiimotes() {
|
||||
if (loaded) {
|
||||
if (connected < 0 && loaded) {
|
||||
connected = wiiuse.doConnections();
|
||||
System.out.println(connected + " wiimote(s) connected !!!");
|
||||
// System.out.println(connected + " wiimote(s) connected !!!");
|
||||
return connected;
|
||||
} else {// library not loaded, no wiimotes connected
|
||||
return 0;
|
||||
@@ -93,11 +128,12 @@ public class WiiUseApiManager extends Thread {
|
||||
*/
|
||||
public void shutdown() {
|
||||
running.set(false);
|
||||
loaded = false;
|
||||
wiiuse.shutdownApi();
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate the rumble for the wiimotes with the given id.
|
||||
* Activate the rumble for the wiimote with the given id.
|
||||
*
|
||||
* @param id
|
||||
* id of the wiimote.
|
||||
@@ -108,7 +144,7 @@ public class WiiUseApiManager extends Thread {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate the rumble for the wiimotes with the given id.
|
||||
* Deactivate the rumble for the wiimote with the given id.
|
||||
*
|
||||
* @param id
|
||||
* id of the wiimote.
|
||||
@@ -119,7 +155,7 @@ public class WiiUseApiManager extends Thread {
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate IR Tracking for the wiimotes with the given id.
|
||||
* Activate IR Tracking for the wiimote with the given id.
|
||||
*
|
||||
* @param id
|
||||
* id of the wiimote.
|
||||
@@ -130,7 +166,7 @@ public class WiiUseApiManager extends Thread {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate IR Tracking for the wiimotes with the given id.
|
||||
* Deactivate IR Tracking for the wiimote with the given id.
|
||||
*
|
||||
* @param id
|
||||
* id of the wiimote.
|
||||
@@ -141,7 +177,7 @@ public class WiiUseApiManager extends Thread {
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate IR Tracking of the wiimotes with the given id.
|
||||
* Activate motion sensing for the wiimote with the given id.
|
||||
*
|
||||
* @param id
|
||||
* id of the wiimote.
|
||||
@@ -152,7 +188,7 @@ public class WiiUseApiManager extends Thread {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate IR Tracking of the wiimotes with the given id.
|
||||
* Deactivate motion sensing for the wiimoter with the given id.
|
||||
*
|
||||
* @param id
|
||||
* id of the wiimote.
|
||||
@@ -258,12 +294,14 @@ public class WiiUseApiManager extends Thread {
|
||||
WiiMoteEvent evt = new WiiMoteEvent();
|
||||
|
||||
// Start polling and tell the observers when there Wiimote events
|
||||
while (running.get()) {
|
||||
while (running.get()) {
|
||||
WiiUseApiRequest req = requests.poll();
|
||||
if (req != null) {// there is a request for the wiiuse api
|
||||
int id = req.getId();
|
||||
if (req.getRequestType() == WiiUseApiRequest.WIIUSE_CLOSE_CONNECTION_REQUEST) {
|
||||
/* Close connections requests */
|
||||
/* Close connections requests */
|
||||
removeWiiUseApiListener(wiimotes[id]);
|
||||
wiimotes[id]=null;
|
||||
wiiuse.closeConnection(id);
|
||||
connected--;
|
||||
} else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_STATUS_REQUEST) {
|
||||
@@ -316,30 +354,43 @@ public class WiiUseApiManager extends Thread {
|
||||
/* Polling */
|
||||
wiiuse.specialPoll(evt);
|
||||
|
||||
if (evt.getWiimoteId() != -1) {// there is an event notify
|
||||
// observers
|
||||
notifyWiiUseApiListener(evt);
|
||||
if (evt.getWiimoteId() != -1) {//event filled
|
||||
if (!evt.isConnected()) {// check if it was a
|
||||
// disconnection
|
||||
connected--;
|
||||
removeWiiUseApiListener(wiimotes[evt.getWiimoteId()]);
|
||||
wiimotes[evt.getWiimoteId()]=null;
|
||||
System.out.println("Wiimote " + evt.getWiimoteId()
|
||||
+ " disconnected !");
|
||||
} else {//there is an event notify observers
|
||||
// not a disconnection notify listeners
|
||||
notifyWiiUseApiListener(evt);
|
||||
// create a new event to be filled
|
||||
evt = new WiiMoteEvent();
|
||||
}
|
||||
}
|
||||
if (connected == 0) {// stop this thread if there is
|
||||
//no more wiimotes connected.
|
||||
System.out.println("No more wiimotes connected !!!");
|
||||
shutdown();
|
||||
}
|
||||
if (evt.getWiimoteId() != -1) {// create a new event when the last event created was filled
|
||||
if (!evt.isConnected()){//check if it was a disconnection
|
||||
connected --;
|
||||
if (connected == 0){
|
||||
System.out.println("No more wiimotes connected !!!");
|
||||
}
|
||||
}
|
||||
evt = new WiiMoteEvent();
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.println("No polling possible !!!");
|
||||
if (!loaded) {
|
||||
System.out.println("Library not Loaded !");
|
||||
}
|
||||
if (connected <= 0) {
|
||||
System.out.println("No wiimotes connected !");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add WiiUseApiListener to the listeners list.
|
||||
* @param listener a WiiUseApiListener
|
||||
*
|
||||
* @param listener
|
||||
* a WiiUseApiListener
|
||||
*/
|
||||
public void addWiiUseApiListener(WiiUseApiListener listener) {
|
||||
listeners.add(WiiUseApiListener.class, listener);
|
||||
@@ -347,7 +398,9 @@ public class WiiUseApiManager extends Thread {
|
||||
|
||||
/**
|
||||
* Remove WiiUseApiListener from the listeners list.
|
||||
* @param listener a WiiUseApiListener
|
||||
*
|
||||
* @param listener
|
||||
* a WiiUseApiListener
|
||||
*/
|
||||
public void removeWiiUseApiListener(WiiUseApiListener listener) {
|
||||
listeners.remove(WiiUseApiListener.class, listener);
|
||||
@@ -355,6 +408,7 @@ public class WiiUseApiManager extends Thread {
|
||||
|
||||
/**
|
||||
* Get the list of WiiUseApiListeners.
|
||||
*
|
||||
* @return the list of WiiUseApiListeners.
|
||||
*/
|
||||
public WiiUseApiListener[] getWiiUseApiListeners() {
|
||||
@@ -363,9 +417,11 @@ public class WiiUseApiManager extends Thread {
|
||||
|
||||
/**
|
||||
* Notify WiiUseApiListeners that an event occured.
|
||||
* @param evt WiimoteEvent occured
|
||||
*
|
||||
* @param evt
|
||||
* WiimoteEvent occured
|
||||
*/
|
||||
public void notifyWiiUseApiListener(WiiMoteEvent evt) {
|
||||
private void notifyWiiUseApiListener(WiiMoteEvent evt) {
|
||||
for (WiiUseApiListener listener : getWiiUseApiListeners()) {
|
||||
listener.wiimoteEvent(evt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user