From eb0b4dcd010c3d670733d1b7071bbc133fbfef80 Mon Sep 17 00:00:00 2001 From: "guilhem.duche" Date: Mon, 26 May 2008 20:25:02 +0000 Subject: [PATCH] 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 --- WiiUseJ/src/wiiusej/WiiUseApiManager.java | 93 +++++++++------ WiiUseJ/src/wiiusej/test/WiiuseJGuiTest.java | 109 ++++++++++++------ .../src/wiiusej/utils/AccelerationPanel.java | 5 + .../src/wiiusej/utils/ButtonsEventPanel.java | 5 +- WiiUseJ/src/wiiusej/utils/GForcePanel.java | 5 +- WiiUseJ/src/wiiusej/utils/IRPanel.java | 5 + .../src/wiiusej/utils/OrientationPanel.java | 5 +- 7 files changed, 149 insertions(+), 78 deletions(-) diff --git a/WiiUseJ/src/wiiusej/WiiUseApiManager.java b/WiiUseJ/src/wiiusej/WiiUseApiManager.java index caa81a2..8c6302e 100644 --- a/WiiUseJ/src/wiiusej/WiiUseApiManager.java +++ b/WiiUseJ/src/wiiusej/WiiUseApiManager.java @@ -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 } diff --git a/WiiUseJ/src/wiiusej/test/WiiuseJGuiTest.java b/WiiUseJ/src/wiiusej/test/WiiuseJGuiTest.java index 4e1ef3f..63b9476 100644 --- a/WiiUseJ/src/wiiusej/test/WiiuseJGuiTest.java +++ b/WiiUseJ/src/wiiusej/test/WiiuseJGuiTest.java @@ -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); diff --git a/WiiUseJ/src/wiiusej/utils/AccelerationPanel.java b/WiiUseJ/src/wiiusej/utils/AccelerationPanel.java index c441e5e..58659f6 100644 --- a/WiiUseJ/src/wiiusej/utils/AccelerationPanel.java +++ b/WiiUseJ/src/wiiusej/utils/AccelerationPanel.java @@ -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 diff --git a/WiiUseJ/src/wiiusej/utils/ButtonsEventPanel.java b/WiiUseJ/src/wiiusej/utils/ButtonsEventPanel.java index 4497e28..58a1e44 100644 --- a/WiiUseJ/src/wiiusej/utils/ButtonsEventPanel.java +++ b/WiiUseJ/src/wiiusej/utils/ButtonsEventPanel.java @@ -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. diff --git a/WiiUseJ/src/wiiusej/utils/GForcePanel.java b/WiiUseJ/src/wiiusej/utils/GForcePanel.java index bc6d86a..ce002b8 100644 --- a/WiiUseJ/src/wiiusej/utils/GForcePanel.java +++ b/WiiUseJ/src/wiiusej/utils/GForcePanel.java @@ -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. diff --git a/WiiUseJ/src/wiiusej/utils/IRPanel.java b/WiiUseJ/src/wiiusej/utils/IRPanel.java index 913c308..9b61852 100644 --- a/WiiUseJ/src/wiiusej/utils/IRPanel.java +++ b/WiiUseJ/src/wiiusej/utils/IRPanel.java @@ -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. diff --git a/WiiUseJ/src/wiiusej/utils/OrientationPanel.java b/WiiUseJ/src/wiiusej/utils/OrientationPanel.java index c67bf0d..b9b63a8 100644 --- a/WiiUseJ/src/wiiusej/utils/OrientationPanel.java +++ b/WiiUseJ/src/wiiusej/utils/OrientationPanel.java @@ -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.