rewriting mote interfaces for faster simulation execution.

button interface
This commit is contained in:
fros4943 2008-10-28 09:42:26 +00:00
parent eb7e75c851
commit ed9063719f

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. * Copyright (c) 2008, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiButton.java,v 1.6 2007/04/02 14:14:28 fros4943 Exp $ * $Id: ContikiButton.java,v 1.7 2008/10/28 09:42:26 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -42,46 +42,40 @@ import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.Button; import se.sics.cooja.interfaces.Button;
/** /**
* This class represents a button. * Button mote interface.
* *
* It needs read/write access to the following core variables: * Contiki variables:
* <ul> * <ul>
* <li>char simButtonIsDown (1=down, else up) * <li>char simButtonIsDown (1=down, else up)
* <li>char simButtonChanged (1=changed, else not changed) * <li>char simButtonChanged (1=changed, else not changed)
* <li>char simButtonIsActive (1=active, else inactive) * <li>char simButtonIsActive (1=active, else inactive)
* </ul> * </ul>
* Dependency core interfaces are: *
* Core interface:
* <ul> * <ul>
* <li>button_interface * <li>button_interface
* </ul> * </ul>
* <p> * <p>
* This observable notifies observers when the button changes state (between *
* pressed and released). * This observable notifies when the button is pressed or released.
* *
* @author Fredrik Osterlind * @author Fredrik Österlind
*/ */
public class ContikiButton extends Button implements ContikiMoteInterface { public class ContikiButton extends Button implements ContikiMoteInterface {
private SectionMoteMemory moteMem; private SectionMoteMemory moteMem;
private Mote mote; private Mote mote;
private boolean shouldBeReleased = false;
private static Logger logger = Logger.getLogger(ContikiButton.class); private static Logger logger = Logger.getLogger(ContikiButton.class);
private final boolean RAISES_EXTERNAL_INTERRUPT;
/** /**
* Creates an interface to the button at mote. * Creates an interface to the button at mote.
* *
* @param mote * @param mote
* Button's mote. * Button's mote.
* @see Mote * @see Mote
* @see se.sics.cooja.MoteInterfaceHandler * @see se.sics.cooja.MoteInterfaceHandler
*/ */
public ContikiButton(Mote mote) { public ContikiButton(Mote mote) {
// Read class configurations of this mote type
RAISES_EXTERNAL_INTERRUPT = mote.getType().getConfig()
.getBooleanValue(ContikiButton.class, "EXTERNAL_INTERRUPT_bool");
this.mote = mote; this.mote = mote;
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
} }
@ -90,12 +84,31 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
return new String[]{"button_interface"}; return new String[]{"button_interface"};
} }
private TimeEvent releaseButtonEvent = new TimeEvent(0) {
public void execute(int t) {
/* Force mote awake when button is down */
mote.setState(Mote.State.ACTIVE);
/* Wait until button change is handled by Contiki */
if (moteMem.getByteValueOf("simButtonChanged") == 0) {
logger.info("Releasing button at: " + t);
releaseButton();
} else {
/* Reschedule button release */
mote.getSimulation().addEvent(releaseButtonEvent, t+1);
}
}
};
/** /**
* Clicks button. Button will be down for one tick, and then released. * Clicks button: Presses and immediately releases button.
*/ */
public void clickButton() { public void clickButton() {
pressButton(); pressButton();
shouldBeReleased = true;
/* Schedule release button */
mote.getSimulation().addEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime());
} }
public void releaseButton() { public void releaseButton() {
@ -104,9 +117,8 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
if (moteMem.getByteValueOf("simButtonIsActive") == 1) { if (moteMem.getByteValueOf("simButtonIsActive") == 1) {
moteMem.setByteValueOf("simButtonChanged", (byte) 1); moteMem.setByteValueOf("simButtonChanged", (byte) 1);
// If mote is inactive, wake it up /* If mote is inactive, wake it up */
if (RAISES_EXTERNAL_INTERRUPT) mote.setState(Mote.State.ACTIVE);
mote.setState(Mote.State.ACTIVE);
setChanged(); setChanged();
notifyObservers(); notifyObservers();
@ -119,9 +131,8 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
if (moteMem.getByteValueOf("simButtonIsActive") == 1) { if (moteMem.getByteValueOf("simButtonIsActive") == 1) {
moteMem.setByteValueOf("simButtonChanged", (byte) 1); moteMem.setByteValueOf("simButtonChanged", (byte) 1);
// If mote is inactive, wake it up /* If mote is inactive, wake it up */
if (RAISES_EXTERNAL_INTERRUPT) mote.setState(Mote.State.ACTIVE);
mote.setState(Mote.State.ACTIVE);
setChanged(); setChanged();
notifyObservers(); notifyObservers();
@ -131,27 +142,6 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
public boolean isPressed() { public boolean isPressed() {
return moteMem.getByteValueOf("simButtonIsDown") == 1; return moteMem.getByteValueOf("simButtonIsDown") == 1;
} }
public void doActionsBeforeTick() {
// Nothing to do
}
public void doActionsAfterTick() {
// Make sure a mote never falls asleep with unhandled button events
if (moteMem.getByteValueOf("simButtonChanged") == 1
&& RAISES_EXTERNAL_INTERRUPT) {
mote.setState(Mote.State.ACTIVE);
}
// If a button is pressed and should be clicked, release it now
if (shouldBeReleased) {
// Make sure that the earlier press event has been handled by core
if (moteMem.getByteValueOf("simButtonChanged") == 0) {
releaseButton();
shouldBeReleased = false;
}
}
}
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel(); JPanel panel = new JPanel();