diff --git a/se/sics/mspsim/chip/Button.java b/se/sics/mspsim/chip/Button.java new file mode 100644 index 0000000..9b01d04 --- /dev/null +++ b/se/sics/mspsim/chip/Button.java @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2012, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of MSPSim. + * + */ +package se.sics.mspsim.chip; +import se.sics.mspsim.core.Chip; +import se.sics.mspsim.core.IOPort; +import se.sics.mspsim.core.MSP430Core; + +/** + * @author Niclas Finne + * + */ +public class Button extends Chip { + + private final IOPort port; + private final int pin; + private final boolean polarity; + private boolean isPressed; + + public Button(String id, MSP430Core cpu, IOPort port, int pin, boolean polarity) { + super(id, cpu); + this.port = port; + this.pin = pin; + this.polarity = polarity; + } + + public boolean isPressed() { + return isPressed; + } + + public void setPressed(boolean isPressed) { + if (this.isPressed != isPressed) { + this.isPressed = isPressed; + stateChanged(isPressed ? 1 : 0); + if (DEBUG) log(isPressed ? "pressed" : "released"); + port.setPinState(pin, isPressed == polarity ? IOPort.PIN_HI : IOPort.PIN_LOW); + } + } + + @Override + public int getConfiguration(int parameter) { + return 0; + } + + @Override + public int getModeMax() { + return 0; + } + + @Override + public String info() { + return id + ": " + (isPressed ? "pressed" : "released"); + } +} diff --git a/se/sics/mspsim/platform/esb/ESBGui.java b/se/sics/mspsim/platform/esb/ESBGui.java index 8213224..5d937cc 100644 --- a/se/sics/mspsim/platform/esb/ESBGui.java +++ b/se/sics/mspsim/platform/esb/ESBGui.java @@ -27,16 +27,12 @@ * * This file is part of MSPSim. * - * $Id$ - * * ----------------------------------------------------------------- * * ESBGui * * Author : Joakim Eriksson * Created : Sun Oct 21 22:00:00 2007 - * Updated : $Date$ - * $Revision$ */ package se.sics.mspsim.platform.esb; @@ -124,7 +120,7 @@ public class ESBGui extends AbstractNodeGUI implements ADCInput { if (y > 152 && y < 168) { if (x > 0 && x < 19) { buttonDown = true; - node.setButton(true); + node.getButton().setPressed(true); } else { int w = getNodeImage().getIconWidth(); if (x > w - 20 && x < w) { @@ -139,7 +135,7 @@ public class ESBGui extends AbstractNodeGUI implements ADCInput { if (e.getButton() == MouseEvent.BUTTON1) { if (buttonDown) { buttonDown = false; - node.setButton(false); + node.getButton().setPressed(false); } else if (resetDown) { int x = e.getX(); int y = e.getY(); diff --git a/se/sics/mspsim/platform/esb/ESBNode.java b/se/sics/mspsim/platform/esb/ESBNode.java index 935e650..8131b51 100644 --- a/se/sics/mspsim/platform/esb/ESBNode.java +++ b/se/sics/mspsim/platform/esb/ESBNode.java @@ -27,22 +27,19 @@ * * This file is part of MSPSim. * - * $Id$ - * * ----------------------------------------------------------------- * * ESBNode * * Author : Joakim Eriksson * Created : Sun Oct 21 22:00:00 2007 - * Updated : $Date$ - * $Revision$ */ package se.sics.mspsim.platform.esb; import java.io.IOException; import se.sics.mspsim.chip.Beeper; +import se.sics.mspsim.chip.Button; import se.sics.mspsim.chip.Leds; import se.sics.mspsim.chip.TR1001; import se.sics.mspsim.config.MSP430f149Config; @@ -80,6 +77,8 @@ public class ESBNode extends GenericNode implements PortListener { public boolean greenLed; public boolean yellowLed; + private Button button; + private TR1001 radio; private Beeper beeper; private ESBGui gui; @@ -96,6 +95,10 @@ public class ESBNode extends GenericNode implements PortListener { return leds; } + public Button getButton() { + return button; + } + public Beeper getBeeper() { return beeper; } @@ -108,8 +111,9 @@ public class ESBNode extends GenericNode implements PortListener { port1.setPinState(VIB_PIN, hi ? IOPort.PIN_HI : IOPort.PIN_LOW); } - public void setButton(boolean hi) { - port2.setPinState(BUTTON_PIN, hi ? IOPort.PIN_HI : IOPort.PIN_LOW); + @Deprecated + public void setButton(boolean buttonPressed) { + button.setPressed(buttonPressed); } public boolean getDebug() { @@ -155,6 +159,7 @@ public class ESBNode extends GenericNode implements PortListener { radio = new TR1001(cpu, usart0); leds = new Leds(cpu, LEDS); + button = new Button("Button", cpu, port2, BUTTON_PIN, true); beeper = new Beeper(cpu); } diff --git a/se/sics/mspsim/platform/sky/MoteIVNode.java b/se/sics/mspsim/platform/sky/MoteIVNode.java index 56c3ca2..4b2296e 100644 --- a/se/sics/mspsim/platform/sky/MoteIVNode.java +++ b/se/sics/mspsim/platform/sky/MoteIVNode.java @@ -1,4 +1,5 @@ package se.sics.mspsim.platform.sky; +import se.sics.mspsim.chip.Button; import se.sics.mspsim.chip.Leds; import se.sics.mspsim.chip.SHT11; import se.sics.mspsim.core.IOPort; @@ -29,6 +30,7 @@ public abstract class MoteIVNode extends CC2420Node { public boolean greenLed; private Leds leds; + private Button button; public SHT11 sht11; public SkyGui gui; @@ -42,18 +44,22 @@ public abstract class MoteIVNode extends CC2420Node { return leds; } - public void setButton(boolean hi) { - port2.setPinState(BUTTON_PIN, hi ? IOPort.PIN_HI : IOPort.PIN_LOW); + public Button getButton() { + return button; + } + + @Deprecated + public void setButton(boolean buttonPressed) { + button.setPressed(buttonPressed); } public void setupNodePorts() { super.setupNodePorts(); leds = new Leds(cpu, LEDS); + button = new Button("Button", cpu, port2, BUTTON_PIN, true); sht11 = new SHT11(cpu); - if (port1 != null) { - sht11.setDataPort(port1, SHT11_DATA_PIN); - } + sht11.setDataPort(port1, SHT11_DATA_PIN); } public void setupGUI() { diff --git a/se/sics/mspsim/platform/sky/SkyGui.java b/se/sics/mspsim/platform/sky/SkyGui.java index 2673719..f2d873e 100644 --- a/se/sics/mspsim/platform/sky/SkyGui.java +++ b/se/sics/mspsim/platform/sky/SkyGui.java @@ -93,7 +93,7 @@ public class SkyGui extends AbstractNodeGUI { if (x > 122 && x < 135) { if (y > 41 && y < 55) { buttonDown = true; - SkyGui.this.node.setButton(true); + SkyGui.this.node.getButton().setPressed(true); } else if (y > 72 && y < 85) { resetDown = true; } @@ -103,7 +103,7 @@ public class SkyGui extends AbstractNodeGUI { public void mouseReleased(MouseEvent e) { if (buttonDown) { buttonDown = false; - SkyGui.this.node.setButton(false); + SkyGui.this.node.getButton().setPressed(false); } else if (resetDown) { int x = e.getX();