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:
@@ -16,6 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package wiiusej;
|
package wiiusej;
|
||||||
|
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import javax.swing.event.EventListenerList;
|
import javax.swing.event.EventListenerList;
|
||||||
@@ -36,6 +37,8 @@ public class WiiUseApiManager extends Thread {
|
|||||||
|
|
||||||
private final EventListenerList listeners = new EventListenerList();
|
private final EventListenerList listeners = new EventListenerList();
|
||||||
|
|
||||||
|
private Semaphore semaphore = new Semaphore(0);
|
||||||
|
|
||||||
private Wiimote[] wiimotes;
|
private Wiimote[] wiimotes;
|
||||||
|
|
||||||
private WiiUseApi wiiuse = WiiUseApi.getInstance();
|
private WiiUseApi wiiuse = WiiUseApi.getInstance();
|
||||||
@@ -64,7 +67,7 @@ public class WiiUseApiManager extends Thread {
|
|||||||
*
|
*
|
||||||
* @return an array with connected wiimotes or NULL.
|
* @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);
|
return getWiimotesPrivate(nb, rumble, false, WIIUSE_STACK_UNKNOWN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +87,7 @@ public class WiiUseApiManager extends Thread {
|
|||||||
*
|
*
|
||||||
* @return an array with connected wiimotes or NULL.
|
* @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);
|
return getWiimotesPrivate(nb, rumble, true, stackType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,11 +109,12 @@ public class WiiUseApiManager extends Thread {
|
|||||||
*
|
*
|
||||||
* @return an array with connected wiimotes or NULL.
|
* @return an array with connected wiimotes or NULL.
|
||||||
*/
|
*/
|
||||||
private synchronized static Wiimote[] getWiimotesPrivate(int nb, boolean rumble,
|
private synchronized static Wiimote[] getWiimotesPrivate(int nb,
|
||||||
boolean forceStackType, int stackType) {
|
boolean rumble, boolean forceStackType, int stackType) {
|
||||||
WiiUseApiManager manager = getInstance();
|
WiiUseApiManager manager = getInstance();
|
||||||
if (manager.connected <= 0 && !manager.running.get()) {
|
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];
|
manager.wiimotes = new Wiimote[nbWiimotes];
|
||||||
for (int i = 0; i < nbWiimotes; i++) {
|
for (int i = 0; i < nbWiimotes; i++) {
|
||||||
Wiimote wim = new Wiimote(WiiUseApi.getInstance().getUnId(i),
|
Wiimote wim = new Wiimote(WiiUseApi.getInstance().getUnId(i),
|
||||||
@@ -154,6 +158,8 @@ public class WiiUseApiManager extends Thread {
|
|||||||
if (!manager.isAlive())
|
if (!manager.isAlive())
|
||||||
manager.start();
|
manager.start();
|
||||||
|
|
||||||
|
manager.semaphore.release();
|
||||||
|
|
||||||
return manager.wiimotes;
|
return manager.wiimotes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,12 +179,14 @@ public class WiiUseApiManager extends Thread {
|
|||||||
* WiiUseApiManager.WIIUSE_STACK_BLUESOLEIL
|
* WiiUseApiManager.WIIUSE_STACK_BLUESOLEIL
|
||||||
* @return 0 if nothing connected or the number of wiimotes connected.
|
* @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) {
|
if (connected <= 0) {
|
||||||
int nbWiimotesFound;
|
int nbWiimotesFound;
|
||||||
wiiuse.init(nb);
|
wiiuse.init(nb);
|
||||||
//force bluetooth stack type ?
|
// force bluetooth stack type ?
|
||||||
if (forceStackType) setBlueToothstackType(stackType);
|
if (forceStackType)
|
||||||
|
setBlueToothstackType(stackType);
|
||||||
nbWiimotesFound = wiiuse.find(nb, 3);
|
nbWiimotesFound = wiiuse.find(nb, 3);
|
||||||
connected = wiiuse.connect(nbWiimotesFound);
|
connected = wiiuse.connect(nbWiimotesFound);
|
||||||
return connected;
|
return connected;
|
||||||
@@ -206,7 +214,7 @@ public class WiiUseApiManager extends Thread {
|
|||||||
if (connected == 0) {// stop this thread if there is
|
if (connected == 0) {// stop this thread if there is
|
||||||
// no more wiimotes connected.
|
// no more wiimotes connected.
|
||||||
// stop thread
|
// stop thread
|
||||||
shutdown();
|
running.set(false);
|
||||||
}
|
}
|
||||||
/* Close connection in wiiuse */
|
/* Close connection in wiiuse */
|
||||||
wiiuse.closeConnection(index);
|
wiiuse.closeConnection(index);
|
||||||
@@ -540,41 +548,50 @@ public class WiiUseApiManager extends Thread {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
if (connected > 0) {
|
while (true) {
|
||||||
running.set(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
|
EventsGatherer gather = new EventsGatherer(connected);
|
||||||
// events
|
|
||||||
while (running.get() && connected > 0) {
|
|
||||||
|
|
||||||
/* Polling */
|
// Start polling and tell the observers when there are Wiimote
|
||||||
wiiuse.specialPoll(gather);
|
// events
|
||||||
|
while (running.get() && connected > 0) {
|
||||||
|
|
||||||
/* deal with events gathered in Wiiuse API */
|
/* Polling */
|
||||||
for (WiiUseApiEvent evt : gather.getEvents()) {
|
wiiuse.specialPoll(gather);
|
||||||
if (evt.getWiimoteId() != -1) {// event filled
|
|
||||||
// there is an event notify observers
|
/* deal with events gathered in Wiiuse API */
|
||||||
notifyWiiUseApiListener(evt);
|
for (WiiUseApiEvent evt : gather.getEvents()) {
|
||||||
if (evt.getEventType() == WiiUseApiEvent.DISCONNECTION_EVENT) {
|
if (evt.getWiimoteId() != -1) {// event filled
|
||||||
// check if it was a disconnection
|
// there is an event notify observers
|
||||||
// in this case disconnect the wiimote
|
notifyWiiUseApiListener(evt);
|
||||||
closeConnection(evt.getWiimoteId());
|
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 {
|
}//end while true
|
||||||
if (connected <= 0) {
|
|
||||||
System.out.println("No wiimotes connected !");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -94,18 +94,53 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
|
|||||||
initComponents();
|
initComponents();
|
||||||
this.addWindowListener(new CloseGuiTestCleanly());
|
this.addWindowListener(new CloseGuiTestCleanly());
|
||||||
this.wiimote = wiimote;
|
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((IRPanel) irViewPanel);
|
||||||
wiimote.addWiiMoteEventListeners((ButtonsEventPanel) buttonsPanel);
|
wiimote.addWiiMoteEventListeners((ButtonsEventPanel) buttonsPanel);
|
||||||
wiimote.addWiiMoteEventListeners((OrientationPanel) motionSensingPanel);
|
wiimote.addWiiMoteEventListeners((OrientationPanel) motionSensingPanel);
|
||||||
wiimote.addWiiMoteEventListeners((GForcePanel) gForcePanel);
|
wiimote.addWiiMoteEventListeners((GForcePanel) gForcePanel);
|
||||||
wiimote.addWiiMoteEventListeners((AccelerationPanel) accelerationPanel);
|
wiimote.addWiiMoteEventListeners((AccelerationPanel) accelerationPanel);
|
||||||
wiimote.addWiiMoteEventListeners(this);
|
wiimote.addWiiMoteEventListeners(this);
|
||||||
wiimote.deactivateContinuous();
|
|
||||||
wiimote.deactivateSmoothing();
|
|
||||||
wiimote.setScreenAspectRatio169();
|
|
||||||
wiimote.setSensorBarBelowScreen();
|
|
||||||
isFirstStatusGot = false;
|
|
||||||
getStatusButtonMousePressed(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onButtonsEvent(WiimoteButtonsEvent arg0) {
|
public void onButtonsEvent(WiimoteButtonsEvent arg0) {
|
||||||
@@ -210,12 +245,8 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
|
|||||||
|
|
||||||
public void onDisconnectionEvent(DisconnectionEvent arg0) {
|
public void onDisconnectionEvent(DisconnectionEvent arg0) {
|
||||||
messageText.setText("Wiimote Disconnected !");
|
messageText.setText("Wiimote Disconnected !");
|
||||||
wiimote.removeWiiMoteEventListeners((IRPanel) irViewPanel);
|
unregisterListeners();
|
||||||
wiimote.removeWiiMoteEventListeners((ButtonsEventPanel) buttonsPanel);
|
clearViews();
|
||||||
wiimote.removeWiiMoteEventListeners((OrientationPanel) motionSensingPanel);
|
|
||||||
wiimote.removeWiiMoteEventListeners((GForcePanel) gForcePanel);
|
|
||||||
wiimote.removeWiiMoteEventListeners((AccelerationPanel) accelerationPanel);
|
|
||||||
wiimote.removeWiiMoteEventListeners(this);
|
|
||||||
isFirstStatusGot = false;
|
isFirstStatusGot = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1051,25 +1082,23 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
|
|||||||
//stop manager
|
//stop manager
|
||||||
WiiUseApiManager manager = WiiUseApiManager.getInstance();
|
WiiUseApiManager manager = WiiUseApiManager.getInstance();
|
||||||
manager.shutdown();
|
manager.shutdown();
|
||||||
|
|
||||||
//unregister previous wiimote
|
//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
|
//Reset Gui
|
||||||
|
//remove frame for expansion
|
||||||
|
if (expansionFrame != null) {
|
||||||
|
if (expansionFrame instanceof NunchukGuiTest) {
|
||||||
|
((NunchukGuiTest) expansionFrame).unRegisterListeners();
|
||||||
|
}
|
||||||
|
expansionFrame.setEnabled(false);
|
||||||
|
expansionFrame.dispose();
|
||||||
|
expansionFrame = null;
|
||||||
|
}
|
||||||
|
|
||||||
//setup buttons In first state
|
//setup buttons In first state
|
||||||
toggleRumbleButton.setText("Activate Rumble");
|
toggleRumbleButton.setText("Activate Rumble");
|
||||||
toggleRumbleButton.setEnabled(true);
|
toggleRumbleButton.setEnabled(true);
|
||||||
@@ -1088,24 +1117,30 @@ public class WiiuseJGuiTest extends javax.swing.JFrame implements WiimoteListene
|
|||||||
mouseIRControlButton.setText("Start infrared mouse control");
|
mouseIRControlButton.setText("Start infrared mouse control");
|
||||||
mouseIRControlButton.setEnabled(true);
|
mouseIRControlButton.setEnabled(true);
|
||||||
|
|
||||||
isFirstStatusGot = false;
|
//get wiimote
|
||||||
getStatusButtonMousePressed(null);
|
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
|
}//GEN-LAST:event_reconnectWiimotesButtonMousePressed
|
||||||
|
|
||||||
private void showExpansionWiimoteButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showExpansionWiimoteButtonMousePressed
|
private void showExpansionWiimoteButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showExpansionWiimoteButtonMousePressed
|
||||||
System.out.println("fdsfds");
|
|
||||||
if (expansionFrame != null) {
|
if (expansionFrame != null) {
|
||||||
System.out.println("aaaaaaaaaa");
|
|
||||||
if (showExpansionWiimoteButton.isEnabled()) {//expansion frame not shown
|
if (showExpansionWiimoteButton.isEnabled()) {//expansion frame not shown
|
||||||
//show it
|
//show it
|
||||||
System.out.println("enabled");
|
|
||||||
expansionFrame.setEnabled(true);
|
expansionFrame.setEnabled(true);
|
||||||
expansionFrame.setVisible(true);
|
expansionFrame.setVisible(true);
|
||||||
showExpansionWiimoteButton.setEnabled(false);
|
showExpansionWiimoteButton.setEnabled(false);
|
||||||
showExpansionWiimoteButton.setText("Hide Nunchuk");
|
showExpansionWiimoteButton.setText("Hide Nunchuk");
|
||||||
messageText.setText("Nunchuk displayed !");
|
messageText.setText("Nunchuk displayed !");
|
||||||
} else {//already being shown
|
} else {//already being shown
|
||||||
System.out.println("desactivated");
|
|
||||||
expansionFrame.setEnabled(false);
|
expansionFrame.setEnabled(false);
|
||||||
expansionFrame.setVisible(false);
|
expansionFrame.setVisible(false);
|
||||||
showExpansionWiimoteButton.setEnabled(true);
|
showExpansionWiimoteButton.setEnabled(true);
|
||||||
|
|||||||
@@ -250,6 +250,11 @@ public abstract class AccelerationPanel extends javax.swing.JPanel implements
|
|||||||
this.zColor = zColor;
|
this.zColor = zColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearView(){
|
||||||
|
values.clear();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called from within the constructor to initialize the form.
|
* 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
|
* WARNING: Do NOT modify this code. The content of this method is always
|
||||||
|
|||||||
@@ -335,7 +335,10 @@ public class ButtonsEventPanel extends javax.swing.JPanel implements
|
|||||||
this.shape = shape;
|
this.shape = shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearView(){
|
||||||
|
buttons = null;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called from within the constructor to initialize the form.
|
* This method is called from within the constructor to initialize the form.
|
||||||
|
|||||||
@@ -252,7 +252,10 @@ public abstract class GForcePanel extends javax.swing.JPanel implements WiimoteL
|
|||||||
this.zColor = zColor;
|
this.zColor = zColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearView(){
|
||||||
|
values.clear();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called from within the constructor to initialize the form.
|
* This method is called from within the constructor to initialize the form.
|
||||||
|
|||||||
@@ -217,6 +217,11 @@ public class IRPanel extends javax.swing.JPanel implements WiimoteListener {
|
|||||||
this.shape = shape;
|
this.shape = shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearView(){
|
||||||
|
initArrays();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
/** This method is called from within the constructor to
|
/** This method is called from within the constructor to
|
||||||
* initialize the form.
|
* initialize the form.
|
||||||
* WARNING: Do NOT modify this code. The content of this method is
|
* WARNING: Do NOT modify this code. The content of this method is
|
||||||
|
|||||||
@@ -254,7 +254,10 @@ public abstract class OrientationPanel extends javax.swing.JPanel implements
|
|||||||
this.yawColor = yawColor;
|
this.yawColor = yawColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearView(){
|
||||||
|
values.clear();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called from within the constructor to initialize the form.
|
* This method is called from within the constructor to initialize the form.
|
||||||
|
|||||||
Reference in New Issue
Block a user