diff --git a/WiiUseJ/src/tests/LedsTask.java b/WiiUseJ/src/tests/LedsTask.java new file mode 100644 index 0000000..d4adda2 --- /dev/null +++ b/WiiUseJ/src/tests/LedsTask.java @@ -0,0 +1,30 @@ +package tests; + +import java.util.TimerTask; + +import wiiusej.WiiUseApiManager; + +public class LedsTask extends TimerTask { + + private int i = 0; + private int inc = 1; + + @Override + public void run() { + if(i==0){ + WiiUseApiManager.getInstance().setLeds(1, true, false, false, false); + }else if(i==1){ + WiiUseApiManager.getInstance().setLeds(1, false, true, false, false); + }else if(i==2){ + WiiUseApiManager.getInstance().setLeds(1, false, false, true, false); + }else if(i==3){ + WiiUseApiManager.getInstance().setLeds(1, false, false, false, true); + } + if (i==0) i=1; + if (i==3) i=-1; + i = i+inc; + + } + +} + diff --git a/WiiUseJ/src/tests/Tests.java b/WiiUseJ/src/tests/Tests.java new file mode 100644 index 0000000..4528e62 --- /dev/null +++ b/WiiUseJ/src/tests/Tests.java @@ -0,0 +1,377 @@ +package tests; + +import java.awt.AWTException; +import java.awt.Robot; +import java.awt.event.InputEvent; +import wiiusej.Point2DInteger; +import wiiusej.WiiMoteEvent; +import wiiusej.WiiUseApiListener; +import wiiusej.WiiUseApiManager; + +/** + * This class used to test this API. + * @author gduche + * + */ +public class Tests implements WiiUseApiListener { + + Robot robot; + + private static int DISPLAY_EACH_VALUE = 1; + private static int DUMP = 2; + private static int MOVE_MOUSE = 3; + private static int ORIENT_THRESH_CONT = 4; + private static int TEST_LEDS = 5; + + int dump = ORIENT_THRESH_CONT; + + public Tests() { + try { + robot = new Robot(); + } catch (AWTException e) { + e.printStackTrace(); + } + } + + @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 : "+ + // e.getWiimoteId() + " **************"); + /* button ONE */ + if (e.isButtonOneJustPressed()) { + System.out.println("button one pressed"); + } + if (e.isButtonOneHeld()) { + System.out.println("button one held"); + } + if (e.isButtonOneJustReleased()) { + System.out.println("button one released"); + } + + /* button TWO */ + if (e.isButtonTwoJustPressed()) { + System.out.println("button two pressed"); + } + if (e.isButtonTwoHeld()) { + System.out.println("button two held"); + } + if (e.isButtonTwoJustReleased()) { + System.out.println("button two released"); + } + + /* button A */ + if (e.isButtonAJustPressed()) { + System.out.println("button A pressed"); + } + if (e.isButtonAHeld()) { + System.out.println("button A held"); + } + if (e.isButtonAJustReleased()) { + System.out.println("button A released"); + } + + /* button B */ + if (e.isButtonBJustPressed()) { + System.out.println("button B pressed"); + } + if (e.isButtonBHeld()) { + System.out.println("button B held"); + } + if (e.isButtonBJustReleased()) { + System.out.println("button B released"); + } + + /* button LEFT */ + if (e.isButtonLeftJustPressed()) { + System.out.println("button Left pressed"); + } + if (e.isButtonLeftHeld()) { + System.out.println("button Left held"); + } + if (e.isButtonLeftJustReleased()) { + System.out.println("button Left released"); + } + + /* button RIGHT */ + if (e.isButtonRightJustPressed()) { + System.out.println("button Right pressed"); + } + if (e.isButtonRightHeld()) { + System.out.println("button Right held"); + } + if (e.isButtonRightJustReleased()) { + System.out.println("button Right released"); + } + + /* button UP */ + if (e.isButtonUpJustPressed()) { + System.out.println("button UP pressed"); + } + if (e.isButtonUpHeld()) { + System.out.println("button UP held"); + } + if (e.isButtonUpJustReleased()) { + System.out.println("button UP released"); + } + + /* button DOWN */ + if (e.isButtonDownJustPressed()) { + System.out.println("button DOWN pressed"); + } + if (e.isButtonDownHeld()) { + System.out.println("button DOWN held"); + } + if (e.isButtonDownJustReleased()) { + System.out.println("button DOWN released"); + } + + /* button MINUS */ + if (e.isButtonMinusJustPressed()) { + System.out.println("button MINUS pressed"); + } + if (e.isButtonMinusHeld()) { + System.out.println("button MINUS held"); + } + if (e.isButtonMinusJustReleased()) { + System.out.println("button MINUS released"); + } + + /* button PLUS */ + if (e.isButtonPlusJustPressed()) { + System.out.println("button PLUS pressed"); + } + if (e.isButtonPlusHeld()) { + System.out.println("button PLUS held"); + } + if (e.isButtonPlusJustReleased()) { + System.out.println("button PLUS released"); + } + + /* button HOME */ + if (e.isButtonHomeJustPressed()) { + System.out.println("button HOME pressed"); + } + if (e.isButtonHomeHeld()) { + System.out.println("button HOME held"); + } + if (e.isButtonHomeJustReleased()) { + System.out.println("button HOME released"); + } + + /* get status */ + if (e.isButtonMinusJustPressed()&&e.isButtonPlusJustPressed()) { + WiiUseApiManager.getInstance().getStatus(1); + } + + /* Activate rumble */ + if (e.isButtonOneJustPressed()) { + System.out.println("Rumble Activated"); + WiiUseApiManager.getInstance().activateRumble(1); + } + if (e.isButtonTwoJustPressed()) { + System.out.println("Rumble Deactivated"); + WiiUseApiManager.getInstance().deactivateRumble(1); + } + + /* Activate IR Tracking */ + if (e.isButtonAJustPressed()) { + System.out.println("IR Activated"); + WiiUseApiManager.getInstance().activateIRTRacking(1); + } + if (e.isButtonBJustPressed()) { + System.out.println("IR Deactivated"); + WiiUseApiManager.getInstance().deactivateIRTRacking(1); + } + + /* Activate Motion sensing */ + if (e.isButtonPlusJustPressed()){ + System.out.println("Motion sensing Activated"); + WiiUseApiManager.getInstance().activateMotionSensing(1); + } + if (e.isButtonMinusJustPressed()){ + System.out.println("Motion sensing Deactivated"); + WiiUseApiManager.getInstance().deactivateMotionSensing(1); + } + + /* display status */ + if (e.getBatteryLevel() != -1) { + System.out + .println("battery level : " + e.getBatteryLevel()); + System.out.println("= --- Leds : " + e.getLeds() + "\n"); + System.out.println("= --- Rumble : " + e.isRumbleActive() + "\n"); + } + + /* display ir points */ + if (e.isIrActive()) { + Point2DInteger[] list = e.getIRPoints(); + for (int i = 0; i < list.length; i++) { + if (list[i] != null) + System.out.print("Point :(" + list[i].getX() + "," + + list[i].getY() + ") "); + } + System.out.println(""); + } + + /* display motion sensing */ + if (e.isMotionSensingActive()){ + System.out.println("Motion Sensing :"+e.getOrientation()+" , "+e.getGforce()); + } + + /* leave test */ + if (e.isButtonHomeJustPressed()) { + System.out.println("LEAVING TEST"); + WiiUseApiManager.getInstance().closeConnection(1); + } + } else { + System.out.println(" WIIMOTE ID : " + e.getWiimoteId() + + " DISCONNECTED !!!!!"); + WiiUseApiManager.getInstance().closeConnection(1); + } + } 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); + } + if (e.isButtonBJustPressed()) { + System.out.println("IR Deactivated"); + WiiUseApiManager.getInstance().deactivateIRTRacking(1); + WiiUseApiManager.getInstance().deactivateMotionSensing(1); + WiiUseApiManager.getInstance().deactivateRumble(1); + } + + /* leave test */ + if (e.isButtonHomeJustPressed()) { + System.out.println("LEAVING TEST"); + WiiUseApiManager.getInstance().closeConnection(1); + if (WiiUseApiManager.getInstance().getNbConnectedWiimotes() == 0) { + WiiUseApiManager.getInstance().shutdown(); + } + } + } else if (dump == MOVE_MOUSE) { + /* Activate IR Tracking */ + if (e.isButtonOneJustPressed()) { + System.out.println("IR Activated"); + WiiUseApiManager.getInstance().activateIRTRacking(1); + } + if (e.isButtonTwoJustPressed()) { + System.out.println("IR Deactivated"); + WiiUseApiManager.getInstance().deactivateIRTRacking(1); + } + + /* button A */ + if (e.isButtonAJustPressed()) { + robot.mousePress(InputEvent.BUTTON1_MASK); + } + if (e.isButtonAJustReleased()) { + robot.mouseRelease(InputEvent.BUTTON1_MASK); + } + + /* button B */ + if (e.isButtonBJustPressed()) { + robot.mousePress(InputEvent.BUTTON2_MASK); + } + if (e.isButtonBJustReleased()) { + robot.mouseRelease(InputEvent.BUTTON2_MASK); + } + + Point2DInteger[] list = e.getIRPoints(); + if (e.isIrActive() && list[0] != null) { + + int x1 = (int) list[0].getX(); + int y1 = (int) list[0].getY(); + + int mousex = (int) Math.round(((double) x1 / 1024.0) * 1280.0); + int mousey = (int) Math.round(((double) y1 / 768.0) * 1024.0); + robot.mouseMove(mousex, mousey); + } + + /* leave test */ + if (e.isButtonHomeJustPressed()) { + System.out.println("LEAVING TEST"); + WiiUseApiManager.getInstance().closeConnection(1); + } + }else if(dump == ORIENT_THRESH_CONT){ + WiiUseApiManager.getInstance().activateMotionSensing(1); + if (e.isButtonOneJustPressed()) { + System.out.println("Continous activated"); + WiiUseApiManager.getInstance().activateContinuous(1); + } + if (e.isButtonTwoJustPressed()) { + System.out.println("Continous deactivated"); + WiiUseApiManager.getInstance().deactivateContinuous(1); + } + if (e.isButtonAJustPressed()) { + System.out.println("Smoothing activated"); + WiiUseApiManager.getInstance().activateSmoothing(1); + } + if (e.isButtonBJustPressed()) { + System.out.println("Smoothing deactivated"); + WiiUseApiManager.getInstance().deactivateSmoothing(1); + } + if (e.isButtonPlusJustPressed()) { + System.out.println("Threshold orientation 10 degrees"); + WiiUseApiManager.getInstance().setOrientationThreshold(1, 10); + } + if (e.isButtonMinusJustPressed()) { + System.out.println("Threshold orientation 0.5 degrees"); + WiiUseApiManager.getInstance().setOrientationThreshold(1, (float)0.5); + } + System.out.println(e); + + + /* leave test */ + if (e.isButtonHomeJustPressed()) { + System.out.println("LEAVING TEST"); + WiiUseApiManager.getInstance().closeConnection(1); + } + }else if(dump == TEST_LEDS){ + WiiUseApiManager.getInstance().activateMotionSensing(1); + if (e.isButtonUpJustPressed()) { + WiiUseApiManager.getInstance().setLeds(1, true, false, false, false); + } + if (e.isButtonDownJustPressed()) { + WiiUseApiManager.getInstance().setLeds(1, false, true, false, false); + } + if (e.isButtonLeftJustPressed()) { + WiiUseApiManager.getInstance().setLeds(1, false, false, true, false); + } + if (e.isButtonRightJustPressed()) { + WiiUseApiManager.getInstance().setLeds(1, false, false, false, true); + } + + /* leave test */ + if (e.isButtonHomeJustPressed()) { + System.out.println("LEAVING TEST"); + WiiUseApiManager.getInstance().closeConnection(1); + } + } + } + + /** + * @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(); +// java.util.Timer timer = new java.util.Timer(); +// timer.scheduleAtFixedRate(new LedsTask(), 0, 100); + + } + +} diff --git a/WiiUseJ/src/wiiusej/GForce.java b/WiiUseJ/src/wiiusej/GForce.java new file mode 100644 index 0000000..3cd513d --- /dev/null +++ b/WiiUseJ/src/wiiusej/GForce.java @@ -0,0 +1,81 @@ +package wiiusej; + +/** + * Represents gravity force on each axis. + * @author gduche + * + */ +public class GForce { + + private float x; + private float y; + private float z; + + /** + * Default constructor; + */ + public GForce(){ + x = 0; + y = 0; + z = 0; + } + + /** + * Constructor with gravity force on each axis. + * @param xx x value + * @param yy x value + * @param zz x value + */ + public GForce(float xx, float yy, float zz){ + x = xx; + y = yy; + z = zz; + } + + /** + * @return the x + */ + public float getX() { + return x; + } + + /** + * @param x the x to set + */ + public void setX(float x) { + this.x = x; + } + + /** + * @return the y + */ + public float getY() { + return y; + } + + /** + * @param y the y to set + */ + public void setY(float y) { + this.y = y; + } + + /** + * @return the z + */ + public float getZ() { + return z; + } + + /** + * @param z the z to set + */ + public void setZ(float z) { + this.z = z; + } + + @Override + public String toString() { + return "Gravity force : ("+x+", "+y+","+z+")"; + } +} diff --git a/WiiUseJ/src/wiiusej/LedsRequest.java b/WiiUseJ/src/wiiusej/LedsRequest.java new file mode 100644 index 0000000..c068ba3 --- /dev/null +++ b/WiiUseJ/src/wiiusej/LedsRequest.java @@ -0,0 +1,109 @@ +package wiiusej; + +/** + * Represents a request to set leds of the wiimote with WiiUse API. + * @author gduche + * + */ +public class LedsRequest extends wiiusej.WiiUseApiRequest { + + private boolean led1, led2, led3, led4; + + /** + * Constructor setting the id of the wiimote concerned. + * + * @param id + * id of the wiimote concerned + * @param type + * type of the request + */ + public LedsRequest(int id, int type) { + super(id, type); + } + + /** + * Constructor setting the id of the wiimote concerned. + * + * @param id + * id of the wiimote concerned + * @param type + * type of the request + * @param l1 + * led1 status. True=ON, False=OFF + * @param l2 + * led2 status. True=ON, False=OFF + * @param l3 + * led3 status. True=ON, False=OFF + * @param l4 + * led4 status. True=ON, False=OFF + */ + public LedsRequest(int id, int type, boolean l1, boolean l2, boolean l3, + boolean l4) { + super(id, type); + led1 = l1; + led2 = l2; + led3 = l3; + led4 = l4; + } + + /** + * @return the led1 + */ + public boolean isLed1() { + return led1; + } + + /** + * @param led1 + * the led1 to set + */ + public void setLed1(boolean led1) { + this.led1 = led1; + } + + /** + * @return the led2 + */ + public boolean isLed2() { + return led2; + } + + /** + * @param led2 + * the led2 to set + */ + public void setLed2(boolean led2) { + this.led2 = led2; + } + + /** + * @return the led3 + */ + public boolean isLed3() { + return led3; + } + + /** + * @param led3 + * the led3 to set + */ + public void setLed3(boolean led3) { + this.led3 = led3; + } + + /** + * @return the led4 + */ + public boolean isLed4() { + return led4; + } + + /** + * @param led4 + * the led4 to set + */ + public void setLed4(boolean led4) { + this.led4 = led4; + } + +} diff --git a/WiiUseJ/src/wiiusej/OrientThresholdRequest.java b/WiiUseJ/src/wiiusej/OrientThresholdRequest.java new file mode 100644 index 0000000..0d3cc14 --- /dev/null +++ b/WiiUseJ/src/wiiusej/OrientThresholdRequest.java @@ -0,0 +1,54 @@ +package wiiusej; + +/** + * Represents a request to set orientation Threshold in Wiiuse API. + * Orientation Threshold is the minimum angle (in degrees) between two events. + * @author gduche + * + */ +public class OrientThresholdRequest extends WiiUseApiRequest { + + private float thresholhd; + + /** + * Constructor setting the id of the wiimote concerned. + * + * @param id + * the id of the wiimote concerned. + */ + public OrientThresholdRequest(int id, int type) { + super(id, type); + } + + /** + * Constructor setting the id of the wiimote concerned. + * + * @param id + * the id of the wiimote concerned. + * @param type + * type of the request + * @param th + * threshold in degrees + */ + public OrientThresholdRequest(int id, int type, float th) { + super(id, type); + thresholhd = th; + } + + /** + * @return the thresholhd + */ + public float getThresholhd() { + return thresholhd; + } + + /** + * @param thresholhd the thresholhd to set + */ + public void setThresholhd(float thresholhd) { + this.thresholhd = thresholhd; + } + + + +} diff --git a/WiiUseJ/src/wiiusej/Orientation.java b/WiiUseJ/src/wiiusej/Orientation.java new file mode 100644 index 0000000..52ebd7c --- /dev/null +++ b/WiiUseJ/src/wiiusej/Orientation.java @@ -0,0 +1,77 @@ +package wiiusej; + +/** + * Class that represents the orientation of the wiimote. + * @author gduche + * + */ +public class Orientation { + + private float roll; + private float pitch; + private float yaw; + + /** + * Default constructor. + */ + public Orientation(){ + roll = 0; + pitch = 0; + yaw = 0; + } + + /** + * Contructor with raw, pitch , yaw. + * @param r raw + * @param p pitch + * @param y yaw + */ + public Orientation(float r, float p, float y){ + roll = r; + pitch = p; + yaw = y; + } + + /** + * @return the roll + */ + public float getRoll() { + return roll; + } + /** + * @param roll the roll to set + */ + public void setRoll(float roll) { + this.roll = roll; + } + /** + * @return the pitch + */ + public float getPitch() { + return pitch; + } + /** + * @param pitch the pitch to set + */ + public void setPitch(float pitch) { + this.pitch = pitch; + } + /** + * @return the yaw + */ + public float getYaw() { + return yaw; + } + /** + * @param yaw the yaw to set + */ + public void setYaw(float yaw) { + this.yaw = yaw; + } + + @Override + public String toString() { + return "Orientation : (roll: "+roll+", pitch: "+pitch+", yaw: "+yaw+")"; + } + +} diff --git a/WiiUseJ/src/wiiusej/Point2DInteger.java b/WiiUseJ/src/wiiusej/Point2DInteger.java new file mode 100644 index 0000000..f2f9181 --- /dev/null +++ b/WiiUseJ/src/wiiusej/Point2DInteger.java @@ -0,0 +1,35 @@ +package wiiusej; + +import java.awt.geom.Point2D; + +public class Point2DInteger extends Point2D { + private int x; + private int y; + + public Point2DInteger(int xx, int yy) { + super(); + setLocation(xx,yy); + } + + @Override + public double getX() { + return x; + } + + @Override + public double getY() { + return y; + } + + @Override + public void setLocation(double xx, double yy) { + this.x = (int)xx; + this.y = (int)yy; + } + + @Override + public String toString() { + return "("+x+","+y+")"; + } + +} diff --git a/WiiUseJ/src/wiiusej/WiiMoteEvent.java b/WiiUseJ/src/wiiusej/WiiMoteEvent.java new file mode 100644 index 0000000..663298d --- /dev/null +++ b/WiiUseJ/src/wiiusej/WiiMoteEvent.java @@ -0,0 +1,706 @@ +package wiiusej; + +import java.util.ArrayList; +import java.util.Iterator; + +/** + * Class that is a bean to be filled by the wiiuse API. + * + * @author gduche + * + */ +public class WiiMoteEvent { + + /* Buttons MACRO */ + private static short WIIMOTE_BUTTON_TWO = 0x0001; + private static short WIIMOTE_BUTTON_ONE = 0x0002; + private static short WIIMOTE_BUTTON_B = 0x0004; + private static short WIIMOTE_BUTTON_A = 0x0008; + private static short WIIMOTE_BUTTON_MINUS = 0x0010; + private static short WIIMOTE_BUTTON_ZACCEL_BIT6 = 0x0020; + private static short WIIMOTE_BUTTON_ZACCEL_BIT7 = 0x0040; + private static short WIIMOTE_BUTTON_HOME = 0x0080; + private static short WIIMOTE_BUTTON_LEFT = 0x0100; + private static short WIIMOTE_BUTTON_RIGHT = 0x0200; + private static short WIIMOTE_BUTTON_DOWN = 0x0400; + private static short WIIMOTE_BUTTON_UP = 0x0800; + private static short WIIMOTE_BUTTON_PLUS = 0x1000; + private static short WIIMOTE_BUTTON_ZACCEL_BIT4 = 0x2000; + private static short WIIMOTE_BUTTON_ZACCEL_BIT5 = 0x4000; + private static int WIIMOTE_BUTTON_UNKNOWN = 0x8000; + private static short WIIMOTE_BUTTON_ALL = 0x1F9F; + + private static short WIIMOTE_LED_1 = 1; + private static short WIIMOTE_LED_2 = 2; + private static short WIIMOTE_LED_3 = 4; + private static short WIIMOTE_LED_4 = 8; + + private static short NB_LEDS = 4; + + /* ID */ + private int wiimoteId = -1; + + /* Status variables */ + private boolean connected = false; + + private float batteryLevel = -1; + + private short leds = 0; + + private boolean isSpeakerEnabled = false; + + private boolean isThereAttachment = false; + + private boolean isRumbleActive = false; + + private float orientationThreshold = 0; + + private boolean isContinuousActive = false; + + private boolean isSmoothingActive = false; + + /* Buttons */ + private short buttonsJustPressed = 0; + private short buttonsJustReleased = 0; + private short buttonsHeld = 0; + + /* IR Tracking */ + private boolean isIrActive = false; + private Point2DInteger[] IRPoints; + + /* Motion Sensing */ + private boolean isMotionSensingActive = false; + private Orientation orientation; + private GForce gforce; + + /** + * Default constructor + */ + public WiiMoteEvent() { + // init IRPoints array + IRPoints = new Point2DInteger[NB_LEDS]; + } + + /** + * Construct the Wiimote setting up the id. + * + * @param id + * the Wiimote id + */ + public WiiMoteEvent(int id) { + this(); + wiimoteId = id; + } + + /** + * Get Wiimote ID + * + * @return the wiimote id. + */ + public int getWiimoteId() { + return wiimoteId; + } + + /** + * Set Wiimote ID + * + * @param wiimoteId + * id of the wiimote + */ + void setWiimoteId(int wiimoteId) { + this.wiimoteId = wiimoteId; + } + + /** + * True if the wiimote is connected false otherwise. + * + * @return return the connected status. + */ + public boolean isConnected() { + return connected; + } + + /** + * Set the connected value to true. + */ + void setConnected() { + this.connected = true; + } + + /** + * Set the connected value to false. + */ + void setDisconnected() { + this.connected = false; + } + + /** + * Get battery level. + * + * @return battery level. 1 = 100% + */ + public float getBatteryLevel() { + return batteryLevel; + } + + /** + * Set level battery. + * + * @param batteryLevel + * must be between 0 and 1 + */ + void setBatteryLevel(float batteryLevel) { + this.batteryLevel = batteryLevel; + } + + /** + * Get status of the leds . + * + * @return a short representing LEDS turned on. + */ + public short getLeds() { + return leds; + } + + /** + * Set Status of the leds. + * + * @param leds + */ + void setLeds(short leds) { + this.leds = leds; + } + + /** + * Tell if the speaker is enable for this wiimote + * + * @return TRUE if it enabled false otherwise + */ + public boolean isSpeakerEnabled() { + return isSpeakerEnabled; + } + + /** + * Set the flag for the speaker to "enabled" + */ + void setSpeakerEnabled() { + this.isSpeakerEnabled = true; + } + + /** + * Set the flag for the speaker to "disabled" + */ + void setSpeakerDisabled() { + this.isSpeakerEnabled = false; + } + + /** + * Tell if there is an attachment to the Wiimote + * + * @return TRUE if it there is one false otherwise + */ + public boolean isThereAttachment() { + return isThereAttachment; + } + + /** + * Set the flag for the attachment to true + */ + void setThereIsAnAttachment() { + this.isThereAttachment = true; + } + + /** + * Set the flag for the attachment to false + */ + void setThereIsNoAttachment() { + this.isThereAttachment = false; + } + + /** + * Get the status of rumble. + * + * @return true if the rumble is active false otherwise + */ + public boolean isRumbleActive() { + return isRumbleActive; + } + + /** + * Set Rumble flag to Active. + */ + void setRumbleActive() { + this.isRumbleActive = true; + } + + /** + * Set Rumble flag to Inactive. + */ + void setRumbleInactive() { + this.isRumbleActive = false; + } + + /** + * Get orientation threshold. + * + * @return the orientationThreshold + */ + public float getOrientationThreshold() { + return orientationThreshold; + } + + /** + * Set the orientation threshold. + * + * @param orientationThreshold + * the orientationThreshold to set + */ + void setOrientationThreshold(float orientationThreshold) { + this.orientationThreshold = orientationThreshold; + } + + /** + * Tell if the CONTINUOUS option is activated. + * + * @return the isContinuousActive + */ + public boolean isContinuousActive() { + return isContinuousActive; + } + + /** + * Set the CONTINUOUS option active. + */ + void setContinuousActive() { + this.isContinuousActive = true; + } + + /** + * Set the CONTINUOUS option inactive. + */ + void setContinuousInactive() { + this.isContinuousActive = false; + } + + /** + * Tell if the option SMOOTHING is activated. + * + * @return the isSmoothingActive + */ + public boolean isSmoothingActive() { + return isSmoothingActive; + } + + /** + * Set SMOOTHING option to active. + */ + void setSmoothingActive() { + this.isSmoothingActive = true; + } + + /** + * Set SMOOTHING option to inactive. + */ + void setSmoothingInactive() { + this.isSmoothingActive = false; + } + + /** + * Get the short storing the buttons just pressed + * + * @return the short storing the buttons just pressed + */ + public short getButtonsJustPressed() { + return buttonsJustPressed; + } + + /** + * set the short storing the buttons just pressed + * + * @param buttonsJustPressed + */ + void setButtonsJustPressed(short buttonsJustPressed) { + this.buttonsJustPressed = buttonsJustPressed; + } + + /** + * Get the short storing the buttons just released + * + * @return the short storing the buttons just released + */ + public short getButtonsJustReleased() { + return buttonsJustReleased; + } + + /** + * set the short storing the buttons just released + * + * @param buttonsJustReleased + */ + void setButtonsJustReleased(short buttonsJustReleased) { + this.buttonsJustReleased = buttonsJustReleased; + } + + /** + * get the short storing the buttons held + * + * @return the short storing the buttons held + */ + public short getButtonsHeld() { + return buttonsHeld; + } + + /** + * set the short storing the buttons held + * + * @param buttonsHeld + */ + void setButtonsHeld(short buttonsHeld) { + this.buttonsHeld = buttonsHeld; + } + + /** + * Tell if the IR Tracking is active. + * + * @return TRUE if it is active or false otherwise. + */ + public boolean isIrActive() { + return isIrActive; + } + + /** + * Set the value isIrActive to true + */ + void setIrActive() { + this.isIrActive = true; + } + + /** + * Set the value isIrActive to true + */ + void setIrInactive() { + this.isIrActive = false; + } + + /** + * Get list of IR points. + * + * @return the list of 2D points + */ + public Point2DInteger[] getIRPoints() { + return IRPoints; + } + + /** + * Add IR Point in the list (Max 4 points) + * + * @param x + * x value + * @param y + * y value + */ + void addIRpoint(int x, int y) { + for (int i = 0; i < IRPoints.length; i++) { + if (IRPoints[i] == null) { + IRPoints[i] = new Point2DInteger(x, y); + return; + } + } + return; + } + + /** + * Clear IR points. + */ + void EmptyIRPoints() { + for (int i = 0; i < IRPoints.length; i++) { + IRPoints[i] = null; + } + } + + /** + * Get the flag indicating if the motion sensing is active. + * + * @return true if the motion sensing is active false otherwise + */ + public boolean isMotionSensingActive() { + return isMotionSensingActive; + } + + /** + * Set the motion sensing flag to active. + */ + void setMotionSensingActive() { + this.isMotionSensingActive = true; + } + + /** + * Set the motion sensing flag to inactive. + */ + void setMotionSensingInactive() { + this.isMotionSensingActive = false; + } + + /** + * @return the orientation + */ + public Orientation getOrientation() { + return orientation; + } + + /** + * Set orientation of the wiimote. + * + * @param r + * roll + * @param p + * pitch + * @param y + * yaw + */ + void setOrientation(float r, float p, float y) { + this.orientation = new Orientation(r, p, y); + } + + /** + * Get the gravity force. + * + * @return the gforce + */ + public GForce getGforce() { + return gforce; + } + + /** + * Set the gravity force. + * + * @param x + * gravity force on x axis + * @param y + * gravity force on y axis + * @param z + * gravity force on z axis + */ + void setGforce(float x, float y, float z) { + this.gforce = new GForce(x, y, z); + } + + /** **************** BUTTONS Methods ***************** */ + /* generic button functions */ + + private boolean buttonTest(short buttonBitsDefinition, short buttons) { + return (buttons & buttonBitsDefinition) == buttonBitsDefinition; + } + + private boolean isButtonJustPressed(short buttonBitsDefinition) { + return buttonTest(buttonBitsDefinition, buttonsJustPressed) + && !isButtonHeld(buttonBitsDefinition); + } + + private boolean isButtonJustReleased(short buttonBitsDefinition) { + return buttonTest(buttonBitsDefinition, buttonsJustReleased); + } + + private boolean isButtonHeld(short buttonBitsDefinition) { + return buttonTest(buttonBitsDefinition, buttonsHeld); + } + + /* Button ONE */ + + public boolean isButtonOneJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_ONE); + } + + public boolean isButtonOneJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_ONE); + } + + public boolean isButtonOneHeld() { + return isButtonHeld(WIIMOTE_BUTTON_ONE); + } + + /* Button TWO */ + + public boolean isButtonTwoJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_TWO); + } + + public boolean isButtonTwoJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_TWO); + } + + public boolean isButtonTwoHeld() { + return isButtonHeld(WIIMOTE_BUTTON_TWO); + } + + /* Button A */ + + public boolean isButtonAJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_A); + } + + public boolean isButtonAJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_A); + } + + public boolean isButtonAHeld() { + return isButtonHeld(WIIMOTE_BUTTON_A); + } + + /* Button B */ + + public boolean isButtonBJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_B); + } + + public boolean isButtonBJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_B); + } + + public boolean isButtonBHeld() { + return isButtonHeld(WIIMOTE_BUTTON_B); + } + + /* Button LEFT */ + + public boolean isButtonLeftJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_LEFT); + } + + public boolean isButtonLeftJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_LEFT); + } + + public boolean isButtonLeftHeld() { + return isButtonHeld(WIIMOTE_BUTTON_LEFT); + } + + /* Button RIGHT */ + + public boolean isButtonRightJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_RIGHT); + } + + public boolean isButtonRightJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_RIGHT); + } + + public boolean isButtonRightHeld() { + return isButtonHeld(WIIMOTE_BUTTON_RIGHT); + } + + /* Button UP */ + + public boolean isButtonUpJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_UP); + } + + public boolean isButtonUpJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_UP); + } + + public boolean isButtonUpHeld() { + return isButtonHeld(WIIMOTE_BUTTON_UP); + } + + /* Button DOWN */ + + public boolean isButtonDownJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_DOWN); + } + + public boolean isButtonDownJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_DOWN); + } + + public boolean isButtonDownHeld() { + return isButtonHeld(WIIMOTE_BUTTON_DOWN); + } + + /* Button - */ + + public boolean isButtonMinusJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_MINUS); + } + + public boolean isButtonMinusJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_MINUS); + } + + public boolean isButtonMinusHeld() { + return isButtonHeld(WIIMOTE_BUTTON_MINUS); + } + + /* Button + */ + + public boolean isButtonPlusJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_PLUS); + } + + public boolean isButtonPlusJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_PLUS); + } + + public boolean isButtonPlusHeld() { + return isButtonHeld(WIIMOTE_BUTTON_PLUS); + } + + /* Button HOME */ + + public boolean isButtonHomeJustPressed() { + return isButtonJustPressed(WIIMOTE_BUTTON_HOME); + } + + public boolean isButtonHomeJustReleased() { + return isButtonJustReleased(WIIMOTE_BUTTON_HOME); + } + + public boolean isButtonHomeHeld() { + return isButtonHeld(WIIMOTE_BUTTON_HOME); + } + + @Override + public String toString() { + super.toString(); + String out = ""; + /* Status */ + out += "/*********** WIIMOTE ID :" + wiimoteId + " ********/\n"; + out += "--- connected : " + connected + "\n"; + out += "--- Battery level : " + batteryLevel + "\n"; + out += "= --- Leds : " + leds + "\n"; + out += "--- Speaker enabled : " + isSpeakerEnabled + "\n"; + out += "--- Attachment ? : " + isThereAttachment + "\n"; + out += "--- Rumble ? : " + isRumbleActive + "\n"; + out += "--- Orientation threshold value ? : " + orientationThreshold + + "\n"; + out += "--- Continuous ? : " + isContinuousActive + "\n"; + out += "--- Smoothing ? : " + isSmoothingActive + "\n"; + /* Display buttons */ + out += "/******** Buttons ********/\n"; + out += "--- Buttons just pressed : " + buttonsJustPressed + "\n"; + out += "--- Buttons just released : " + buttonsJustReleased + "\n"; + out += "--- Buttons held : " + buttonsHeld + "\n"; + + /* Display IR Tracking */ + out += "/******** IR Tracking ********/\n"; + out += "--- Active : " + isIrActive + "\n"; + if (isIrActive) { + out += "--- Seen points\n"; + for (int i = 0; i < IRPoints.length; i++) { + if (IRPoints[i] != null) { + out += IRPoints[i].toString(); + } + } + } + + /* Motion sensing */ + out += "/******** Motion sensing ********/\n"; + out += "--- Motion sensing : " + isMotionSensingActive + "\n"; + if (isMotionSensingActive) { + out += "--- " + orientation + "\n"; + out += "--- " + gforce + "\n"; + } + return out; + } + +} diff --git a/WiiUseJ/src/wiiusej/WiiUseApi.java b/WiiUseJ/src/wiiusej/WiiUseApi.java new file mode 100644 index 0000000..091f269 --- /dev/null +++ b/WiiUseJ/src/wiiusej/WiiUseApi.java @@ -0,0 +1,174 @@ +package wiiusej; + +/** + * Singleton used to manipulate WiiUse Api. + * @author gduche + * + */ +public class WiiUseApi { + + static { + System.loadLibrary("libWiiuseJ"); + } + + private static WiiUseApi instance = new WiiUseApi(); + + /** + * Get the only instance of WiiUseApi. + * @return + */ + static WiiUseApi getInstance(){ + return instance; + } + + /** + * Load the library. + * + * @return 0 if there is an error, 1 if everything is ok. + */ + native int loadLibrary(); + + /** + * Try to connect to 2 wiimotes. Make them rumble to show they are + * connected. + * + * @return 0 if there is an error otherwise it returns the number of + * wiimotes connected. + */ + native int doConnections(); + + /** + * Close connection to the wiimote with the given id. + * + */ + native void closeConnection(int id); + + /** + * Shutdown Wiiuse API. + * @return 0 if there is an error, 1 if everything is ok. + */ + native void shutdownApi(); + + /** + * Activate rumble on the wiimote with the given id. + * @param id the id of the wiimote. + */ + native void activateRumble(int id); + + /** + * Deactivate rumble on the wiimote with the given id. + * + * @param id the id of the wiimote. + */ + native void deactivateRumble(int id); + + /** + * Activate IR Tracking on the wiimote with the given id. + * @param id the id of the wiimote. + */ + native void activateIRTracking(int id); + + /** + * Deactivate IR Tracking on the wiimote with the given id. + * @param id the id of the wiimote. + */ + native void deactivateIRTracking(int id); + + /** + * Activate motion sensing on the wiimote with the given id. + * @param id the id of the wiimote. + */ + native void activateMotionSensing(int id); + + /** + * Deactivate motion sensing on the wiimote with the given id. + * @param id the id of the wiimote. + */ + native void deactivateMotionSensing(int id); + + /** + * Set wiimote leds status. + * @param id the id of the wiimote concerned + * @param led1 status of led1: True=ON, False=OFF + * @param led2 status of led2: True=ON, False=OFF + * @param led3 status of led3: True=ON, False=OFF + * @param led4 status of led4: True=ON, False=OFF + */ + native void setLeds(int id, boolean led1, boolean led2, boolean led3, boolean led4); + + /** + * Set how many degrees an angle must change to generate an event. + * @param id id of the wiimote concerned + * @param angle minimum angle detected by an event + */ + native void setOrientThreshold(int id, float angle); + + /** + * Make the the accelerometers give smoother results. + * This is set by default. + * @param id the id of the wiimote concerned + */ + native void activateSmoothing(int id); + + /** + * Make the the accelerometers give raw results. + * @param id the id of the wiimote concerned + */ + native void deactivateSmoothing(int id); + + /** + * Make the wiimote generate an event each time we poll. + * Not set by default. + * @param id the id of the wiimote concerned + */ + native void activateContinuous(int id); + + /** + * Make the wiimote generate an event only when there is one. + * @param id the id of the wiimote concerned + */ + native void deactivateContinuous(int id); + + /** + * Get status and values from the wiimotes and send it through callbacks. + * + * @param id the id of the wiimote of which we want the status. + */ + native void getStatus(int id); + + /** + * Get status and values from the wiimotes and send it through callbacks. + * + * @param mote The WiimoteEvent object to fill with the datas. + */ + native void specialPoll(WiiMoteEvent mote); + + + /* Tests */ + public static void main(String[] args) { + + /* Test JNI Side */ + WiiUseApi manager = new WiiUseApi(); + + int value = manager.loadLibrary(); + System.out.println("loadLibrary : " + value); + + value = manager.doConnections(); + System.out.println("doConnections : " + value); + + WiiMoteEvent mote = new WiiMoteEvent(); + + manager.getStatus(1); + System.out.println("Status : \n" + mote); + + System.out.println(""); + System.out.println("!!!!!!!!!!!!!! Polling !!!!!!!!!"); + while (true) { + manager.specialPoll(mote); + System.out.println(mote); + mote.EmptyIRPoints(); + } + // manager.closeConnectionsAndShutDown(); + } + +} diff --git a/WiiUseJ/src/wiiusej/WiiUseApiListener.java b/WiiUseJ/src/wiiusej/WiiUseApiListener.java new file mode 100644 index 0000000..81f3792 --- /dev/null +++ b/WiiUseJ/src/wiiusej/WiiUseApiListener.java @@ -0,0 +1,9 @@ +package wiiusej; + + + +public interface WiiUseApiListener extends java.util.EventListener { + + void wiimoteEvent(WiiMoteEvent e); + +} diff --git a/WiiUseJ/src/wiiusej/WiiUseApiManager.java b/WiiUseJ/src/wiiusej/WiiUseApiManager.java new file mode 100644 index 0000000..178e0c4 --- /dev/null +++ b/WiiUseJ/src/wiiusej/WiiUseApiManager.java @@ -0,0 +1,374 @@ +package wiiusej; + +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.swing.event.EventListenerList; + +/** + * Class that manage the use of Wiiuse API. + * + * @author gduche + * + */ +public class WiiUseApiManager extends Thread { + + private static WiiUseApiManager instance = new WiiUseApiManager(); + + private final EventListenerList listeners = new EventListenerList(); + + private WiiUseApi wiiuse = WiiUseApi.getInstance(); + + private boolean loaded = false; + + private int connected = 0; + + private AtomicBoolean running = new AtomicBoolean(false); + + private ConcurrentLinkedQueue requests = new ConcurrentLinkedQueue(); + + public static WiiUseApiManager getInstance() { + return instance; + } + + /** + * Load the wiimote library. + * + * @return false if the library is not loaded true otherwise. + */ + public boolean loadLibrary() { + if (!loaded) {// not yet loaded try to load it + int load = wiiuse.loadLibrary(); + if (load > 0) { + loaded = true; + return true; + } else { + loaded = false; + System.out.println("Error loading the Wiimote library !!!"); + return false; + } + } + // already loaded + return loaded; + } + + /** + * Connect wiimote and get the nu ber of wiimotes connected. Supposed to be + * used once. + * + * @return 0 if nothing connected or the number of wiimotes connected. + */ + public int connectWiimotes() { + if (loaded) { + connected = wiiuse.doConnections(); + System.out.println(connected + " wiimote(s) connected !!!"); + return connected; + } else {// library not loaded, no wiimotes connected + return 0; + } + } + + /** + * Ask the thread to close a connection. + * + * @param id + * id of the wiimote to disconnect. + */ + public void closeConnection(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_CLOSE_CONNECTION_REQUEST)); + } + + /** + * Get the number of wiimotes connected. + * + * @return the number of wiimotes connected. + */ + public int getNbConnectedWiimotes() { + return connected; + } + + /** + * Stop thread and shutdown wiiuse Api. + */ + public void shutdown() { + running.set(false); + wiiuse.shutdownApi(); + } + + /** + * Activate the rumble for the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void activateRumble(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_ACTIVATE_RUMBLE_REQUEST)); + } + + /** + * Deactivate the rumble for the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void deactivateRumble(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_DEACTIVATE_RUMBLE_REQUEST)); + } + + /** + * Activate IR Tracking for the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void activateIRTRacking(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_ACTIVATE_IR_TRACKING_REQUEST)); + } + + /** + * Deactivate IR Tracking for the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void deactivateIRTRacking(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_DEACTIVATE_IR_TRACKING_REQUEST)); + } + + /** + * Activate IR Tracking of the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void activateMotionSensing(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_ACTIVATE_MOTION_SENSING_REQUEST)); + } + + /** + * Deactivate IR Tracking of the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void deactivateMotionSensing(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_DEACTIVATE_MOTION_SENSING_REQUEST)); + } + + /** + * Activate smoothing the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void activateSmoothing(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_ACTIVATE_SMOOTHING_REQUEST)); + } + + /** + * Deactivate smoothing the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void deactivateSmoothing(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_DEACTIVATE_SMOOTHING_REQUEST)); + } + + /** + * Activate continuous for the wiimotes with the given id. + * + * @param id + * id of the wiimote. + */ + public void activateContinuous(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_ACTIVATE_CONTINUOUS_REQUEST)); + } + + /** + * Deactivate continuous for the wiimotes with the given id. + * + * @param id + * id of the wiimote + */ + public void deactivateContinuous(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_DEACTIVATE_CONTINUOUS_REQUEST)); + } + + /** + * Set leds for the wiimotes with the given id. + * + * @param id + * id of the wiimote + * @param l1 + * status of led1. True : ON, False : OFF + * @param l2 + * status of led2. True : ON, False : OFF + * @param l3 + * status of led3. True : ON, False : OFF + * @param l4 + * status of led4. True : ON, False : OFF + */ + public void setLeds(int id, boolean l1, boolean l2, boolean l3, boolean l4) { + requests.add(new LedsRequest(id, WiiUseApiRequest.WIIUSE_LEDS_REQUEST, + l1, l2, l3, l4)); + } + + /** + * Set the orientation threshold for the given id. + * + * @param id + * id of the wiimote + * @param th + * threshold in degrees + */ + public void setOrientationThreshold(int id, float th) { + requests.add(new OrientThresholdRequest(id, + WiiUseApiRequest.WIIUSE_ORIENT_THRESHOLHD_REQUEST, th)); + } + + /** + * Get Status for the wiimote for the given id. + * + * @param id + * id of the wiimote + */ + public void getStatus(int id) { + requests.add(new WiiUseApiRequest(id, + WiiUseApiRequest.WIIUSE_STATUS_REQUEST)); + } + + @Override + public void run() { + + if (loaded && (connected > 0)) { + running.set(true); + + WiiMoteEvent evt = new WiiMoteEvent(); + + // Start polling and tell the observers when there Wiimote events + 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 */ + wiiuse.closeConnection(id); + connected--; + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_STATUS_REQUEST) { + /* Status requests */ + wiiuse.getStatus(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_ACTIVATE_RUMBLE_REQUEST) { + /* Activate Rumble requests */ + wiiuse.activateRumble(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_DEACTIVATE_RUMBLE_REQUEST) { + /* Deactivate Rumble requests */ + wiiuse.deactivateRumble(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_ACTIVATE_IR_TRACKING_REQUEST) { + /* Activate IR Tracking requests */ + wiiuse.activateIRTracking(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_DEACTIVATE_IR_TRACKING_REQUEST) { + /* Deactivate IR Tracking requests */ + wiiuse.deactivateIRTracking(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_ACTIVATE_MOTION_SENSING_REQUEST) { + /* Activate Motion sensing requests */ + wiiuse.activateMotionSensing(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_DEACTIVATE_MOTION_SENSING_REQUEST) { + /* Deactivate Motion sensing requests */ + wiiuse.deactivateMotionSensing(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_LEDS_REQUEST) { + /* leds requests */ + LedsRequest reqLed = (LedsRequest) req; + wiiuse.setLeds(id, reqLed.isLed1(), reqLed.isLed2(), + reqLed.isLed3(), reqLed.isLed4()); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_ACTIVATE_SMOOTHING_REQUEST) { + /* Activate smoothing requests */ + wiiuse.activateSmoothing(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_DEACTIVATE_SMOOTHING_REQUEST) { + /* Deactivate smoothing requests */ + wiiuse.deactivateSmoothing(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_ACTIVATE_CONTINUOUS_REQUEST) { + /* Activate continuous requests */ + wiiuse.activateContinuous(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_DEACTIVATE_CONTINUOUS_REQUEST) { + /* Deactivate continuous requests */ + wiiuse.deactivateContinuous(id); + } else if (req.getRequestType() == WiiUseApiRequest.WIIUSE_ORIENT_THRESHOLHD_REQUEST) { + /* set orientation request */ + wiiuse.setOrientThreshold(req.getId(), + ((OrientThresholdRequest) req).getThresholhd()); + } else { + System.out.println("Bad request to Wiiuse API !!!!!"); + } + } + + /* Polling */ + wiiuse.specialPoll(evt); + + if (evt.getWiimoteId() != -1) {// there is an event notify + // observers + notifyWiiUseApiListener(evt); + } + 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 !!!"); + } + + } + + /** + * + * @param listener + */ + public void addWiiUseApiListener(WiiUseApiListener listener) { + listeners.add(WiiUseApiListener.class, listener); + } + + /** + * + * @param listener + */ + public void removeWiiUseApiListener(WiiUseApiListener listener) { + listeners.remove(WiiUseApiListener.class, listener); + } + + /** + * + * @return + */ + public WiiUseApiListener[] getWiiUseApiListeners() { + return listeners.getListeners(WiiUseApiListener.class); + } + + /** + * + * @param evt + */ + public void notifyWiiUseApiListener(WiiMoteEvent evt) { + for (WiiUseApiListener listener : getWiiUseApiListeners()) { + listener.wiimoteEvent(evt); + } + } + +} diff --git a/WiiUseJ/src/wiiusej/WiiUseApiRequest.java b/WiiUseJ/src/wiiusej/WiiUseApiRequest.java new file mode 100644 index 0000000..56998c7 --- /dev/null +++ b/WiiUseJ/src/wiiusej/WiiUseApiRequest.java @@ -0,0 +1,82 @@ +package wiiusej; + +/** + * Represents a request we could do to the WiiUse API. + * @author gduche + * + */ +public class WiiUseApiRequest { + + public static int WIIUSE_STATUS_REQUEST=1; + public static int WIIUSE_ACTIVATE_SMOOTHING_REQUEST=2; + public static int WIIUSE_DEACTIVATE_SMOOTHING_REQUEST=-2; + public static int WIIUSE_ACTIVATE_IR_TRACKING_REQUEST=3; + public static int WIIUSE_DEACTIVATE_IR_TRACKING_REQUEST=-3; + public static int WIIUSE_ACTIVATE_MOTION_SENSING_REQUEST=4; + public static int WIIUSE_DEACTIVATE_MOTION_SENSING_REQUEST=-4; + public static int WIIUSE_CLOSE_CONNECTION_REQUEST=5; + public static int WIIUSE_ACTIVATE_CONTINUOUS_REQUEST=6; + public static int WIIUSE_DEACTIVATE_CONTINUOUS_REQUEST=-6; + public static int WIIUSE_ACTIVATE_RUMBLE_REQUEST=7; + public static int WIIUSE_DEACTIVATE_RUMBLE_REQUEST=-7; + public static int WIIUSE_LEDS_REQUEST=8; + public static int WIIUSE_ORIENT_THRESHOLHD_REQUEST=9; + + + private int wiimoteId=0; + private int requestType=0; + + /** + * Constructor setting the id of the wiimote concerned. + * @param id the id of the wiimote concerned. + */ + public WiiUseApiRequest(int id){ + wiimoteId = id; + } + + /** + * Constructor setting the id of the wiimote concerned. + * @param id the id of the wiimote concerned. + * + */ + public WiiUseApiRequest(int id, int type){ + wiimoteId = id; + requestType = type; + } + + /** + * Get id of the wiimote concerned by this request. + * @return id of the wiimote concerned + */ + public int getId(){ + return wiimoteId; + } + + /** + * Set id of the wiimote concerned by this request. + * @param id id fh the wiimote concernet + */ + public void setId(int id){ + wiimoteId = id; + } + + /** + * Get the request type. + * @return the requestType + */ + public int getRequestType() { + return requestType; + } + + /** + * Set the request type. + * @param requestType the requestType to set + */ + public void setRequestType(int requestType) { + this.requestType = requestType; + } + + + +} +