rewriting mote interfaces for faster simulation execution.

beeper interface
This commit is contained in:
fros4943 2008-10-28 09:33:00 +00:00
parent 46853d9395
commit eb7e75c851

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: ContikiBeeper.java,v 1.4 2007/01/09 10:05:19 fros4943 Exp $ * $Id: ContikiBeeper.java,v 1.5 2008/10/28 09:33:00 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -41,26 +41,28 @@ import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMoteInterface; import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.Beeper; import se.sics.cooja.interfaces.Beeper;
import se.sics.cooja.interfaces.PolledAfterActiveTicks;
/** /**
* Ths class represents a beeper. * Beeper mote interface.
* *
* It needs read access to the following core variables: * Contiki variables:
* <ul> * <ul>
* <li>char simBeeped (1=on, else off) * <li>char simBeeped (1=on, else off)
* </ul> * </ul>
* <p> * <p>
* Dependency core interfaces are: *
* Core interface:
* <ul> * <ul>
* <li>beep_interface * <li>beep_interface
* </ul> * </ul>
* <p> * <p>
* This observable is changed and notifies observers whenever the beeper changes
* state.
* *
* @author Fredrik Osterlind * This observable is changed and notifies observers when the mote beeps.
*
* @author Fredrik Österlind
*/ */
public class ContikiBeeper extends Beeper implements ContikiMoteInterface { public class ContikiBeeper extends Beeper implements ContikiMoteInterface, PolledAfterActiveTicks {
private Mote mote = null; private Mote mote = null;
private SectionMoteMemory moteMem = null; private SectionMoteMemory moteMem = null;
private static Logger logger = Logger.getLogger(ContikiBeeper.class); private static Logger logger = Logger.getLogger(ContikiBeeper.class);
@ -74,8 +76,6 @@ public class ContikiBeeper extends Beeper implements ContikiMoteInterface {
private double myEnergyConsumption = 0.0; private double myEnergyConsumption = 0.0;
private boolean justBeeped = false;
/** /**
* Creates an interface to the beeper at mote. * Creates an interface to the beeper at mote.
* *
@ -94,42 +94,51 @@ public class ContikiBeeper extends Beeper implements ContikiMoteInterface {
} }
public boolean isBeeping() { public boolean isBeeping() {
return justBeeped; return moteMem.getByteValueOf("simBeeped") == 1;
} }
public static String[] getCoreInterfaceDependencies() { public static String[] getCoreInterfaceDependencies() {
return new String[]{"beep_interface"}; return new String[]{"beep_interface"};
} }
public void doActionsBeforeTick() { private TimeEvent stopBeepEvent = new TimeEvent(0) {
// Nothing to do public void execute(int t) {
justBeeped = false; myEnergyConsumption = 0.0;
} }
};
public void doActionsAfterTick() { public void doActionsAfterTick() {
if (moteMem.getByteValueOf("simBeeped") == 1) { if (moteMem.getByteValueOf("simBeeped") == 1) {
myEnergyConsumption = ENERGY_CONSUMPTION_BEEP;
this.setChanged(); this.setChanged();
this.notifyObservers(mote); this.notifyObservers(mote);
justBeeped = true;
moteMem.setByteValueOf("simBeeped", (byte) 0); moteMem.setByteValueOf("simBeeped", (byte) 0);
myEnergyConsumption = ENERGY_CONSUMPTION_BEEP;
} else /* Schedule stop beeping (reset energy consumption) */
myEnergyConsumption = 0.0; mote.getSimulation().addEvent(stopBeepEvent, mote.getSimulation().getSimulationTime());
}
} }
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final JLabel statusLabel = new JLabel("Beeping active"); final JLabel statusLabel = new JLabel("Last beep at time: ?");
panel.add(statusLabel); panel.add(statusLabel);
Observer observer; Observer observer;
this.addObserver(observer = new Observer() { this.addObserver(observer = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
if (!isBeeping()) {
return;
}
int currentTime = mote.getSimulation().getSimulationTime(); int currentTime = mote.getSimulation().getSimulationTime();
statusLabel.setText("Beeping active: last beep at " + currentTime); statusLabel.setText("Last beep at time: " + currentTime);
// Beep on speakers
/* Beep on speakers */
Toolkit.getDefaultToolkit().beep(); Toolkit.getDefaultToolkit().beep();
} }
}); });