From 0a63922fa247127946d62c06d19921696903e217 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 31 Oct 2014 01:20:54 +0100 Subject: [PATCH] Buttons: Move implementation of button routines to Button class The implementation of clickButton(), pressButton(), and releaseButton() can be shared accross the several node-dependent implementations as they use the node-dependent doPressButton() doReleaseButton() routines. --- .../cooja/mspmote/interfaces/ESBButton.java | 18 +------ .../cooja/mspmote/interfaces/MspButton.java | 41 +-------------- .../cooja/mspmote/interfaces/SkyButton.java | 46 +---------------- .../contikimote/interfaces/ContikiButton.java | 49 +----------------- .../contikios/cooja/interfaces/Button.java | 50 +++++++++++++++++-- 5 files changed, 50 insertions(+), 154 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java index 6e03921f3..cb6b6689f 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/ESBButton.java @@ -46,31 +46,15 @@ public class ESBButton extends Button { private ESBMote mote; public ESBButton(Mote mote) { + super(mote); this.mote = (ESBMote) mote; } - public void clickButton() { - pressButton(); - releaseButton(); - } - - public void releaseButton() { - doReleaseButton(); - setChanged(); - notifyObservers(); - } - @Override protected void doReleaseButton() { mote.esbNode.setButton(false); } - public void pressButton() { - doPressButton(); - setChanged(); - notifyObservers(); - } - @Override protected void doPressButton() { mote.esbNode.setButton(true); diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java index 9762430fc..30214802a 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspButton.java @@ -45,6 +45,7 @@ public class MspButton extends Button { private final se.sics.mspsim.chip.Button button; public MspButton(Mote mote) { + super(mote); final MspMote mspMote = (MspMote) mote; sim = mote.getSimulation(); button = mspMote.getCPU().getChip(se.sics.mspsim.chip.Button.class); @@ -53,34 +54,11 @@ public class MspButton extends Button { } } - @Override - public void clickButton() { - sim.invokeSimulationThread(new ButtonClick()); - } - - @Override - public void pressButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - doPressButton(); - } - }); - } - @Override protected void doPressButton() { button.setPressed(true); } - @Override - public void releaseButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - doReleaseButton(); - } - }); - } - @Override protected void doReleaseButton() { button.setPressed(false); @@ -91,21 +69,4 @@ public class MspButton extends Button { return button.isPressed(); } - private class ButtonClick extends TimeEvent implements Runnable { - - public ButtonClick() { - super(0); - } - - @Override - public void run() { - button.setPressed(true); - sim.scheduleEvent(this, sim.getSimulationTime() + Simulation.MILLISECOND); - } - - @Override - public void execute(long t) { - button.setPressed(false); - } - } } diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java index c785c5695..b11030ccf 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/SkyButton.java @@ -33,8 +33,6 @@ package org.contikios.cooja.mspmote.interfaces; import org.apache.log4j.Logger; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; -import org.contikios.cooja.MoteTimeEvent; -import org.contikios.cooja.Simulation; import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.mspmote.SkyMote; @@ -43,42 +41,10 @@ public class SkyButton extends Button { private static Logger logger = Logger.getLogger(SkyButton.class); private SkyMote skyMote; - private Simulation sim; - - private MoteTimeEvent pressButtonEvent; - private MoteTimeEvent releaseButtonEvent; - + public SkyButton(Mote mote) { + super(mote); skyMote = (SkyMote) mote; - sim = mote.getSimulation(); - - pressButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - doPressButton(); - } - }; - releaseButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - doReleaseButton(); - } - }; - } - - public void clickButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); - sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND); - } - }); - } - - public void pressButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); - } - }); } @Override @@ -86,14 +52,6 @@ public class SkyButton extends Button { skyMote.skyNode.setButton(true); } - public void releaseButton() { - sim.invokeSimulationThread(new Runnable() { - public void run() { - sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime()); - } - }); - } - @Override protected void doReleaseButton() { skyMote.skyNode.setButton(false); diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java index 8190968bd..67a16c97f 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiButton.java @@ -72,6 +72,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface { * @see org.contikios.cooja.MoteInterfaceHandler */ public ContikiButton(Mote mote) { + super(mote); this.mote = (ContikiMote) mote; this.moteMem = new VarMemory(mote.getMemory()); } @@ -80,54 +81,6 @@ public class ContikiButton extends Button implements ContikiMoteInterface { return new String[]{"button_interface"}; } - private TimeEvent pressButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - doPressButton(); - } - }; - - private TimeEvent releaseButtonEvent = new MoteTimeEvent(mote, 0) { - public void execute(long t) { - /* Wait until button change is handled by Contiki */ - if (moteMem.getByteValueOf("simButtonChanged") != 0) { - /* Postpone button release */ - mote.getSimulation().scheduleEvent(releaseButtonEvent, t + Simulation.MILLISECOND); - return; - } - - /*logger.info("Releasing button at: " + t);*/ - doReleaseButton(); - } - }; - - /** - * Clicks button: Presses and immediately releases button. - */ - public void clickButton() { - mote.getSimulation().invokeSimulationThread(new Runnable() { - public void run() { - mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime()); - mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND); - } - }); - } - - public void pressButton() { - mote.getSimulation().invokeSimulationThread(new Runnable() { - public void run() { - mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime()); - } - }); - } - - public void releaseButton() { - mote.getSimulation().invokeSimulationThread(new Runnable() { - public void run() { - mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime()); - } - }); - } - @Override protected void doReleaseButton() { moteMem.setByteValueOf("simButtonIsDown", (byte) 0); diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java index 6c402c0e7..02985a8e8 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Button.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Button.java @@ -36,6 +36,7 @@ import java.util.Collection; import javax.swing.JButton; import javax.swing.JPanel; import org.contikios.cooja.*; +import org.contikios.cooja.mspmote.SkyMote; import org.jdom.Element; /** @@ -48,16 +49,49 @@ import org.jdom.Element; @ClassDescription("Button") public abstract class Button extends MoteInterface { + private final Simulation sim; + + private final MoteTimeEvent pressButtonEvent; + private final MoteTimeEvent releaseButtonEvent; + + public Button(Mote mote) { + sim = mote.getSimulation(); + + pressButtonEvent = new MoteTimeEvent(mote, 0) { + public void execute(long t) { + doPressButton(); + } + }; + releaseButtonEvent = new MoteTimeEvent(mote, 0) { + public void execute(long t) { + doReleaseButton(); + } + }; + } + /** * Clicks button. Button will be pressed for some time and then automatically * released. */ - public abstract void clickButton(); + public void clickButton() { + sim.invokeSimulationThread(new Runnable() { + public void run() { + sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); + sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND); + } + }); + } /** - * Releases button (if pressed). + * Presses button. */ - public abstract void releaseButton(); + public void pressButton() { + sim.invokeSimulationThread(new Runnable() { + public void run() { + sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime()); + } + }); + } /** * Node-type dependent implementation of pressing a button. @@ -65,9 +99,15 @@ public abstract class Button extends MoteInterface { protected abstract void doPressButton(); /** - * Presses button (if not already pressed). + * Releases button. */ - public abstract void pressButton(); + public void releaseButton() { + sim.invokeSimulationThread(new Runnable() { + public void run() { + sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime()); + } + }); + } /** * Node-type dependent implementation of releasing a button.