Merge pull request #847 from ejoerns/pull-req/cooja-buttons

Cooja Button interface update
This commit is contained in:
Fredrik Österlind 2014-11-10 07:34:48 +01:00
commit 147e48af26
5 changed files with 175 additions and 279 deletions

View file

@ -30,13 +30,7 @@
package org.contikios.cooja.mspmote.interfaces; package org.contikios.cooja.mspmote.interfaces;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import org.contikios.cooja.*; import org.contikios.cooja.*;
import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.interfaces.Button;
@ -47,58 +41,28 @@ import org.contikios.cooja.mspmote.ESBMote;
*/ */
@ClassDescription("Button") @ClassDescription("Button")
public class ESBButton extends Button { public class ESBButton extends Button {
private static Logger logger = Logger.getLogger(ESBButton.class); private static final Logger logger = Logger.getLogger(ESBButton.class);
private ESBMote mote; private final ESBMote mote;
public ESBButton(Mote mote) { public ESBButton(Mote mote) {
super(mote);
this.mote = (ESBMote) mote; this.mote = (ESBMote) mote;
} }
public void clickButton() { @Override
pressButton(); protected void doReleaseButton() {
releaseButton();
}
public void releaseButton() {
mote.esbNode.setButton(false); mote.esbNode.setButton(false);
setChanged();
notifyObservers();
} }
public void pressButton() { @Override
protected void doPressButton() {
mote.esbNode.setButton(true); mote.esbNode.setButton(true);
setChanged();
notifyObservers();
} }
@Override
public boolean isPressed() { public boolean isPressed() {
return false; return false;
} }
public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel();
final JButton clickButton = new JButton("Click button");
panel.add(clickButton);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickButton();
}
});
return panel;
}
public void releaseInterfaceVisualizer(JPanel panel) {
}
public Collection<Element> getConfigXML() {
return null;
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
}
} }

View file

@ -28,16 +28,9 @@
*/ */
package org.contikios.cooja.mspmote.interfaces; package org.contikios.cooja.mspmote.interfaces;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.jdom.Element;
import org.contikios.cooja.ClassDescription; import org.contikios.cooja.ClassDescription;
import org.contikios.cooja.Mote; import org.contikios.cooja.Mote;
import org.contikios.cooja.Simulation; import org.contikios.cooja.Simulation;
import org.contikios.cooja.TimeEvent;
import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.interfaces.Button;
import org.contikios.cooja.mspmote.MspMote; import org.contikios.cooja.mspmote.MspMote;
@ -51,6 +44,7 @@ public class MspButton extends Button {
private final se.sics.mspsim.chip.Button button; private final se.sics.mspsim.chip.Button button;
public MspButton(Mote mote) { public MspButton(Mote mote) {
super(mote);
final MspMote mspMote = (MspMote) mote; final MspMote mspMote = (MspMote) mote;
sim = mote.getSimulation(); sim = mote.getSimulation();
button = mspMote.getCPU().getChip(se.sics.mspsim.chip.Button.class); button = mspMote.getCPU().getChip(se.sics.mspsim.chip.Button.class);
@ -60,26 +54,13 @@ public class MspButton extends Button {
} }
@Override @Override
public void clickButton() { protected void doPressButton() {
sim.invokeSimulationThread(new ButtonClick()); button.setPressed(true);
} }
@Override @Override
public void pressButton() { protected void doReleaseButton() {
sim.invokeSimulationThread(new Runnable() { button.setPressed(false);
public void run() {
button.setPressed(true);
}
});
}
@Override
public void releaseButton() {
sim.invokeSimulationThread(new Runnable() {
public void run() {
button.setPressed(false);
}
});
} }
@Override @Override
@ -87,50 +68,4 @@ public class MspButton extends Button {
return button.isPressed(); return button.isPressed();
} }
@Override
public JPanel getInterfaceVisualizer() {
final JPanel panel = new JPanel();
final JButton clickButton = new JButton("Click button");
panel.add(clickButton);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickButton();
}
});
return panel;
}
@Override
public void releaseInterfaceVisualizer(JPanel panel) {
}
@Override
public Collection<Element> getConfigXML() {
return null;
}
@Override
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
}
private class ButtonClick extends TimeEvent implements Runnable {
public ButtonClick() {
super(0);
}
@Override
public void run() {
button.setPressed(true);
sim.scheduleEvent(this, sim.getSimulationTime() + Simulation.MILLISECOND);
}
@Override
public void execute(long t) {
button.setPressed(false);
}
}
} }

View file

