wiiuseJ 0.12 work in progress. fixes on GUI. Adapted reconnection.

git-svn-id: http://wiiusej.googlecode.com/svn/trunk@153 ae48ae66-6a45-0410-b38e-211266189506
This commit is contained in:
guilhem.duche
2008-05-26 20:25:02 +00:00
parent f7454499f0
commit eb0b4dcd01
7 changed files with 149 additions and 78 deletions

View File

@@ -16,6 +16,7 @@
*/
package wiiusej;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.event.EventListenerList;
@@ -36,6 +37,8 @@ public class WiiUseApiManager extends Thread {
private final EventListenerList listeners = new EventListenerList();
private Semaphore semaphore = new Semaphore(0);
private Wiimote[] wiimotes;
private WiiUseApi wiiuse = WiiUseApi.getInstance();
@@ -51,7 +54,7 @@ public class WiiUseApiManager extends Thread {
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
@@ -64,10 +67,10 @@ public class WiiUseApiManager extends Thread {
*
* @return an array with connected wiimotes or NULL.
*/
public static Wiimote[] getWiimotes(int nb, boolean rumble){
public static Wiimote[] getWiimotes(int nb, boolean rumble) {
return getWiimotesPrivate(nb, rumble, false, WIIUSE_STACK_UNKNOWN);
}
/**
* Get wiimotes. Load library if necessary. Connect to wiimotes if
* necessary. Start polling if necessary. Return an array with the connected
@@ -84,7 +87,7 @@ public class WiiUseApiManager extends Thread {
*
* @return an array with connected wiimotes or NULL.
*/
public static Wiimote[] getWiimotes(int nb, boolean rumble, int stackType){
public static Wiimote[] getWiimotes(int nb, boolean rumble, int stackType) {
return getWiimotesPrivate(nb, rumble, true, stackType);
}
@@ -106,11 +109,12 @@ public class WiiUseApiManager extends Thread {
*
* @return an array with connected wiimotes or NULL.
*/
private synchronized static Wiimote[] getWiimotesPrivate(int nb, boolean rumble,
boolean forceStackType, int stackType) {
private synchronized static Wiimote[] getWiimotesPrivate(int nb,
boolean rumble, boolean forceStackType, int stackType) {
WiiUseApiManager manager = getInstance();
if (manager.connected <= 0 && !manager.running.get()) {
int nbWiimotes = manager.connectWiimotes(nb, rumble, forceStackType, stackType);
int nbWiimotes = manager.connectWiimotes(nb, rumble,
forceStackType, stackType);
manager.wiimotes = new Wiimote[nbWiimotes];
for (int i = 0; i < nbWiimotes; i++) {
Wiimote wim = new Wiimote(WiiUseApi.getInstance().getUnId(i),
@@ -154,6 +158,8 @@ public class WiiUseApiManager extends Thread {
if (!manager.isAlive())
manager.start();
manager.semaphore.release();
return manager.wiimotes;
}
@@ -173,12 +179,14 @@ public class WiiUseApiManager extends Thread {
* WiiUseApiManager.WIIUSE_STACK_BLUESOLEIL
* @return 0 if nothing connected or the number of wiimotes connected.
*/
private int connectWiimotes(int nb, boolean rumble, boolean forceStackType, int stackType) {
private int connectWiimotes(int nb, boolean rumble, boolean forceStackType,
int stackType) {
if (connected <= 0) {
int nbWiimotesFound;
wiiuse.init(nb);
//force bluetooth stack type ?
if (forceStackType) setBlueToothstackType(stackType);
// force bluetooth stack type ?
if (forceStackType)
setBlueToothstackType(stackType);
nbWiimotesFound = wiiuse.find(nb, 3);
connected = wiiuse.connect(nbWiimotesFound);
return connected;
@@ -206,7 +214,7 @@ public class WiiUseApiManager extends Thread {
if (connected == 0) {// stop this thread if there is
// no more wiimotes connected.
// stop thread
shutdown();
running.set(false);
}
/* Close connection in wiiuse */
wiiuse.closeConnection(index);
@@ -540,41 +548,50 @@ public class WiiUseApiManager extends Thread {
@Override
public void run() {
if (connected > 0) {
running.set(true);
while (true) {
try {
semaphore.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EventsGatherer gather = new EventsGatherer(connected);
if (connected > 0) {
running.set(true);
// Start polling and tell the observers when there are Wiimote
// events
while (running.get() && connected > 0) {
EventsGatherer gather = new EventsGatherer(connected);
/* Polling */
wiiuse.specialPoll(gather);
// Start polling and tell the observers when there are Wiimote
// events
while (running.get() && connected > 0) {
/* deal with events gathered in Wiiuse API */
for (WiiUseApiEvent evt : gather.getEvents()) {
if (evt.getWiimoteId() != -1) {// event filled
// there is an event notify observers
notifyWiiUseApiListener(evt);
if (evt.getEventType() == WiiUseApiEvent.DISCONNECTION_EVENT) {
// check if it was a disconnection
// in this case disconnect the wiimote
closeConnection(evt.getWiimoteId());
/* Polling */
wiiuse.specialPoll(gather);
/* deal with events gathered in Wiiuse API */
for (WiiUseApiEvent evt : gather.getEvents()) {
if (evt.getWiimoteId() != -1) {// event filled
// there is an event notify observers
notifyWiiUseApiListener(evt);
if (evt.getEventType() == WiiUseApiEvent.DISCONNECTION_EVENT) {
// check if it was a disconnection
// in this case disconnect the wiimote
closeConnection(evt.getWiimoteId());
}
} else {
System.out
.println("There is an event with id == -1 ??? there is a problem !!! : "
+ evt);
}
} else {
System.out
.println("There is an event with id == -1 ??? there is a problem !!! : "
+ evt);
}
gather.clearEvents();
}
} else {
if (connected <= 0) {
System.out.println("No wiimotes connected !");
}
gather.clearEvents();
}
} else {
if (connected <= 0) {
System.out.println("No wiimotes connected !");
}
}
}//end while true
}

View File

@@ -94,18 +94,53 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
initComponents();
this.addWindowListener(new CloseGuiTestCleanly());
this.wiimote = wiimote;
registerListeners();
initWiimote();
isFirstStatusGot = false;
getStatusButtonMousePressed(null);
}
/**
* Clear all views
*/
private void clearViews(){
((IRPanel) irViewPanel).clearView();
((ButtonsEventPanel) buttonsPanel).clearView();
((OrientationPanel) motionSensingPanel).clearView();
((GForcePanel) gForcePanel).clearView();
((AccelerationPanel) accelerationPanel).clearView();
}
/**
* Unregister all listeners.
*/
private void unregisterListeners(){
wiimote.removeWiiMoteEventListeners((IRPanel) irViewPanel);
wiimote.removeWiiMoteEventListeners((ButtonsEventPanel) buttonsPanel);
wiimote.removeWiiMoteEventListeners((OrientationPanel) motionSensingPanel);
wiimote.removeWiiMoteEventListeners((GForcePanel) gForcePanel);
wiimote.removeWiiMoteEventListeners((AccelerationPanel) accelerationPanel);
wiimote.removeWiiMoteEventListeners(this);
}
private void initWiimote(){
wiimote.deactivateContinuous();
wiimote.deactivateSmoothing();
wiimote.setScreenAspectRatio169();
wiimote.setSensorBarBelowScreen();
}
/**
* Register all listeners
*/
private void registerListeners(){
wiimote.addWiiMoteEventListeners((IRPanel) irViewPanel);
wiimote.addWiiMoteEventListeners((ButtonsEventPanel) buttonsPanel);
wiimote.addWiiMoteEventListeners((OrientationPanel) motionSensingPanel);
wiimote.addWiiMoteEventListeners((GForcePanel) gForcePanel);
wiimote.addWiiMoteEventListeners((AccelerationPanel) accelerationPanel);
wiimote.addWiiMoteEventListeners(this);
wiimote.deactivateContinuous();
wiimote.deactivateSmoothing();
wiimote.setScreenAspectRatio169();
wiimote.setSensorBarBelowScreen();
isFirstStatusGot = false;
getStatusButtonMousePressed(null);
}
public void onButtonsEvent(WiimoteButtonsEvent arg0) {
@@ -210,14 +245,10 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
public void onDisconnectionEvent(DisconnectionEvent arg0) {
messageText.setText("Wiimote Disconnected !");
wiimote.removeWiiMoteEventListeners((IRPanel) irViewPanel);
wiimote.removeWiiMoteEventListeners((ButtonsEventPanel) buttonsPanel);
wiimote.removeWiiMoteEventListeners((OrientationPanel) motionSensingPanel);
wiimote.removeWiiMoteEventListeners((GForcePanel) gForcePanel);
wiimote.removeWiiMoteEventListeners((AccelerationPanel) accelerationPanel);
wiimote.removeWiiMoteEventListeners(this);
unregisterListeners();
clearViews();
isFirstStatusGot = false;
}
}
public void onNunchukInsertedEvent(NunchukInsertedEvent e) {
messageText.setText("Nunchuk connected !");
@@ -1051,25 +1082,23 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
//stop manager
WiiUseApiManager manager = WiiUseApiManager.getInstance();
manager.shutdown();
//unregister previous wiimote
onDisconnectionEvent(null);
if (wiimote != null){
onDisconnectionEvent(null);
}
//get wiimote
wiimote = WiiUseApiManager.getWiimotes(1, true)[0];
//registers listeners
wiimote.addWiiMoteEventListeners((IRPanel) irViewPanel);
wiimote.addWiiMoteEventListeners((ButtonsEventPanel) buttonsPanel);
wiimote.addWiiMoteEventListeners((OrientationPanel) motionSensingPanel);
wiimote.addWiiMoteEventListeners((GForcePanel) gForcePanel);
wiimote.addWiiMoteEventListeners((AccelerationPanel) accelerationPanel);
wiimote.addWiiMoteEventListeners(this);
wiimote.deactivateContinuous();
wiimote.deactivateSmoothing();
wiimote.setScreenAspectRatio169();
wiimote.setSensorBarBelowScreen();
//Reset Gui
//remove frame for expansion
if (expansionFrame != null) {
if (expansionFrame instanceof NunchukGuiTest) {
((NunchukGuiTest) expansionFrame).unRegisterListeners();
}
expansionFrame.setEnabled(false);
expansionFrame.dispose();
expansionFrame = null;
}
//Reset Gui
//setup buttons In first state
toggleRumbleButton.setText("Activate Rumble");
toggleRumbleButton.setEnabled(true);
@@ -1088,24 +1117,30 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
mouseIRControlButton.setText("Start infrared mouse control");
mouseIRControlButton.setEnabled(true);
isFirstStatusGot = false;
getStatusButtonMousePressed(null);
//get wiimote
Wiimote[] listWiimote = WiiUseApiManager.getWiimotes(1, true);
if (listWiimote.length > 0){
wiimote = listWiimote[0];
//registers listeners
registerListeners();
initWiimote();
isFirstStatusGot = false;
getStatusButtonMousePressed(null);
}
}//GEN-LAST:event_reconnectWiimotesButtonMousePressed
private void showExpansionWiimoteButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showExpansionWiimoteButtonMousePressed
System.out.println("fdsfds");
if (expansionFrame != null) {
System.out.println("aaaaaaaaaa");
if (expansionFrame != null) {
if (showExpansionWiimoteButton.isEnabled()) {//expansion frame not shown
//show it
System.out.println("enabled");
//show it
expansionFrame.setEnabled(true);
expansionFrame.setVisible(true);
showExpansionWiimoteButton.setEnabled(false);
showExpansionWiimoteButton.setText("Hide Nunchuk");
messageText.setText("Nunchuk displayed !");
} else {//already being shown
System.out.println("desactivated");
expansionFrame.setEnabled(false);
expansionFrame.setVisible(false);
showExpansionWiimoteButton.setEnabled(true);

View File

@@ -250,6 +250,11 @@ public abstract class AccelerationPanel extends javax.swing.JPanel implements
this.zColor = zColor;
}
public void clearView(){
values.clear();
repaint();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always

View File

@@ -335,7 +335,10 @@ public class ButtonsEventPanel extends javax.swing.JPanel implements
this.shape = shape;
}
public void clearView(){
buttons = null;
repaint();
}
/**
* This method is called from within the constructor to initialize the form.

View File

@@ -252,7 +252,10 @@ public abstract class GForcePanel extends javax.swing.JPanel implements WiimoteL
this.zColor = zColor;
}
public void clearView(){
values.clear();
repaint();
}
/**
* This method is called from within the constructor to initialize the form.

View File

@@ -216,6 +216,11 @@ public class IRPanel extends javax.swing.JPanel implements WiimoteListener {
public void setShape(Shape shape) {
this.shape = shape;
}
public void clearView(){
initArrays();
repaint();
}
/** This method is called from within the constructor to
* initialize the form.

View File

@@ -254,7 +254,10 @@ public abstract class OrientationPanel extends javax.swing.JPanel implements
this.yawColor = yawColor;
}
public void clearView(){
values.clear();
repaint();
}
/**
* This method is called from within the constructor to initialize the form.