mirror of https://github.com/contiki-ng/mspsim
Added abstraction for user button
This commit is contained in:
parent
c4a91ce25c
commit
a4bdb2b864
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -27,16 +27,12 @@
|
||||||
*
|
*
|
||||||
* This file is part of MSPSim.
|
* This file is part of MSPSim.
|
||||||
*
|
*
|
||||||
* $Id$
|
|
||||||
*
|
|
||||||
* -----------------------------------------------------------------
|
* -----------------------------------------------------------------
|
||||||
*
|
*
|
||||||
* ESBGui
|
* ESBGui
|
||||||
*
|
*
|
||||||
* Author : Joakim Eriksson
|
* Author : Joakim Eriksson
|
||||||
* Created : Sun Oct 21 22:00:00 2007
|
* Created : Sun Oct 21 22:00:00 2007
|
||||||
* Updated : $Date$
|
|
||||||
* $Revision$
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.sics.mspsim.platform.esb;
|
package se.sics.mspsim.platform.esb;
|
||||||
|
|
@ -124,7 +120,7 @@ public class ESBGui extends AbstractNodeGUI implements ADCInput {
|
||||||
if (y > 152 && y < 168) {
|
if (y > 152 && y < 168) {
|
||||||
if (x > 0 && x < 19) {
|
if (x > 0 && x < 19) {
|
||||||
buttonDown = true;
|
buttonDown = true;
|
||||||
node.setButton(true);
|
node.getButton().setPressed(true);
|
||||||
} else {
|
} else {
|
||||||
int w = getNodeImage().getIconWidth();
|
int w = getNodeImage().getIconWidth();
|
||||||
if (x > w - 20 && x < w) {
|
if (x > w - 20 && x < w) {
|
||||||
|
|
@ -139,7 +135,7 @@ public class ESBGui extends AbstractNodeGUI implements ADCInput {
|
||||||
if (e.getButton() == MouseEvent.BUTTON1) {
|
if (e.getButton() == MouseEvent.BUTTON1) {
|
||||||
if (buttonDown) {
|
if (buttonDown) {
|
||||||
buttonDown = false;
|
buttonDown = false;
|
||||||
node.setButton(false);
|
node.getButton().setPressed(false);
|
||||||
} else if (resetDown) {
|
} else if (resetDown) {
|
||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
int y = e.getY();
|
int y = e.getY();
|
||||||
|
|
|
||||||
|
|
@ -27,22 +27,19 @@
|
||||||
*
|
*
|
||||||
* This file is part of MSPSim.
|
* This file is part of MSPSim.
|
||||||
*
|
*
|
||||||
* $Id$
|
|
||||||
*
|
|
||||||
* -----------------------------------------------------------------
|
* -----------------------------------------------------------------
|
||||||
*
|
*
|
||||||
* ESBNode
|
* ESBNode
|
||||||
*
|
*
|
||||||
* Author : Joakim Eriksson
|
* Author : Joakim Eriksson
|
||||||
* Created : Sun Oct 21 22:00:00 2007
|
* Created : Sun Oct 21 22:00:00 2007
|
||||||
* Updated : $Date$
|
|
||||||
* $Revision$
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.sics.mspsim.platform.esb;
|
package se.sics.mspsim.platform.esb;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import se.sics.mspsim.chip.Beeper;
|
import se.sics.mspsim.chip.Beeper;
|
||||||
|
import se.sics.mspsim.chip.Button;
|
||||||
import se.sics.mspsim.chip.Leds;
|
import se.sics.mspsim.chip.Leds;
|
||||||
import se.sics.mspsim.chip.TR1001;
|
import se.sics.mspsim.chip.TR1001;
|
||||||
import se.sics.mspsim.config.MSP430f149Config;
|
import se.sics.mspsim.config.MSP430f149Config;
|
||||||
|
|
@ -80,6 +77,8 @@ public class ESBNode extends GenericNode implements PortListener {
|
||||||
public boolean greenLed;
|
public boolean greenLed;
|
||||||
public boolean yellowLed;
|
public boolean yellowLed;
|
||||||
|
|
||||||
|
private Button button;
|
||||||
|
|
||||||
private TR1001 radio;
|
private TR1001 radio;
|
||||||
private Beeper beeper;
|
private Beeper beeper;
|
||||||
private ESBGui gui;
|
private ESBGui gui;
|
||||||
|
|
@ -96,6 +95,10 @@ public class ESBNode extends GenericNode implements PortListener {
|
||||||
return leds;
|
return leds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Button getButton() {
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
|
||||||
public Beeper getBeeper() {
|
public Beeper getBeeper() {
|
||||||
return beeper;
|
return beeper;
|
||||||
}
|
}
|
||||||
|
|
@ -108,8 +111,9 @@ public class ESBNode extends GenericNode implements PortListener {
|
||||||
port1.setPinState(VIB_PIN, hi ? IOPort.PIN_HI : IOPort.PIN_LOW);
|
port1.setPinState(VIB_PIN, hi ? IOPort.PIN_HI : IOPort.PIN_LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setButton(boolean hi) {
|
@Deprecated
|
||||||
port2.setPinState(BUTTON_PIN, hi ? IOPort.PIN_HI : IOPort.PIN_LOW);
|
public void setButton(boolean buttonPressed) {
|
||||||
|
button.setPressed(buttonPressed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getDebug() {
|
public boolean getDebug() {
|
||||||
|
|
@ -155,6 +159,7 @@ public class ESBNode extends GenericNode implements PortListener {
|
||||||
radio = new TR1001(cpu, usart0);
|
radio = new TR1001(cpu, usart0);
|
||||||
|
|
||||||
leds = new Leds(cpu, LEDS);
|
leds = new Leds(cpu, LEDS);
|
||||||
|
button = new Button("Button", cpu, port2, BUTTON_PIN, true);
|
||||||
beeper = new Beeper(cpu);
|
beeper = new Beeper(cpu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
package se.sics.mspsim.platform.sky;
|
package se.sics.mspsim.platform.sky;
|
||||||
|
import se.sics.mspsim.chip.Button;
|
||||||
import se.sics.mspsim.chip.Leds;
|
import se.sics.mspsim.chip.Leds;
|
||||||
import se.sics.mspsim.chip.SHT11;
|
import se.sics.mspsim.chip.SHT11;
|
||||||
import se.sics.mspsim.core.IOPort;
|
import se.sics.mspsim.core.IOPort;
|
||||||
|
|
@ -29,6 +30,7 @@ public abstract class MoteIVNode extends CC2420Node {
|
||||||
public boolean greenLed;
|
public boolean greenLed;
|
||||||
|
|
||||||
private Leds leds;
|
private Leds leds;
|
||||||
|
private Button button;
|
||||||
public SHT11 sht11;
|
public SHT11 sht11;
|
||||||
|
|
||||||
public SkyGui gui;
|
public SkyGui gui;
|
||||||
|
|
@ -42,18 +44,22 @@ public abstract class MoteIVNode extends CC2420Node {
|
||||||
return leds;
|
return leds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setButton(boolean hi) {
|
public Button getButton() {
|
||||||
port2.setPinState(BUTTON_PIN, hi ? IOPort.PIN_HI : IOPort.PIN_LOW);
|
return button;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public void setButton(boolean buttonPressed) {
|
||||||
|
button.setPressed(buttonPressed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupNodePorts() {
|
public void setupNodePorts() {
|
||||||
super.setupNodePorts();
|
super.setupNodePorts();
|
||||||
|
|
||||||
leds = new Leds(cpu, LEDS);
|
leds = new Leds(cpu, LEDS);
|
||||||
|
button = new Button("Button", cpu, port2, BUTTON_PIN, true);
|
||||||
sht11 = new SHT11(cpu);
|
sht11 = new SHT11(cpu);
|
||||||
if (port1 != null) {
|
sht11.setDataPort(port1, SHT11_DATA_PIN);
|
||||||
sht11.setDataPort(port1, SHT11_DATA_PIN);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupGUI() {
|
public void setupGUI() {
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ public class SkyGui extends AbstractNodeGUI {
|
||||||
if (x > 122 && x < 135) {
|
if (x > 122 && x < 135) {
|
||||||
if (y > 41 && y < 55) {
|
if (y > 41 && y < 55) {
|
||||||
buttonDown = true;
|
buttonDown = true;
|
||||||
SkyGui.this.node.setButton(true);
|
SkyGui.this.node.getButton().setPressed(true);
|
||||||
} else if (y > 72 && y < 85) {
|
} else if (y > 72 && y < 85) {
|
||||||
resetDown = true;
|
resetDown = true;
|
||||||
}
|
}
|
||||||
|
|
@ -103,7 +103,7 @@ public class SkyGui extends AbstractNodeGUI {
|
||||||
public void mouseReleased(MouseEvent e) {
|
public void mouseReleased(MouseEvent e) {
|
||||||
if (buttonDown) {
|
if (buttonDown) {
|
||||||
buttonDown = false;
|
buttonDown = false;
|
||||||
SkyGui.this.node.setButton(false);
|
SkyGui.this.node.getButton().setPressed(false);
|
||||||
|
|
||||||
} else if (resetDown) {
|
} else if (resetDown) {
|
||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue