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.
This commit is contained in:
Enrico Joerns 2014-10-31 01:20:54 +01:00
parent be88a4fc52
commit 0a63922fa2
5 changed files with 50 additions and 154 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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.