From 0b143596dd6c5c4eae7092c65a37b6bdba62271f Mon Sep 17 00:00:00 2001 From: "guilhem.duche" Date: Sat, 24 May 2008 09:43:49 +0000 Subject: [PATCH] wiiuseJ 0.12 work in progress. Added method to force stack type and fixed IR.z value and nunchuk thresholds. git-svn-id: http://wiiusej.googlecode.com/svn/trunk@144 ae48ae66-6a45-0410-b38e-211266189506 --- WiiUseJ/src/wiiusej/WiiUseApi.java | 20 ++- WiiUseJ/src/wiiusej/WiiUseApiManager.java | 119 ++++++++++++++---- WiiUseJ/src/wiiusej/Wiimote.java | 40 ++++-- .../wiiusejevents/physicalevents/IREvent.java | 6 +- .../wiiusejevents/utils/EventsGatherer.java | 2 +- .../wiiuseapievents/StatusEvent.java | 43 ++++++- .../wiiuseapievents/WiimoteEvent.java | 2 +- 7 files changed, 180 insertions(+), 52 deletions(-) diff --git a/WiiUseJ/src/wiiusej/WiiUseApi.java b/WiiUseJ/src/wiiusej/WiiUseApi.java index c5d4866..5d5f06c 100644 --- a/WiiUseJ/src/wiiusej/WiiUseApi.java +++ b/WiiUseJ/src/wiiusej/WiiUseApi.java @@ -309,7 +309,8 @@ public class WiiUseApi { synchronized native void setIrSensitivity(int id, int level); /** - * Set how many degrees an angle must change to generate an event for the nunchuk. + * Set how many degrees an angle must change to generate an event for the + * nunchuk. * * @param id * id of the wiimote concerned. @@ -317,9 +318,10 @@ public class WiiUseApi { * minimum angle detected by an event. */ synchronized native void setNunchukOrientationThreshold(int id, float angle); - + /** - * Set how much acceleration must change to generate an event for the nunchuk. + * Set how much acceleration must change to generate an event for the + * nunchuk. * * @param id * id of the wiimote concerned. @@ -327,8 +329,16 @@ public class WiiUseApi { * minimum value detected by an event. */ synchronized native void setNunchukAccelerationThreshold(int id, int value); - - + + /** + * Force the bluetooth stack type.(useful only for windows) + * + * @param bluetoothStackType + * must be WiiUseApi.WIIUSE_STACK_UNKNOWN or WiiUseApi.WIIUSE_STACK_MS or + * WiiUseApi.WIIUSE_STACK_BLUESOLEIL. + */ + native void windowsSetBluetoothStack(int bluetoothStackType); + /** * Check for new Events and Get it. * diff --git a/WiiUseJ/src/wiiusej/WiiUseApiManager.java b/WiiUseJ/src/wiiusej/WiiUseApiManager.java index 62f759e..d68f503 100644 --- a/WiiUseJ/src/wiiusej/WiiUseApiManager.java +++ b/WiiUseJ/src/wiiusej/WiiUseApiManager.java @@ -44,9 +44,49 @@ public class WiiUseApiManager extends Thread { private AtomicBoolean running = new AtomicBoolean(false); + public static int WIIUSE_STACK_UNKNOWN = 0; + public static int WIIUSE_STACK_MS = 1; + public static int WIIUSE_STACK_BLUESOLEIL = 2; + 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. + * + * @param nb + * try to connect nb wiimotes. + * @param rumble + * make the connected wiimotes rumble. + * + * @return an array with connected wiimotes or NULL. + */ + 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 + * wiimotes. + * + * @param nb + * try to connect nb wiimotes. + * @param rumble + * make the connected wiimotes rumble.* + * @param stackType + * the stack type : WiiUseApiManager.WIIUSE_STACK_UNKNOWN or + * WiiUseApiManager.WIIUSE_STACK_MS or + * WiiUseApiManager.WIIUSE_STACK_BLUESOLEIL + * + * @return an array with connected wiimotes or NULL. + */ + public static Wiimote[] getWiimotes(int nb, boolean rumble, int stackType){ + return getWiimotesPrivate(nb, rumble, true, stackType); + } /** * Get wiimotes. Load library if necessary. Connect to wiimotes if @@ -56,42 +96,50 @@ public class WiiUseApiManager extends Thread { * @param nb * try to connect nb wiimotes. * @param rumble - * make the connected wiimotes rumble. + * make the connected wiimotes rumble.* + * @param forceStackType + * true if we want to force the stack type. + * @param stackType + * the stack type : WiiUseApiManager.WIIUSE_STACK_UNKNOWN or + * WiiUseApiManager.WIIUSE_STACK_MS or + * WiiUseApiManager.WIIUSE_STACK_BLUESOLEIL + * * @return an array with connected wiimotes or NULL. */ - public synchronized static Wiimote[] getWiimotes(int nb, boolean rumble) { + 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); + 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), manager); manager.wiimotes[i] = wim; - manager.addWiiUseApiListener(wim); + manager.addWiiUseApiListener(wim); } - //Set leds on wiimote + // Set leds on wiimote for (Wiimote wiimote : manager.wiimotes) { int id = wiimote.getId(); short leds = 0; - if (id%4==0){ + if (id % 4 == 0) { wiimote.setLeds(true, true, true, true); - }else if (id%4==1){ + } else if (id % 4 == 1) { wiimote.setLeds(true, false, false, false); - }else if (id%4==2){ + } else if (id % 4 == 2) { wiimote.setLeds(true, true, false, false); - }else if (id%4==3){ + } else if (id % 4 == 3) { wiimote.setLeds(true, true, true, false); - } + } } - //make the connected wiimotes rumble + // make the connected wiimotes rumble if (rumble) { - for (Wiimote wiimote : manager.wiimotes) { + for (Wiimote wiimote : manager.wiimotes) { wiimote.activateRumble(); } try { sleep(500); - } catch (InterruptedException e) { + } catch (InterruptedException e) { } for (Wiimote wiimote : manager.wiimotes) { wiimote.deactivateRumble(); @@ -117,12 +165,20 @@ public class WiiUseApiManager extends Thread { * try to connect nb wiimotes * @param rumble * make the connected wiimotes rumble + * @param forceStackType + * true if we want to force the stack type. + * @param stackType + * the stack type : WiiUseApiManager.WIIUSE_STACK_UNKNOWN or + * WiiUseApiManager.WIIUSE_STACK_MS or + * WiiUseApiManager.WIIUSE_STACK_BLUESOLEIL * @return 0 if nothing connected or the number of wiimotes connected. */ - private int connectWiimotes(int nb, boolean rumble) { + 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); nbWiimotesFound = wiiuse.find(nb, 3); connected = wiiuse.connect(nbWiimotesFound); return connected; @@ -302,8 +358,8 @@ public class WiiUseApiManager extends Thread { } /** - * Set the orientation threshold for the given id. - * (minimum angle between two events) + * Set the orientation threshold for the given id. (minimum angle between + * two events) * * @param id * id of the wiimote. @@ -315,8 +371,8 @@ public class WiiUseApiManager extends Thread { } /** - * Set the acceleration threshold for the given id. - * (minimum angle between two events) + * Set the acceleration threshold for the given id. (minimum angle between + * two events) * * @param id * id of the wiimote. @@ -443,33 +499,44 @@ public class WiiUseApiManager extends Thread { public void setIrSensitivity(int id, int level) { wiiuse.setIrSensitivity(id, level); } - + /** - * Set the nunchuk orientation threshold for the given id. - * (minimum angle between two events) + * Set the nunchuk orientation threshold for the given id. (minimum angle + * between two events) * * @param id * id of the wiimote. * @param th * threshold in degrees. */ - public void setNunchukOrientationThreshold(int id, float th){ + public void setNunchukOrientationThreshold(int id, float th) { wiiuse.setNunchukOrientationThreshold(id, th); } - + /** - * Set the nunchuk acceleration threshold for the given id. - * (minimum angle between two events) + * Set the nunchuk acceleration threshold for the given id. (minimum angle + * between two events) * * @param id * id of the wiimote. * @param th * threshold. */ - public void setNunchukAccelerationThreshold(int id, int th){ + public void setNunchukAccelerationThreshold(int id, int th) { wiiuse.setNunchukAccelerationThreshold(id, th); } + /** + * Force the bluetooth stack type.(useful only for windows) + * + * @param type + * must be WIIUSE_STACK_UNKNOWN or WIIUSE_STACK_MS or + * WIIUSE_STACK_BLUESOLEIL. + */ + private void setBlueToothstackType(int type) { + + } + @Override public void run() { diff --git a/WiiUseJ/src/wiiusej/Wiimote.java b/WiiUseJ/src/wiiusej/Wiimote.java index 58d921d..98a5233 100644 --- a/WiiUseJ/src/wiiusej/Wiimote.java +++ b/WiiUseJ/src/wiiusej/Wiimote.java @@ -231,26 +231,26 @@ public class Wiimote implements WiiUseApiListener { public void setVirtualResolution(int x, int y) { manager.setVirtualResolution(id, x, y); } - + /** - * Set the nunchuk orientation threshold for the given id. - * (minimum angle between two events) + * Set the nunchuk orientation threshold for the given id. (minimum angle + * between two events) * * @param th * threshold in degrees. */ - public void setNunchukOrientationThreshold(float th){ + public void setNunchukOrientationThreshold(float th) { manager.setNunchukOrientationThreshold(id, th); } - + /** - * Set the nunchuk acceleration threshold for the given id. - * (minimum angle between two events) + * Set the nunchuk acceleration threshold for the given id. (minimum angle + * between two events) * * @param th * threshold. */ - public void setNunchukAccelerationThreshold(int th){ + public void setNunchukAccelerationThreshold(int th) { manager.setNunchukAccelerationThreshold(id, th); } @@ -270,6 +270,21 @@ public class Wiimote implements WiiUseApiListener { manager.getStatus(id); } + /** + * Set the normal and expansion handshake timeouts for this wiimote. Normal + * time out is for classic polling. The expansion timeout is used when an + * expansion is detected until the expansion successfully handshakes. + * + * @param normalTimeout + * The timeout in milliseconds for a normal read. + * @param expansionTimeout + * The timeout in millisecondsd to wait for an expansion + * handshake. + */ + public void setTimeout(short normalTimeout, short expansionTimeout) { + manager.setTimeout(id, normalTimeout, expansionTimeout); + } + /** * Set the IR sensitivity. * @@ -297,13 +312,12 @@ public class Wiimote implements WiiUseApiListener { } else if (e.getEventType() == WiiUseApiEvent.DISCONNECTION_EVENT) { notifyDisconnectionEventListeners((DisconnectionEvent) e); } else if (e.getEventType() == WiiUseApiEvent.WIIUSE_NUNCHUK_INSERTED) { - notifyNunchukInsertedEventListeners((NunchukInsertedEvent)e); + notifyNunchukInsertedEventListeners((NunchukInsertedEvent) e); } else if (e.getEventType() == WiiUseApiEvent.WIIUSE_NUNCHUK_REMOVED) { - notifyNunchukRemovedEventListeners((NunchukRemovedEvent)e); + notifyNunchukRemovedEventListeners((NunchukRemovedEvent) e); } /* - * events not managed yet - * || e.getEventType() == WIIUSE_READ_DATA + * events not managed yet || e.getEventType() == WIIUSE_READ_DATA * WiiUseApiEvent.WIIUSE_CLASSIC_CTRL_INSERTED || e.getEventType() == * WiiUseApiEvent.WIIUSE_CLASSIC_CTRL_REMOVED || e.getEventType() == * WiiUseApiEvent.WIIUSE_GUITAR_HERO_3_CTRL_INSERTED || @@ -359,7 +373,7 @@ public class Wiimote implements WiiUseApiListener { if (evt.isThereMotionSensingEvent()) { listener.onMotionSensingEvent(evt.getMotionSensingEvent()); } - if (evt.isThereExpansionEvent()){ + if (evt.isThereExpansionEvent()) { listener.onExpansionEvent(evt.getExpansionEvent()); } } diff --git a/WiiUseJ/src/wiiusej/wiiusejevents/physicalevents/IREvent.java b/WiiUseJ/src/wiiusej/wiiusejevents/physicalevents/IREvent.java index fee9b67..1873a8c 100644 --- a/WiiUseJ/src/wiiusej/wiiusejevents/physicalevents/IREvent.java +++ b/WiiUseJ/src/wiiusej/wiiusejevents/physicalevents/IREvent.java @@ -31,7 +31,7 @@ public class IREvent extends GenericEvent { private short indexPoints = 0; private int x; private int y; - private int z;// distance from the sensor bar + private float z;// distance from the sensor bar private int ax; private int ay; private int xVRes; @@ -82,7 +82,7 @@ public class IREvent extends GenericEvent { * @param distance * Pixel Distance between first two dots */ - public IREvent(int id, int x, int y, int z, int ax, int ay, int xVRes, + public IREvent(int id, int x, int y, float z, int ax, int ay, int xVRes, int yVRes, int xOffset, int yOffset, short sensorBarPostion, short screenAsPectRatio, short irSensitivity, float distance) { super(id); @@ -154,7 +154,7 @@ public class IREvent extends GenericEvent { * * @return the z */ - public int getZ() { + public float getZ() { return z; } diff --git a/WiiUseJ/src/wiiusej/wiiusejevents/utils/EventsGatherer.java b/WiiUseJ/src/wiiusej/wiiusejevents/utils/EventsGatherer.java index 4b15516..003dd79 100644 --- a/WiiUseJ/src/wiiusej/wiiusejevents/utils/EventsGatherer.java +++ b/WiiUseJ/src/wiiusej/wiiusejevents/utils/EventsGatherer.java @@ -103,7 +103,7 @@ public class EventsGatherer { * @param distance * Pixel Distance between first two dots. */ - public void prepareIRevent(int x, int y, int z, int ax, int ay, int xVRes, + public void prepareIRevent(int x, int y, float z, int ax, int ay, int xVRes, int yVRes, int xOffset, int yOffset, short sensorBarPostion, short screenAsPectRatio, short irSensitivity, float distance) { genericEvent.prepareIRevent(x, y, z, ax, ay, xVRes, yVRes, xOffset, diff --git a/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/StatusEvent.java b/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/StatusEvent.java index 9c2a6d4..8d41f2d 100644 --- a/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/StatusEvent.java +++ b/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/StatusEvent.java @@ -16,7 +16,6 @@ */ package wiiusej.wiiusejevents.wiiuseapievents; - /** * Class used to represent a status event. This class is used to know what are * the settings of the wiimote. @@ -131,10 +130,12 @@ public class StatusEvent extends WiiUseApiEvent { public short getLeds() { return leds; } - + /** * Tells if the given led is turned on according to the leds status int. - * @param led the int encoding a led. + * + * @param led + * the int encoding a led. * @return true if the led is turned on false otherwise. */ private boolean ledStatusCheck(short led) { @@ -235,6 +236,42 @@ public class StatusEvent extends WiiUseApiEvent { return isMotionSensingActive; } + /** + * Tells if an attachment is connected. + * + * @return true if anything is connected to the wiimote false otherwise. + */ + public boolean isAttachmentConnected() { + return attachment != EXP_NONE; + } + + /** + * Tells if a nunchuk is connected. + * + * @return true if a nunchuk is connected to the wiimote false otherwise. + */ + public boolean isNunchukConnected() { + return attachment != EXP_NUNCHUK; + } + + /** + * Tells if a classic controller is connected. + * + * @return true if a classic controller is connected to the wiimote false otherwise. + */ + public boolean isClassicControllerConnected() { + return attachment != EXP_CLASSIC; + } + + /** + * Tells if a guitar hero controller is connected. + * + * @return true if a guitar hero controllerr is connected to the wiimote false otherwise. + */ + public boolean isGuitarHeroConnected() { + return attachment != EXP_GUITAR_HERO_3; + } + @Override public String toString() { String out = ""; diff --git a/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/WiimoteEvent.java b/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/WiimoteEvent.java index 3bc6d78..8135be7 100644 --- a/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/WiimoteEvent.java +++ b/WiiUseJ/src/wiiusej/wiiusejevents/wiiuseapievents/WiimoteEvent.java @@ -157,7 +157,7 @@ public class WiimoteEvent extends WiiUseApiEvent { * @param distance * Pixel Distance between first two dots */ - public void prepareIRevent(int x, int y, int z, int ax, int ay, int xVRes, + public void prepareIRevent(int x, int y, float z, int ax, int ay, int xVRes, int yVRes, int xOffset, int yOffset, short sensorBarPostion, short screenAsPectRatio, short irSensitivity, float distance) { if (infraredEvent == null) {