From 9048c76afea474e7177409dc267ba69f58d22064 Mon Sep 17 00:00:00 2001 From: Bram Veenboer Date: Sat, 5 Feb 2011 22:10:39 +0000 Subject: [PATCH] Thread aangemaakt voor het updaten van features en pollen van events. Misschien moet het hele Device een Thread worden? --- java/src/pm/Main.java | 3 +- .../voorbeeld/VoorbeeldApplication.java | 11 ++-- java/src/pm/device/JavaInputDevice.java | 58 ++++++++++++------- java/src/pm/device/example/ExampleDevice.java | 8 +-- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/java/src/pm/Main.java b/java/src/pm/Main.java index e851c70..717b960 100644 --- a/java/src/pm/Main.java +++ b/java/src/pm/Main.java @@ -50,7 +50,7 @@ public class Main extends Target { } public void start() throws Exception { - addDevice(new ExampleDevice()); + //addDevice(new ExampleDevice()); addDevice(new RumblepadDevice()); Application application = new VoorbeeldApplication(); @@ -67,6 +67,7 @@ public class Main extends Target { public void run() throws ActionException, EventException { run = true; while (run) { + //System.out.println("Print!"); if (actionQueue.isEmpty()) { try { Thread.sleep(SLEEP); diff --git a/java/src/pm/application/voorbeeld/VoorbeeldApplication.java b/java/src/pm/application/voorbeeld/VoorbeeldApplication.java index 3a9855b..186095e 100644 --- a/java/src/pm/application/voorbeeld/VoorbeeldApplication.java +++ b/java/src/pm/application/voorbeeld/VoorbeeldApplication.java @@ -3,12 +3,15 @@ package pm.application.voorbeeld; import pm.application.Application; public class VoorbeeldApplication extends Application { - public void start() throws Exception { - System.out.println("jeheee!"); - //throw new Exception("GEK"); + public void start() { + System.out.println("start"); + } + + public void stop() { + System.out.println("stop"); } public void test() { - System.out.println("neheee!"); + System.out.println("test"); } } diff --git a/java/src/pm/device/JavaInputDevice.java b/java/src/pm/device/JavaInputDevice.java index 5b990ff..4b8feee 100644 --- a/java/src/pm/device/JavaInputDevice.java +++ b/java/src/pm/device/JavaInputDevice.java @@ -13,6 +13,8 @@ import de.hardcode.jxinput.event.JXInputButtonEvent; import de.hardcode.jxinput.event.JXInputDirectionalEvent; import de.hardcode.jxinput.event.JXInputEventManager; +import pm.action.Action; +import pm.event.Target; import pm.exception.ServiceJavaInputDeviceNotFoundException; import pm.exception.ServiceJavaInputException; @@ -56,38 +58,50 @@ public abstract class JavaInputDevice extends Device { public void start() { startListeners(); - while (run) { - boolean sleep = true; - if (!axisEventQueue.isEmpty()) { - processEvent(axisEventQueue.poll()); - sleep = false; + startUpdateThread(); + } + + protected void startUpdateThread() { + new Thread() { + public void run() { + while (run) { + JXInputManager.updateFeatures(); + boolean sleep = true; + if (!axisEventQueue.isEmpty()) { + processEvent(axisEventQueue.poll()); + sleep = false; + } + if (!buttonEventQueue.isEmpty()) { + processEvent(buttonEventQueue.poll()); + sleep = false; + } + if (!directionalEventQueue.isEmpty()) { + processEvent(directionalEventQueue.poll()); + sleep = false; + } + if (sleep) { + try { + Thread.sleep(SLEEP); + } catch (InterruptedException e) {} + } + } } - if (!buttonEventQueue.isEmpty()) { - processEvent(buttonEventQueue.poll()); - sleep = false; - } - if (!directionalEventQueue.isEmpty()) { - processEvent(directionalEventQueue.poll()); - sleep = false; - } - if (sleep) { - try { - Thread.sleep(SLEEP); - } catch (InterruptedException e) {} - } - } + }.start(); } protected void processEvent(JXInputAxisEvent event) { - + addAction(Action.START, Target.APPLICATION); + //System.out.println(event); } protected void processEvent(JXInputButtonEvent event) { - System.out.println(event); + addAction(Action.TEST, Target.APPLICATION); + //System.out.println(event); } protected void processEvent(JXInputDirectionalEvent event) { - + addAction(Action.EXIT, Target.APPLICATION); + //System.out.println(event); } protected void startListeners() { diff --git a/java/src/pm/device/example/ExampleDevice.java b/java/src/pm/device/example/ExampleDevice.java index 66d598c..5ec11a8 100644 --- a/java/src/pm/device/example/ExampleDevice.java +++ b/java/src/pm/device/example/ExampleDevice.java @@ -6,10 +6,10 @@ import pm.event.Target; public class ExampleDevice extends Device { public void start() { - System.out.println("Ik hoef niets te starten"); - addAction(Action.START, Target.APPLICATION); - addAction(Action.TEST, Target.APPLICATION); - addAction(Action.EXIT, Target.MAIN); + //System.out.println("Ik hoef niets te starten"); + //addAction(Action.START, Target.APPLICATION); + //addAction(Action.TEST, Target.APPLICATION); + //addAction(Action.EXIT, Target.MAIN); } public void exit() {