@ -30,102 +30,36 @@
package org.contikios.cooja.mspmote.interfaces; package org.contikios.cooja.mspmote.interfaces;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import org.contikios.cooja.ClassDescription; import org.contikios.cooja.ClassDescription;
import org.contikios.cooja.Mote; import org.contikios.cooja.Mote;
import org.contikios.cooja.Simulation;
import org.contikios.cooja.interfaces.Button; import org.contikios.cooja.interfaces.Button;
import org.contikios.cooja.mspmote.MspMoteTimeEvent;
import org.contikios.cooja.mspmote.SkyMote; import org.contikios.cooja.mspmote.SkyMote;
@ClassDescription("Button") @ClassDescription("Button")
public class SkyButton extends Button { public class SkyButton extends Button {
private static Logger logger = Logger.getLogger(SkyButton.class); private static final Logger logger = Logger.getLogger(SkyButton.class);
private final SkyMote skyMote;
private SkyMote skyMote;
private Simulation sim;
private MspMoteTimeEvent pressButtonEvent;
private MspMoteTimeEvent releaseButtonEvent;
public SkyButton(Mote mote) { public SkyButton(Mote mote) {
super(mote);
skyMote = (SkyMote) mote; skyMote = (SkyMote) mote;
sim = mote.getSimulation();
pressButtonEvent = new MspMoteTimeEvent((SkyMote)mote, 0) {
public void execute(long t) {
skyMote.skyNode.setButton(true);
}
};
releaseButtonEvent = new MspMoteTimeEvent((SkyMote)mote, 0) {
public void execute(long t) {
skyMote.skyNode.setButton(false);
}
};
} }
public void clickButton() { @Override
sim.invokeSimulationThread(new Runnable() { protected void doPressButton() {
public void run() { skyMote.skyNode.getButton().setPressed(true);
sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime());
sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND);
}
});
} }
public void pressButton() { @Override
sim.invokeSimulationThread(new Runnable() { protected void doReleaseButton() {
public void run() { skyMote.skyNode.getButton().setPressed(false);
sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime());
}
});
}
public void releaseButton() {
sim.invokeSimulationThread(new Runnable() {
public void run() {
sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime());
}
});
} }
@Override
public boolean isPressed() { public boolean isPressed() {
/* Not implemented */ return skyMote.skyNode.getButton().isPressed();
return false;
}
public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel();
final JButton clickButton = new JButton("Click button");
panel.add(clickButton);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickButton();
}
});
return panel;
}
public void releaseInterfaceVisualizer(JPanel panel) {
}
public Collection<Element> getConfigXML() {
return null;
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
} }
} }

View file

@ -30,11 +30,7 @@
package org.contikios.cooja.contikimote.interfaces; package org.contikios.cooja.contikimote.interfaces;
import java.awt.event.*;
import java.util.Collection;
import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import org.contikios.cooja.*; import org.contikios.cooja.*;
import org.contikios.cooja.contikimote.ContikiMote; import org.contikios.cooja.contikimote.ContikiMote;
@ -63,10 +59,10 @@ import org.contikios.cooja.mote.memory.VarMemory;
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public class ContikiButton extends Button implements ContikiMoteInterface { public class ContikiButton extends Button implements ContikiMoteInterface {
private VarMemory moteMem; private final VarMemory moteMem;
private ContikiMote mote; private final ContikiMote mote;
private static Logger logger = Logger.getLogger(ContikiButton.class); private static final Logger logger = Logger.getLogger(ContikiButton.class);
/** /**
* Creates an interface to the button at mote. * Creates an interface to the button at mote.
@ -76,6 +72,7 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
* @see org.contikios.cooja.MoteInterfaceHandler * @see org.contikios.cooja.MoteInterfaceHandler
*/ */
public ContikiButton(Mote mote) { public ContikiButton(Mote mote) {
super(mote);
this.mote = (ContikiMote) mote; this.mote = (ContikiMote) mote;
this.moteMem = new VarMemory(mote.getMemory()); this.moteMem = new VarMemory(mote.getMemory());
} }
@ -84,55 +81,8 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
return new String[]{"button_interface"}; return new String[]{"button_interface"};
} }
private TimeEvent pressButtonEvent = new MoteTimeEvent(mote, 0) { @Override
public void execute(long t) { protected void doReleaseButton() {
doPressButton();
}
};
private TimeEvent releaseButtonEvent = new MoteTimeEvent(mote, 0) {
public void execute(long t) {
/* Wait until button change is handled by Contiki */
if (moteMem.getByteValueOf("simButtonChanged") != 0) {
/* Postpone button release */
mote.getSimulation().scheduleEvent(releaseButtonEvent, t + Simulation.MILLISECOND);
return;
}
/*logger.info("Releasing button at: " + t);*/
doReleaseButton();
}
};
/**
* Clicks button: Presses and immediately releases button.
*/
public void clickButton() {
mote.getSimulation().invokeSimulationThread(new Runnable() {
public void run() {
mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime());
mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
}
});
}
public void pressButton() {
mote.getSimulation().invokeSimulationThread(new Runnable() {
public void run() {
mote.getSimulation().scheduleEvent(pressButtonEvent, mote.getSimulation().getSimulationTime());
}
});
}
public void releaseButton() {
mote.getSimulation().invokeSimulationThread(new Runnable() {
public void run() {
mote.getSimulation().scheduleEvent(releaseButtonEvent, mote.getSimulation().getSimulationTime());
}
});
}
private void doReleaseButton() {
moteMem.setByteValueOf("simButtonIsDown", (byte) 0); moteMem.setByteValueOf("simButtonIsDown", (byte) 0);
if (moteMem.getByteValueOf("simButtonIsActive") == 1) { if (moteMem.getByteValueOf("simButtonIsActive") == 1) {
@ -146,7 +96,8 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
} }
} }
private void doPressButton() { @Override
protected void doPressButton() {
moteMem.setByteValueOf("simButtonIsDown", (byte) 1); moteMem.setByteValueOf("simButtonIsDown", (byte) 1);
if (moteMem.getByteValueOf("simButtonIsActive") == 1) { if (moteMem.getByteValueOf("simButtonIsActive") == 1) {
@ -160,33 +111,9 @@ public class ContikiButton extends Button implements ContikiMoteInterface {
} }
} }
@Override
public boolean isPressed() { public boolean isPressed() {
return moteMem.getByteValueOf("simButtonIsDown") == 1; return moteMem.getByteValueOf("simButtonIsDown") == 1;
} }
public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel();
final JButton clickButton = new JButton("Click button");
panel.add(clickButton);
clickButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clickButton();
}
});
return panel;
}
public void releaseInterfaceVisualizer(JPanel panel) {
}
public Collection<Element> getConfigXML() {
return null;
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
}
} }

