Mogelijkheid tot het sturen van Actions naar Devices toegevoegd. Gestures ingevoerd in de WiimoteDevice. Herkennen is nog niet constant succesvol!

This commit is contained in:
2011-02-14 19:25:19 +00:00
parent b0e2b09487
commit 6ee9e9fff1
12 changed files with 274 additions and 39 deletions

View File

@@ -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);
}
}

View File

@@ -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());
}
}
}

View File

@@ -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<Integer> wiimoteList;
protected Wiimote[] wiimoteArray;

View File

@@ -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();
}*/
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}