Herstructuratie alom:

- EventMaps in plaats van event+task steeds handmatig toevoegen
- EventMapCycles om eenvoudig te kunnen cyclen in geval van bvb een shift button
- JIntellitype en Wiimote device hiernaar omgeschreven
- wat overbodige logging verwijderd
This commit is contained in:
2011-06-07 18:21:24 +00:00
parent b7f2636bda
commit 15b8008257
12 changed files with 274 additions and 168 deletions

View File

@@ -1,58 +1,37 @@
package mimis;
import mimis.device.EventMapCycle;
import mimis.event.EventHandler;
import mimis.event.Task;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.manager.Exitable;
import mimis.manager.Titled;
import mimis.sequence.Sequence;
import mimis.sequence.EventMap;
import mimis.sequence.SequenceListener;
import mimis.sequence.State;
import mimis.sequence.state.Press;
public abstract class Device extends EventHandler implements Titled, Exitable {
protected String title;
protected EventMapCycle eventMapCycle;
protected SequenceListener sequenceListener;
public Device(String title) {
this.title = title;
}
public String title() {
return title;
}
/* Worker */
public void activate() throws ActivateException {
super.activate();
sequenceListener = new SequenceListener(this);
}
/* Register macro's */
protected void add(Sequence sequence, Task task) {
sequenceListener.add(sequence, task);
}
protected void add(State state, Task task) {
add(new Sequence(state), task);
}
protected void add(Press press, Task task, boolean macro) {
if (macro) {
add(new Macro(press), task);
} else {
add((State) press, task);
}
}
protected void add(Press press, Task task) {
add(press, task, true);
}
/* Recognize events */
protected void add(State state) {
log.debug(state.toString() + " " + state.getButton());
sequenceListener.add(state);
}
public String title() {
return title;
public void deactivate() throws DeactivateException {
super.deactivate();
sequenceListener.reset();
}
public void stop() {
@@ -65,4 +44,21 @@ public abstract class Device extends EventHandler implements Titled, Exitable {
}
super.stop();
}
/* SequenceListener */
protected void add(EventMap eventMap) {
sequenceListener.add(eventMap);
}
protected void remove(EventMap eventMap) {
sequenceListener.remove(eventMap);
}
protected void add(State state) {
sequenceListener.add(state);
}
protected void reset() {
sequenceListener.reset();
}
}

View File

@@ -85,7 +85,6 @@ public class Mimis extends EventHandler {
}
protected void end(Action action) {
log.debug(String.format("action(%s)", action));
switch (action) {
case NEXT:
eventRouter.set(applicationCycle.next());

View File

@@ -0,0 +1,13 @@
package mimis.device;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import mimis.sequence.EventMap;
import mimis.util.ArrayCycle;
public class EventMapCycle extends ArrayCycle<EventMap> {
protected static final long serialVersionUID = 1L;
protected Log log = LogFactory.getLog(getClass());
}

View File

@@ -3,23 +3,19 @@ package mimis.device.jintellitype;
import java.util.ArrayList;
import mimis.Device;
import mimis.event.Task;
import mimis.exception.button.UnknownButtonException;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.sequence.state.Press;
import mimis.sequence.state.Release;
import mimis.value.Action;
import mimis.value.Key;
import mimis.value.Target;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.IntellitypeListener;
import com.melloware.jintellitype.JIntellitype;
public class JIntellitypeDevice extends Device implements HotkeyListener, IntellitypeListener {
protected static final String TITLE = "JIntellitype";
protected JIntellitypeEventMapCycle eventMapCycle;
protected ArrayList<Hotkey> hotkeyList;
protected JIntellitype jit;
@@ -28,45 +24,15 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell
hotkeyList = new ArrayList<Hotkey>();
jit = JIntellitype.getInstance();
Hotkey.initialise(hotkeyList, jit);
eventMapCycle = new JIntellitypeEventMapCycle();
}
public void activate() throws ActivateException {
super.activate();
jit.addHotKeyListener(this);
jit.addIntellitypeListener(this);
add(
new Hotkey(Key.PRIOR),
new Task(Target.MIMIS, Action.PREVIOUS));
add(
new Hotkey(Key.NEXT),
new Task(Target.MIMIS, Action.NEXT));
add(
new Press(CommandButton.VOLUME_DOWN),
new Task(Target.APPLICATIONS, Action.VOLUME_DOWN));
add(
new Press(CommandButton.VOLUME_UP),
new Task(Target.APPLICATIONS, Action.VOLUME_UP));
add(
new Hotkey(Modifier.CTRL | Modifier.WIN, 'x'),
new Task(Target.MIMIS, Action.EXIT));
add(
new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'n'),
new Task(Target.APPLICATION, Action.NEXT));
add(
new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'p'),
new Task(Target.APPLICATION, Action.PREVIOUS));
/*add(
new Hotkey(Modifier.CTRL | Modifier.WIN, 't'),
new Task(Action.TEST, Target.MAIN));
add(
new Hotkey(Modifier.CTRL | Modifier.WIN, 'r'),
new Hotkey(Modifier.CTRL | Modifier.WIN, 's'),
new Continuous(Action.REPEAT, Target.APPLICATIONS, 500));*/
}
protected void add(Hotkey hotkey, Task task) {
add(new Press(hotkey), task);
add(eventMapCycle.mimis);
add(eventMapCycle.player);
}
public void onIntellitype(int command) {
@@ -89,8 +55,8 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell
}
}
public void stop() {
super.stop();
public void deactivate() throws DeactivateException {
super.deactivate();
jit.removeHotKeyListener(this);
jit.removeIntellitypeListener(this);
jit.cleanUp();

View File

@@ -0,0 +1,44 @@
package mimis.device.jintellitype;
import mimis.device.EventMapCycle;
import mimis.event.Task;
import mimis.sequence.EventMap;
import mimis.sequence.state.Press;
import mimis.value.Action;
import mimis.value.Key;
import mimis.value.Target;
public class JIntellitypeEventMapCycle extends EventMapCycle {
protected static final long serialVersionUID = 1L;
public EventMap mimis, player;
public JIntellitypeEventMapCycle() {
/* Mimis */
mimis = new EventMap();
mimis.add(
new Hotkey(Key.PRIOR),
new Task(Target.MIMIS, Action.PREVIOUS));
mimis.add(
new Hotkey(Key.NEXT),
new Task(Target.MIMIS, Action.NEXT));
/* Player */
player = new EventMap();
player.add(
new Press(CommandButton.VOLUME_DOWN),
new Task(Target.APPLICATIONS, Action.VOLUME_DOWN));
player.add(
new Press(CommandButton.VOLUME_UP),
new Task(Target.APPLICATIONS, Action.VOLUME_UP));
player.add(
new Hotkey(Modifier.CTRL | Modifier.WIN, 'x'),
new Task(Target.MIMIS, Action.EXIT));
player.add(
new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'n'),
new Task(Target.APPLICATION, Action.NEXT));
player.add(
new Hotkey(Modifier.CTRL | Modifier.SHIFT | Modifier.WIN, 'p'),
new Task(Target.APPLICATION, Action.PREVIOUS));
}
}

View File

@@ -4,7 +4,6 @@ import mimis.Button;
import mimis.Device;
import mimis.device.wiimote.gesture.GestureDevice;
import mimis.event.Feedback;
import mimis.event.Task;
import mimis.exception.button.UnknownButtonException;
import mimis.exception.device.DeviceNotFoundException;
import mimis.exception.worker.ActivateException;
@@ -12,31 +11,26 @@ import mimis.exception.worker.DeactivateException;
import mimis.sequence.state.Press;
import mimis.sequence.state.Release;
import mimis.value.Action;
import org.wiigee.event.GestureEvent;
import org.wiigee.event.GestureListener;
import org.wiigee.util.Log;
import wiiusej.Wiimote;
import wiiusej.values.Calibration;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
public class WiimoteDevice extends Device implements GestureListener {
protected static final String TITLE = "Wiimote";
protected static final int CONNECT_MAX = 10;
protected static final int RUMBLE = 150;
public static final int TIMEOUT = 200;
protected static final int TIMEOUT = 200;
protected static WiimoteService wiimoteService;
protected Wiimote wiimote;
protected Calibration calibration;
protected GestureDevice gestureDevice;
protected int gestureId;
protected WiimoteEventMapCycle eventMapCycle;
protected WiimoteDiscovery wiimoteDiscovery;
protected boolean connected;
protected Wiimote wiimote;
protected GestureDevice gestureDevice;
protected int gestureId;
static {
WiimoteDevice.wiimoteService = new WiimoteService();
@@ -45,6 +39,7 @@ public class WiimoteDevice extends Device implements GestureListener {
public WiimoteDevice() {
super(TITLE);
eventMapCycle = new WiimoteEventMapCycle();
wiimoteDiscovery = new WiimoteDiscovery(this);
gestureDevice = new GestureDevice();
gestureDevice.add(this);
@@ -55,69 +50,11 @@ public class WiimoteDevice extends Device implements GestureListener {
public void activate() throws ActivateException {
super.activate();
connect();
/*add(
new Hold(WiimoteButton.A),
new Task(Action.TRAIN),
new Task(Action.STOP));*/
add(
new Press(WiimoteButton.B),
new Task(Action.SAVE));
add(
new Press(WiimoteButton.DOWN),
new Task(Action.LOAD));
/*add(
new Hold(WiimoteButton.HOME),
new Task(Action.RECOGNIZE),
new Task(Action.STOP));/*
add(
new Press(WiimoteButton.A),
new Task(Target.APPLICATION, Action.PLAY));
add(
new Press(WiimoteButton.B),
new Task(Target.APPLICATION, Action.MUTE));
add(
new Press(WiimoteButton.ONE),
new Task(Target.APPLICATION, Action.SHUFFLE));
add(
new Press(WiimoteButton.TWO),
new Task(Target.APPLICATION, Action.REPEAT));
add(
new Press(WiimoteButton.UP),
new Task(Target.APPLICATION, Action.NEXT));
add(
new Press(WiimoteButton.DOWN),
new Task(Target.APPLICATION, Action.PREVIOUS));
add(
new Hold(WiimoteButton.RIGHT),
new Dynamic(Action.FORWARD, Target.APPLICATION, 200, -30));
add(
new Hold(WiimoteButton.LEFT),
new Dynamic(Action.REWIND, Target.APPLICATION, 200, -30));
add(
new Hold(WiimoteButton.MINUS),
new Continuous(Action.VOLUME_DOWN, Target.APPLICATION, 100));
add(
new Hold(WiimoteButton.PLUS),
new Continuous(Action.VOLUME_UP, Target.APPLICATION, 100));
add(
new Press(WiimoteButton.HOME),
new Task(Target.MANAGER, Action.NEXT));
try {
add(
new Macro(
new Hold(WiimoteButton.TWO),
new Press(WiimoteButton.PLUS),
new Release(WiimoteButton.TWO)),
new Task(Target.APPLICATION, Action.LIKE));
add(
new Macro(
new Hold(WiimoteButton.TWO),
new Press(WiimoteButton.MINUS),
new Release(WiimoteButton.TWO)),
new Task(Target.APPLICATION, Action.DISLIKE));
} catch (StateOrderException e) {}*/
add(eventMapCycle.mimis);
add(eventMapCycle.player);
add(eventMapCycle.like);
}
public void deactivate() throws DeactivateException {
super.deactivate();
if (wiimote != null) {
@@ -132,32 +69,40 @@ public class WiimoteDevice extends Device implements GestureListener {
}
/* Events */
public void action(Action action) {
public void begin(Action action) {
switch (action) {
case TRAIN:
System.out.println("Wiimote Train");
log.debug("Gesture train");
gestureDevice.train();
break;
case STOP:
System.out.println("Wiimote Stop");
gestureDevice.stop();
break;
case SAVE:
System.out.println("Wiimote Save");
log.debug("Gesture save");
gestureDevice.close();
gestureDevice.saveGesture(gestureId, "C:\\gesture-" + gestureId);
++gestureId;
break;
case LOAD:
log.debug("Gesture load");
for (int i = 0; i < gestureId; ++i) {
gestureDevice.loadGesture("C:\\gesture-" + i);
}
break;
case RECOGNIZE:
log.debug("Gesture recognize");
gestureDevice.recognize();
break;
}
}
public void end(Action action) {
switch (action) {
case TRAIN:
case RECOGNIZE:
log.debug("Gesture stop");
gestureDevice.stop();
break;
}
}
public void feedback(Feedback feedback) {
System.out.println("Wiimote feedback");

View File

@@ -0,0 +1,96 @@
package mimis.device.wiimote;
import mimis.Macro;
import mimis.device.EventMapCycle;
import mimis.event.Task;
import mimis.exception.macro.StateOrderException;
import mimis.sequence.EventMap;
import mimis.sequence.state.Hold;
import mimis.sequence.state.Press;
import mimis.sequence.state.Release;
import mimis.value.Action;
import mimis.value.Target;
public class WiimoteEventMapCycle extends EventMapCycle {
protected static final long serialVersionUID = 1L;
public EventMap mimis, player, gesture, like;
public WiimoteEventMapCycle() {
/* Mimis */
mimis = new EventMap();
mimis.add(
new Press(WiimoteButton.HOME),
new Task(Target.MIMIS, Action.NEXT));
/* Gesture */
gesture = new EventMap();
gesture.add(
new Press(WiimoteButton.A),
new Task(Action.TRAIN));
gesture.add(
new Press(WiimoteButton.B),
new Task(Action.SAVE));
gesture.add(
new Press(WiimoteButton.DOWN),
new Task(Action.LOAD));
gesture.add(
new Press(WiimoteButton.HOME),
new Task(Action.RECOGNIZE));
add(gesture);
/* Player */
player = new EventMap();
player.add(
new Press(WiimoteButton.A),
new Task(Target.APPLICATION, Action.PLAY));
player.add(
new Press(WiimoteButton.B),
new Task(Target.APPLICATION, Action.MUTE));
player.add(
new Press(WiimoteButton.ONE),
new Task(Target.APPLICATION, Action.SHUFFLE));
player.add(
new Press(WiimoteButton.TWO),
new Task(Target.APPLICATION, Action.REPEAT));
player.add(
new Press(WiimoteButton.UP),
new Task(Target.APPLICATION, Action.NEXT));
player.add(
new Press(WiimoteButton.DOWN),
new Task(Target.APPLICATION, Action.PREVIOUS));
player.add(
new Press(WiimoteButton.RIGHT),
new Task(Target.APPLICATION, Action.FORWARD));
player.add(
new Press(WiimoteButton.LEFT),
new Task(Target.APPLICATION, Action.REWIND));
player.add(
new Press(WiimoteButton.MINUS),
new Task(Target.APPLICATION, Action.VOLUME_DOWN));
player.add(
new Press(WiimoteButton.PLUS),
new Task(Target.APPLICATION, Action.VOLUME_UP));
add(player);
/* Like */
try {
like = new EventMap();
like.add(
new Macro(
new Hold(WiimoteButton.TWO),
new Press(WiimoteButton.PLUS),
new Release(WiimoteButton.TWO)),
new Task(Target.APPLICATION, Action.LIKE));
like.add(
new Macro(
new Hold(WiimoteButton.TWO),
new Press(WiimoteButton.MINUS),
new Release(WiimoteButton.TWO)),
new Task(Target.APPLICATION, Action.DISLIKE));
add(like);
} catch (StateOrderException e) {
log.error(e);
}
}
}

View File

@@ -21,7 +21,6 @@ public abstract class EventHandler extends EventListener {
protected void feedback(Feedback feedback) {}
protected final void task(Task task) {
log.debug("Signal: " + task.signal);
Action action = task.getAction();
switch (task.getSignal()) {
case BEGIN:

View File

@@ -16,7 +16,6 @@ public abstract class EventListener extends Worker {
}
public void add(Event event) {
log.info("event " + event + " " + active);
eventQueue.add(event);
synchronized (work) {
work.notifyAll();

View File

@@ -17,7 +17,6 @@ public class LocalRouter extends EventRouter {
default:
for (EventListener eventListener : eventListenerList) {
if (event.compatible(eventListener)) {
log.trace(eventListener);
eventListener.add(event);
}
}

View File

@@ -0,0 +1,37 @@
package mimis.sequence;
import java.util.HashMap;
import mimis.Button;
import mimis.Event;
import mimis.Macro;
import mimis.event.Task;
import mimis.sequence.state.Press;
public class EventMap extends HashMap<Sequence, Event> {
protected static final long serialVersionUID = 1L;
public void add(Button button, Task task) {
add(new Press(button), task);
}
public void add(Press press, Task task) {
add(press, task, true);
}
protected void add(Press press, Task task, boolean macro) {
if (macro) {
add(new Macro(press), task);
} else {
add((State) press, task);
}
}
protected void add(State state, Task task) {
add(new Sequence(state), task);
}
public void add(Sequence sequence, Task task) {
put(sequence, task);
}
}

View File

@@ -1,7 +1,7 @@
package mimis.sequence;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -17,31 +17,44 @@ public class SequenceListener {
protected Log log = LogFactory.getLog(getClass());
protected EventHandler self;
protected EventMap eventMap;
protected ArrayList<Sequence> sequenceList;
protected HashMap<Sequence, Event> eventMap;
protected ArrayList<Active> activeList;
protected static EventListener eventListener;
public SequenceListener(EventHandler self) {
this.self = self;
eventMap = new EventMap();
sequenceList = new ArrayList<Sequence>();
eventMap = new HashMap<Sequence, Event>();
activeList = new ArrayList<Active>();
}
public static void initialise(EventListener eventListener) {
SequenceListener.eventListener = eventListener;
}
public int add(Sequence sequence, Task task) {
int id = sequenceList.size();
sequenceList.add(sequence);
eventMap.put(sequence, task);
return id;
public synchronized void add(EventMap eventMap) {
this.eventMap.putAll(eventMap);
sequenceList.addAll(eventMap.keySet());
}
public void add(State state) {
public void remove(EventMap eventMap) {
for (Entry<Sequence, Event> entry : eventMap.entrySet()) {
Sequence sequence = entry.getKey();
this.eventMap.remove(sequence);
sequenceList.remove(sequence);
activeList.remove(sequence);
}
}
public synchronized void reset() {
eventMap.clear();
sequenceList.clear();
activeList.clear();
}
public synchronized void add(State state) {
for (Sequence sequence : sequenceList) {
activeList.add(new Active(sequence));
}