View file

@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2014, TU Braunschweig.
* Copyright (c) 2006, Swedish Institute of Computer Science. * Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
@ -30,7 +31,15 @@
package org.contikios.cooja.interfaces; package org.contikios.cooja.interfaces;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.contikios.cooja.*; import org.contikios.cooja.*;
import org.jdom.Element;
/** /**
* A Button represents a mote button. An implementation should notify all * A Button represents a mote button. An implementation should notify all
@ -42,24 +51,151 @@ import org.contikios.cooja.*;
@ClassDescription("Button") @ClassDescription("Button")
public abstract class Button extends MoteInterface { public abstract class Button extends MoteInterface {
private final Simulation sim;
private final MoteTimeEvent pressButtonEvent;
private final MoteTimeEvent releaseButtonEvent;
public Button(Mote mote) {
sim = mote.getSimulation();
pressButtonEvent = new MoteTimeEvent(mote, 0) {
@Override
public void execute(long t) {
doPressButton();
}
};
releaseButtonEvent = new MoteTimeEvent(mote, 0) {
@Override
public void execute(long t) {
doReleaseButton();
}
};
}
/** /**
* Clicks button. Button will be pressed for some time and then automatically * Clicks button. Button will be pressed for some time and then automatically
* released. * released.
*/ */
public abstract void clickButton(); public void clickButton() {
sim.invokeSimulationThread(new Runnable() {
@Override
public void run() {
sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime());
sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime() + Simulation.MILLISECOND);
}
});
}
/** /**
* Releases button (if pressed). * Presses button.
*/ */
public abstract void releaseButton(); public void pressButton() {
sim.invokeSimulationThread(new Runnable() {
@Override
public void run() {
sim.scheduleEvent(pressButtonEvent, sim.getSimulationTime());
}
});
}
/** /**
* Presses button (if not already pressed). * Node-type dependent implementation of pressing a button.
*/ */
public abstract void pressButton(); protected abstract void doPressButton();
/**
* Releases button.
*/
public void releaseButton() {
sim.invokeSimulationThread(new Runnable() {
@Override
public void run() {
sim.scheduleEvent(releaseButtonEvent, sim.getSimulationTime());
}
});
}
/**
* Node-type dependent implementation of releasing a button.
*/
protected abstract void doReleaseButton();
/** /**
* @return True if button is pressed * @return True if button is pressed
*/ */
public abstract boolean isPressed(); public abstract boolean isPressed();
@Override
public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel();
final JButton clickButton = new JButton("Click button");
panel.add(clickButton);
clickButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
sim.invokeSimulationThread(new Runnable() {
@Override
public void run() {
doPressButton();
}
});
}
@Override
public void mouseReleased(MouseEvent e) {
sim.invokeSimulationThread(new Runnable() {
@Override
public void run() {
doReleaseButton();
}
});
}
});
clickButton.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
sim.invokeSimulationThread(new Runnable() {
@Override
public void run() {
doPressButton();
}
});
}
@Override
public void keyReleased(KeyEvent e) {
sim.invokeSimulationThread(new Runnable() {
@Override
public void run() {
doReleaseButton();
}
});
}
});
return panel;
}
@Override
public void releaseInterfaceVisualizer(JPanel panel) {
}
@Override
public Collection<Element> getConfigXML() {
// The button state will not be saved!
return null;
}
@Override
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
// The button state will not be saved!
}
} }