rewriting mote interfaces for faster simulation execution.

LEDs interface
This commit is contained in:
fros4943 2008-10-28 10:21:37 +00:00
parent 892ed6f316
commit 960f32f3d4

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: ContikiLED.java,v 1.4 2008/02/11 15:53:28 fros4943 Exp $ * $Id: ContikiLED.java,v 1.5 2008/10/28 10:21:37 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote.interfaces; package se.sics.cooja.contikimote.interfaces;
@ -40,30 +40,33 @@ 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.LED; import se.sics.cooja.interfaces.LED;
import se.sics.cooja.interfaces.PolledAfterActiveTicks;
/** /**
* This class represents three mote LEDs. * LEDs mote interface.
* *
* It needs read access to the following core variables: * Contiki variables:
* <ul> * <ul>
* <li>char simLedsValue * <li>char simLedsValue
* </ul> * </ul>
* <p> * <p>
* Dependency core interfaces are: *
* Core interface:
* <ul> * <ul>
* <li>leds_interface * <li>leds_interface
* </ul> * </ul>
* <p> * <p>
* This observable is changed and notifies observers whenever any led has
* changed.
* *
* @author Fredrik Osterlind * This observable notifies when any LED changes.
*
* @author Fredrik Österlind
*/ */
public class ContikiLED extends LED implements ContikiMoteInterface { public class ContikiLED extends LED implements ContikiMoteInterface, PolledAfterActiveTicks {
private static Logger logger = Logger.getLogger(ContikiLED.class); private static Logger logger = Logger.getLogger(ContikiLED.class);
private Mote mote = null; private Mote mote = null;
private SectionMoteMemory moteMem = null; private SectionMoteMemory moteMem = null;
private byte oldLedValue = 0; private byte currentLedValue = 0;
private static final byte LEDS_GREEN = 1; private static final byte LEDS_GREEN = 1;
private static final byte LEDS_YELLOW = 2; private static final byte LEDS_YELLOW = 2;
@ -107,10 +110,10 @@ public class ContikiLED extends LED implements ContikiMoteInterface {
} }
/** /**
* Creates an interface to the led at mote. * Creates an interface to LEDs at mote.
*
* @param mote Mote
* *
* @param mote
* Led's mote.
* @see Mote * @see Mote
* @see se.sics.cooja.MoteInterfaceHandler * @see se.sics.cooja.MoteInterfaceHandler
*/ */
@ -127,12 +130,9 @@ public class ContikiLED extends LED implements ContikiMoteInterface {
this.moteMem = (SectionMoteMemory) mote.getMemory(); this.moteMem = (SectionMoteMemory) mote.getMemory();
if (energyOfGreenLedPerTick < 0) { if (energyOfGreenLedPerTick < 0) {
energyOfGreenLedPerTick = ENERGY_CONSUMPTION_GREEN_LED energyOfGreenLedPerTick = ENERGY_CONSUMPTION_GREEN_LED * 0.001;
* mote.getSimulation().getTickTimeInSeconds(); energyOfYellowLedPerTick = ENERGY_CONSUMPTION_YELLOW_LED * 0.001;
energyOfYellowLedPerTick = ENERGY_CONSUMPTION_YELLOW_LED energyOfRedLedPerTick = ENERGY_CONSUMPTION_RED_LED * 0.001;
* mote.getSimulation().getTickTimeInSeconds();
energyOfRedLedPerTick = ENERGY_CONSUMPTION_RED_LED
* mote.getSimulation().getTickTimeInSeconds();
} }
} }
@ -141,37 +141,26 @@ public class ContikiLED extends LED implements ContikiMoteInterface {
} }
public boolean isAnyOn() { public boolean isAnyOn() {
return oldLedValue > 0; return currentLedValue > 0;
} }
public boolean isGreenOn() { public boolean isGreenOn() {
return (oldLedValue & LEDS_GREEN) > 0; return (currentLedValue & LEDS_GREEN) > 0;
} }
public boolean isYellowOn() { public boolean isYellowOn() {
return (oldLedValue & LEDS_YELLOW) > 0; return (currentLedValue & LEDS_YELLOW) > 0;
} }
public boolean isRedOn() { public boolean isRedOn() {
return (oldLedValue & LEDS_RED) > 0; return (currentLedValue & LEDS_RED) > 0;
}
public void doActionsBeforeTick() {
// Nothing to do
} }
public void doActionsAfterTick() { public void doActionsAfterTick() {
if (checkLedStatus()) {
this.setChanged();
this.notifyObservers(mote);
}
}
private boolean checkLedStatus() {
boolean ledChanged; boolean ledChanged;
byte newLedsValue = moteMem.getByteValueOf("simLedsValue"); byte newLedsValue = moteMem.getByteValueOf("simLedsValue");
if (newLedsValue != oldLedValue) { if (newLedsValue != currentLedValue) {
ledChanged = true; ledChanged = true;
} else { } else {
ledChanged = false; ledChanged = false;
@ -188,8 +177,11 @@ public class ContikiLED extends LED implements ContikiMoteInterface {
myEnergyConsumption += energyOfRedLedPerTick; myEnergyConsumption += energyOfRedLedPerTick;
} }
oldLedValue = newLedsValue; currentLedValue = newLedsValue;
return ledChanged; if (ledChanged) {
this.setChanged();
this.notifyObservers(mote);
}
} }
public JPanel getInterfaceVisualizer() { public JPanel getInterfaceVisualizer() {