diff --git a/java/src/pm/Action.java b/java/src/pm/Action.java index 41f298f..730042e 100644 --- a/java/src/pm/Action.java +++ b/java/src/pm/Action.java @@ -14,5 +14,6 @@ public enum Action { START, TEST, VOLUME_DOWN, - VOLUME_UP; + VOLUME_UP, + TRAIN, STOP, SAVE, RECOGNIZE, LOAD; } diff --git a/java/src/pm/Device.java b/java/src/pm/Device.java index 53c152a..36f0cc5 100644 --- a/java/src/pm/Device.java +++ b/java/src/pm/Device.java @@ -23,14 +23,28 @@ public abstract class Device { sequenceListener.add(sequence, task); } - protected void add(Press press, Task task) { - add(new Macro(press), task); - } - protected void add(Event event, Task task) { add(new Sequence(event), task); } + protected void add(Press press, Task task, boolean macro) { + if (macro) { + add(new Macro(press), task); + } else { + add((Event) press, task); + } + } + + protected void add(Press press, Task task) { + add(press, task, true); + } + + protected void add(Hold hold, Task pressTask, Task releaseTask) { + Button button = hold.getButton(); + add(new Press(button), pressTask, false); + add(new Release(button), releaseTask); + } + protected void add(Sequence startSequence, Sequence stopSequence, Continuous continuous) { add(startSequence, continuous); add(stopSequence, new Stopper(continuous)); @@ -59,4 +73,5 @@ public abstract class Device { /* Device default methods */ public void initialise() throws DeviceInitialiseException {} public void exit() throws DeviceExitException {} + public void action(Action action) {} } diff --git a/java/src/pm/Main.java b/java/src/pm/Main.java index ae77a3a..6287f8d 100644 --- a/java/src/pm/Main.java +++ b/java/src/pm/Main.java @@ -39,7 +39,11 @@ public class Main extends TaskListener { add(new WiimoteDevice()); //add(new GUIDevice()); for (Device device : deviceList) { - device.initialise(); + try { + device.initialise(); + } catch (DeviceInitialiseException e) { + e.printStackTrace(); + } } add(new ExampleApplication()); @@ -52,17 +56,22 @@ public class Main extends TaskListener { } public void exit() { - try { - System.out.println("Exit devices..."); - for (Device device : deviceList) { - device.exit(); + System.out.println("Exit devices..."); + for (Device device : deviceList) { + try { + device.exit(); + } catch (DeviceExitException e) { + e.printStackTrace(); } - System.out.println("Exit applications..."); - for (Application application : applicationCycle) { + } + System.out.println("Exit applications..."); + for (Application application : applicationCycle) { + try { application.exit(); + } catch (ApplicationExitException e) { + e.printStackTrace(); } - } catch (DeviceExitException e) { - } catch (ApplicationExitException e) {} + } System.out.println("Exit main..."); stop(); } @@ -86,13 +95,23 @@ public class Main extends TaskListener { Action action = task.getAction(); Target target = task.getTarget(); System.out.println("Action: " + action + " Target: " + target); - switch (target) { - case MAIN: - action(action); - break; - case APPLICATION: - applicationCycle.current().add(task); - break; + try { + switch (target) { + case MAIN: + action(action); + break; + case DEVICE: + for (Device device : deviceList) { + device.action(action); + } + break; + case APPLICATION: + applicationCycle.current().add(task); + break; + } + } catch (Exception e) { + System.out.println("Action exception:"); + e.printStackTrace(); } } diff --git a/java/src/pm/device/wiimote/GestureService.java b/java/src/pm/device/wiimote/GestureService.java deleted file mode 100644 index 5628e49..0000000 --- a/java/src/pm/device/wiimote/GestureService.java +++ /dev/null @@ -1,10 +0,0 @@ -package pm.device.wiimote; - -import org.wiigee.event.GestureEvent; -import org.wiigee.event.GestureListener; - -public class GestureService implements GestureListener { - public void gestureReceived(GestureEvent event) { - System.out.println(event); - } -} diff --git a/java/src/pm/device/wiimote/WiimoteDevice.java b/java/src/pm/device/wiimote/WiimoteDevice.java index 2e46544..4e1590e 100644 --- a/java/src/pm/device/wiimote/WiimoteDevice.java +++ b/java/src/pm/device/wiimote/WiimoteDevice.java @@ -1,43 +1,97 @@ package pm.device.wiimote; +import org.wiigee.event.GestureEvent; +import org.wiigee.event.GestureListener; +import org.wiigee.util.Log; + import pm.Action; import pm.Button; import pm.Device; import pm.Target; import pm.Task; +import pm.device.wiimote.gesture.GestureDevice; import pm.exception.device.DeviceInitialiseException; import pm.exception.event.UnknownButtonException; +import pm.macro.event.Hold; import pm.macro.event.Press; import pm.macro.event.Release; + import wiiusej.Wiimote; +import wiiusej.values.RawAcceleration; import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent; import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent; -public class WiimoteDevice extends Device { +public class WiimoteDevice extends Device implements GestureListener { protected static final int CONNECT_MAX = 10; protected static WiimoteService wiimoteService; - protected static GestureService gestureService; protected Wiimote wiimote; + protected GestureDevice gestureDevice; - { + protected int gestureId = 0; + + static { WiimoteDevice.wiimoteService = new WiimoteService(); - WiimoteDevice.gestureService = new GestureService(); + Log.setLevel(0); + } + + public WiimoteDevice() { + gestureDevice = new GestureDevice(); + gestureDevice.add(this); } public void initialise() throws DeviceInitialiseException { wiimote = wiimoteService.getDevice(this); wiimote.activateMotionSensing(); add( - new Press(WiimoteButton.A), - new Task(Action.TEST, Target.APPLICATION)); + new Hold(WiimoteButton.A), + new Task(Action.TRAIN, Target.DEVICE), + new Task(Action.STOP, Target.DEVICE)); + add( + new Press(WiimoteButton.B), + new Task(Action.SAVE, Target.DEVICE)); + add( + new Press(WiimoteButton.DOWN), + new Task(Action.LOAD, Target.DEVICE)); + add( + new Hold(WiimoteButton.HOME), + new Task(Action.RECOGNIZE, Target.DEVICE), + new Task(Action.STOP, Target.DEVICE)); } public void exit() { + wiimote.deactivateMotionSensing(); wiimoteService.exit(); } + public void action(Action action) { + switch (action) { + case TRAIN: + System.out.println("Wiimote Train"); + gestureDevice.train(); + break; + case STOP: + System.out.println("Wiimote Stop"); + gestureDevice.stop(); + break; + case SAVE: + System.out.println("Wiimote Save"); + gestureDevice.close(); + gestureDevice.saveGesture(gestureId, "C:\\gesture-" + gestureId); + ++gestureId; + break; + case LOAD: + for (int i = 0; i < gestureId; ++i) { + gestureDevice.loadGesture("C:\\gesture-" + i); + } + break; + case RECOGNIZE: + gestureDevice.recognize(); + break; + } + } + public void onButtonsEvent(WiimoteButtonsEvent event) { int pressed = event.getButtonsJustPressed() - event.getButtonsHeld(); int released = event.getButtonsJustReleased(); @@ -55,6 +109,18 @@ public class WiimoteDevice extends Device { } public void onMotionSensingEvent(MotionSensingEvent event) { - //System.out.println(event.getRawAcceleration()); + RawAcceleration rawAcceleration = event.getRawAcceleration(); + System.out.println(event); + double[] vector = new double[] { + rawAcceleration.getX(), + rawAcceleration.getY(), + rawAcceleration.getZ()}; + gestureDevice.add(vector); + } + + public void gestureReceived(GestureEvent event) { + if (event.isValid()) { + System.out.printf("id #%d, prob %.0f%%, valid %b\n", event.getId(), 100 * event.getProbability(), event.isValid()); + } } } diff --git a/java/src/pm/device/wiimote/WiimoteService.java b/java/src/pm/device/wiimote/WiimoteService.java index d71eb58..1cf2825 100644 --- a/java/src/pm/device/wiimote/WiimoteService.java +++ b/java/src/pm/device/wiimote/WiimoteService.java @@ -22,7 +22,7 @@ import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent; import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent; public class WiimoteService extends WiiUseApiManager implements WiimoteListener { - protected final boolean RUMBLE = false; + protected final boolean RUMBLE = true; protected ArrayList wiimoteList; protected Wiimote[] wiimoteArray; diff --git a/java/src/pm/device/wiimote/gesture/GestureDevice.java b/java/src/pm/device/wiimote/gesture/GestureDevice.java new file mode 100644 index 0000000..c6e322e --- /dev/null +++ b/java/src/pm/device/wiimote/gesture/GestureDevice.java @@ -0,0 +1,70 @@ +package pm.device.wiimote.gesture; + +import org.wiigee.device.Device; +import org.wiigee.event.ButtonListener; +import org.wiigee.event.ButtonPressedEvent; +import org.wiigee.event.GestureListener; +import pm.device.wiimote.gesture.event.Close; +import pm.device.wiimote.gesture.event.Recognize; +import pm.device.wiimote.gesture.event.Train; + +public class GestureDevice extends Device /*implements AccelerationListener */{ + public static final boolean AUTOFILTERING = true; + //public static final boolean AUTOMOTION = true; + + public GestureDevice(boolean autofiltering/*, boolean automotion*/) { + super(autofiltering); + /*if (automotion) { + addAccelerationListener(this); + }*/ + } + + public GestureDevice() { + this(AUTOFILTERING/*, AUTOMOTION*/); + } + + public void add(GestureListener gestureListener) { + addGestureListener(gestureListener); + } + + public void add(double[] vector) { + fireAccelerationEvent(vector); + } + + public void recognize() { + fireButtonPressedEvent(new Recognize(this)); + } + + public void train() { + fireButtonPressedEvent(new Train(this)); + } + + public void close() { + fireButtonPressedEvent(new Close(this)); + } + + public void stop() { + fireButtonReleasedEvent(0); + } + + public void fireButtonPressedEvent(ButtonPressedEvent buttonPressedEvent) { + for (ButtonListener bttnLstnr : buttonlistener) { + bttnLstnr.buttonPressReceived(buttonPressedEvent); + } + if (buttonPressedEvent.isRecognitionInitEvent() || buttonPressedEvent.isTrainInitEvent()) { + this.resetAccelerationFilters(); + } + } + + /*public void accelerationReceived(AccelerationEvent event) {} + + public void motionStartReceived(MotionStartEvent event) { + //System.out.println("Motion start!"); + recognize(); + } + + public void motionStopReceived(MotionStopEvent event) { + //System.out.println("Motion stop!"); + stop(); + }*/ +} diff --git a/java/src/pm/device/wiimote/gesture/event/Close.java b/java/src/pm/device/wiimote/gesture/event/Close.java new file mode 100644 index 0000000..56ecf0c --- /dev/null +++ b/java/src/pm/device/wiimote/gesture/event/Close.java @@ -0,0 +1,24 @@ +package pm.device.wiimote.gesture.event; + +import org.wiigee.device.Device; +import org.wiigee.event.ButtonPressedEvent; + +public class Close extends ButtonPressedEvent { + protected static final long serialVersionUID = 1L; + + public Close(Device device) { + super(device, 0); + } + + public boolean isRecognitionInitEvent() { + return false; + } + + public boolean isTrainInitEvent() { + return false; + } + + public boolean isCloseGestureInitEvent() { + return true; + } +} diff --git a/java/src/pm/device/wiimote/gesture/event/Recognize.java b/java/src/pm/device/wiimote/gesture/event/Recognize.java new file mode 100644 index 0000000..2721062 --- /dev/null +++ b/java/src/pm/device/wiimote/gesture/event/Recognize.java @@ -0,0 +1,24 @@ +package pm.device.wiimote.gesture.event; + +import org.wiigee.device.Device; +import org.wiigee.event.ButtonPressedEvent; + +public class Recognize extends ButtonPressedEvent { + protected static final long serialVersionUID = 1L; + + public Recognize(Device device) { + super(device, 0); + } + + public boolean isRecognitionInitEvent() { + return true; + } + + public boolean isTrainInitEvent() { + return false; + } + + public boolean isCloseGestureInitEvent() { + return false; + } +} diff --git a/java/src/pm/device/wiimote/gesture/event/Train.java b/java/src/pm/device/wiimote/gesture/event/Train.java new file mode 100644 index 0000000..8ca4b2b --- /dev/null +++ b/java/src/pm/device/wiimote/gesture/event/Train.java @@ -0,0 +1,24 @@ +package pm.device.wiimote.gesture.event; + +import org.wiigee.device.Device; +import org.wiigee.event.ButtonPressedEvent; + +public class Train extends ButtonPressedEvent { + protected static final long serialVersionUID = 1L; + + public Train(Device device) { + super(device, 0); + } + + public boolean isRecognitionInitEvent() { + return false; + } + + public boolean isTrainInitEvent() { + return true; + } + + public boolean isCloseGestureInitEvent() { + return false; + } +} diff --git a/java/src/pm/macro/event/SequenceListener.java b/java/src/pm/macro/event/SequenceListener.java index b3dd449..611e18e 100644 --- a/java/src/pm/macro/event/SequenceListener.java +++ b/java/src/pm/macro/event/SequenceListener.java @@ -3,6 +3,7 @@ package pm.macro.event; import java.util.ArrayList; import java.util.HashMap; +import pm.Device; import pm.Task; import pm.macro.Active; import pm.macro.Event; diff --git a/java/src/pm/task/TaskGatherer.java b/java/src/pm/task/TaskGatherer.java index c3702a5..f8ef169 100644 --- a/java/src/pm/task/TaskGatherer.java +++ b/java/src/pm/task/TaskGatherer.java @@ -2,6 +2,7 @@ package pm.task; import java.util.Queue; +import pm.Device; import pm.Task; public class TaskGatherer {