From: <ni...@us...> - 2009-10-26 23:03:35
|
Revision: 635 http://mspsim.svn.sourceforge.net/mspsim/?rev=635&view=rev Author: nifi Date: 2009-10-26 23:03:19 +0000 (Mon, 26 Oct 2009) Log Message: ----------- added base class for node gui and fixed ESBGui to work with managed windows Modified Paths: -------------- mspsim/se/sics/mspsim/platform/esb/ESBGui.java mspsim/se/sics/mspsim/platform/esb/ESBNode.java mspsim/se/sics/mspsim/platform/sky/SkyGui.java Added Paths: ----------- mspsim/se/sics/mspsim/platform/AbstractNodeGUI.java Added: mspsim/se/sics/mspsim/platform/AbstractNodeGUI.java =================================================================== --- mspsim/se/sics/mspsim/platform/AbstractNodeGUI.java (rev 0) +++ mspsim/se/sics/mspsim/platform/AbstractNodeGUI.java 2009-10-26 23:03:19 UTC (rev 635) @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2009, 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. + * + * $Id$ + * + * ----------------------------------------------------------------- + * + * AbstractNodeGUI + * + * Authors : Joakim Eriksson, Niclas Finne + * Created : 9 okt 2009 + * Updated : $Date$ + * $Revision$ + */ + +package se.sics.mspsim.platform; +import java.awt.Color; +import java.awt.Dimension; +import java.io.File; +import java.net.URL; + +import javax.swing.ImageIcon; +import javax.swing.JComponent; + +import se.sics.mspsim.ui.ManagedWindow; +import se.sics.mspsim.ui.WindowManager; +import se.sics.mspsim.util.ComponentRegistry; +import se.sics.mspsim.util.ServiceComponent; + +public abstract class AbstractNodeGUI extends JComponent implements ServiceComponent { + + private static final long serialVersionUID = 1435276301923987019L; + + private final String nodeImageName; + + private String name; + private ComponentRegistry registry; + + private ImageIcon nodeImage; + private ManagedWindow window; + + private ServiceComponent.Status status = Status.STOPPED; + + protected AbstractNodeGUI(String imageName) { + this.nodeImageName = imageName; + setBackground(Color.black); + setOpaque(true); + } + + public Status getStatus() { + return status; + } + + public String getName() { + return name; + } + + protected ImageIcon getNodeImage() { + return nodeImage; + } + + protected ComponentRegistry getRegistry() { + return registry; + } + + public final void init(String name, ComponentRegistry registry) { + this.name = name; + this.registry = registry; + } + + public final void start() { + File fp = new File(nodeImageName); + if (!fp.canRead()) { + URL imageURL = getImageURL(nodeImageName); + if (imageURL == null + && !nodeImageName.startsWith("images/") + && !nodeImageName.startsWith("/images/")) { + imageURL = getImageURL("images/" + nodeImageName); + } + if (imageURL != null) { + nodeImage = new ImageIcon(imageURL); + } else { + throw new IllegalStateException("image not found: " + nodeImageName); + } + } else { + nodeImage = new ImageIcon(nodeImageName); + } + if (nodeImage.getIconWidth() == 0 || nodeImage.getIconHeight() == 0) { + // Image not found + throw new IllegalStateException("image not found: " + nodeImageName); + } + setPreferredSize(new Dimension(nodeImage.getIconWidth(), + nodeImage.getIconHeight())); + + WindowManager wm = (WindowManager) registry.getComponent("windowManager"); + window = wm.createWindow("NodeGUI"); + window.add(this); + + startGUI(); + + status = Status.STARTED; + window.setVisible(true); + } + + private URL getImageURL(String image) { + URL imageURL = getClass().getResource(image); + if (imageURL == null && !image.startsWith("/")) { + imageURL = getClass().getResource("/" + image); + } + return imageURL; + } + + public final void stop() { + status = Status.STOPPED; + stopGUI(); + } + + protected abstract void startGUI(); + + protected abstract void stopGUI(); + +} Property changes on: mspsim/se/sics/mspsim/platform/AbstractNodeGUI.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + LF Modified: mspsim/se/sics/mspsim/platform/esb/ESBGui.java =================================================================== --- mspsim/se/sics/mspsim/platform/esb/ESBGui.java 2009-10-26 22:51:29 UTC (rev 634) +++ mspsim/se/sics/mspsim/platform/esb/ESBGui.java 2009-10-26 23:03:19 UTC (rev 635) @@ -41,38 +41,36 @@ package se.sics.mspsim.platform.esb; import java.awt.Color; -import java.awt.Dimension; import java.awt.Graphics; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; +import java.awt.Rectangle; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; -import java.net.URL; + import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.TargetDataLine; import javax.swing.ImageIcon; -import javax.swing.JComponent; -import javax.swing.JFrame; + import se.sics.mspsim.chip.Beeper; import se.sics.mspsim.core.ADC12; import se.sics.mspsim.core.ADCInput; import se.sics.mspsim.core.IOUnit; import se.sics.mspsim.core.MSP430; import se.sics.mspsim.core.USART; +import se.sics.mspsim.platform.AbstractNodeGUI; import se.sics.mspsim.ui.SerialMon; -import se.sics.mspsim.ui.WindowUtils; -public class ESBGui extends JComponent implements ADCInput { +public class ESBGui extends AbstractNodeGUI implements ADCInput { private static final long serialVersionUID = -139331418649524704L; - public static final int GREEN_X = 3; - public static final int YELLOW_X = 10; - public static final int RED_X = 17; + public static final int GREEN_X = 2; + public static final int YELLOW_X = 9; + public static final int RED_X = 16; public static final int LED_Y = 4; + private static final Rectangle LED_BOUNDS = new Rectangle(GREEN_X - 2, LED_Y - 4, RED_X + 6, LED_Y + 10); public static final Color RED_TRANS = new Color(0xff,0x40,0x40,0xa0); public static final Color YELLOW_TRANS = new Color(0xff, 0xff, 0x00, 0xa0); @@ -85,11 +83,12 @@ private static final float SAMPLE_RATE = 22050; private static final int DL_BUFFER_SIZE = 2200; + private MouseMotionAdapter mouseMotionListener; + private MouseAdapter mouseListener; + private SerialMon serial; Beeper beeper; - private ImageIcon esbImage; - private JFrame window; private ESBNode node; private boolean buttonDown = false; private boolean resetDown = false; @@ -97,45 +96,13 @@ private TargetDataLine inDataLine; public ESBGui(ESBNode esbNode) { - this.node = esbNode; + super("images/esb.jpg"); + this.node = esbNode; + } - setBackground(Color.black); - setOpaque(true); - - URL imageURL = ESBGui.class.getResource("images/esb.jpg"); - if (imageURL == null) { - imageURL = ESBGui.class.getResource("/images/esb.jpg"); - } - if (imageURL != null) { - esbImage = new ImageIcon(imageURL); - } else { - esbImage = new ImageIcon("images/esb.jpg"); - } - if (esbImage.getIconWidth() == 0 || esbImage.getIconHeight() == 0) { - // Image not found - throw new IllegalStateException("image not found"); - } - setPreferredSize(new Dimension(esbImage.getIconWidth(), - esbImage.getIconHeight())); - - window = new JFrame("ESB"); -// window.setSize(190,240); - window.add(this); - WindowUtils.restoreWindowBounds("ESBGui", window); - WindowUtils.addSaveOnShutdown("ESBGui", window); - window.setVisible(true); - - window.addKeyListener(new KeyAdapter() { - - public void keyPressed(KeyEvent key) { - if (key.getKeyChar() == 'd') { - node.setDebug(!node.getDebug()); - } - } - - }); - - addMouseMotionListener(new MouseMotionAdapter() { + @Override + protected void startGUI() { + mouseMotionListener = new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { // System.out.println("Mouse moved: " + e.getX() + "," + e.getY()); int x = e.getX(); @@ -143,46 +110,62 @@ node.setPIR(x > 18 && x < 80 && y > 35 && y < 100); node.setVIB(x > 62 && x < 95 && y > 160 && y < 178); } - }); - addMouseListener(new MouseAdapter() { + }; + addMouseMotionListener(mouseMotionListener); + + mouseListener = new MouseAdapter() { // For the button sensor on the ESB nodes. public void mousePressed(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); - if (y > 152 && y < 168) { - if (x > 0 && x < 19) { - node.setButton(buttonDown = true); - } else { - int w = esbImage.getIconWidth(); - if (x > w - 20 && x < w) { - resetDown = true; + if (e.getButton() == MouseEvent.BUTTON1) { + int x = e.getX(); + int y = e.getY(); +// System.err.println("PRESSED AT " + x + "," + y +// + " IMAGE=" + getNodeImage().getIconWidth() +// + "x" + getNodeImage().getIconHeight() + +// " SIZE=" + getWidth() + "x" + getHeight()); + + if (y > 152 && y < 168) { + if (x > 0 && x < 19) { + buttonDown = true; + node.setButton(true); + } else { + int w = getNodeImage().getIconWidth(); + if (x > w - 20 && x < w) { + resetDown = true; + } } } } } public void mouseReleased(MouseEvent e) { - if (buttonDown) { - node.setButton(buttonDown = false); - } else if (resetDown) { - int x = e.getX(); - int y = e.getY(); - if (y > 152 && y < 168) { - int w = esbImage.getIconWidth(); - if (x > w - 20 && x < w) { - node.getCPU().reset(); + if (e.getButton() == MouseEvent.BUTTON1) { + if (buttonDown) { + buttonDown = false; + node.setButton(false); + } else if (resetDown) { + int x = e.getX(); + int y = e.getY(); + if (y > 152 && y < 168) { + int w = getNodeImage().getIconWidth(); + if (x > w - 20 && x < w) { + node.getCPU().reset(); + } } + resetDown = false; } - resetDown = false; } } - }); + }; + addMouseListener(mouseListener); // Add some windows for listening to serial output MSP430 cpu = node.getCPU(); IOUnit usart = cpu.getIOUnit("USART 1"); if (usart instanceof USART) { - serial = new SerialMon((USART)usart, "RS232 Port Output"); + if (serial == null) { + serial = new SerialMon((USART)usart, "RS232 Port Output"); + } ((USART) usart).setUSARTListener(serial); } @@ -190,31 +173,41 @@ if (adc instanceof ADC12) { ((ADC12) adc).setADCInput(0, this); } - + beeper = new Beeper(); - - // Just a test... TODO: remove!!! - AudioFormat af = new AudioFormat(SAMPLE_RATE, 16, 1, true, false); - DataLine.Info dlin = - new DataLine.Info(TargetDataLine.class, af, DL_BUFFER_SIZE); + // Just a test... TODO: remove!!! try { + AudioFormat af = new AudioFormat(SAMPLE_RATE, 16, 1, true, false); + DataLine.Info dlin = + new DataLine.Info(TargetDataLine.class, af, DL_BUFFER_SIZE); inDataLine = (TargetDataLine) AudioSystem.getLine(dlin); if (inDataLine == null) { - System.out.println("No in dataline"); + System.out.println("No input dataline"); } else { System.out.println("Format: " + inDataLine.getFormat()); inDataLine.open(inDataLine.getFormat(), DL_BUFFER_SIZE); inDataLine.start(); } } catch (Exception e) { - System.out.println("Problem while getting data line "); - e.printStackTrace(); + System.err.println("Failed to get audio data line: " + e.getMessage()); } } - byte[] data = new byte[4]; + @Override + protected void stopGUI() { + removeMouseMotionListener(mouseMotionListener); + removeMouseListener(mouseListener); + + // TODO cleanup + } + + public void ledsChanged() { + repaint(LED_BOUNDS); + } + + private byte[] data = new byte[4]; public int nextData() { if (inDataLine != null) { inDataLine.read(data, 0, 4); @@ -226,6 +219,7 @@ protected void paintComponent(Graphics g) { Color old = g.getColor(); int w = getWidth(), h = getHeight(); + ImageIcon esbImage = getNodeImage(); int iw = esbImage.getIconWidth(), ih = esbImage.getIconHeight(); esbImage.paintIcon(this, g, 0, 0); // Clear all areas not covered by the image Modified: mspsim/se/sics/mspsim/platform/esb/ESBNode.java =================================================================== --- mspsim/se/sics/mspsim/platform/esb/ESBNode.java 2009-10-26 22:51:29 UTC (rev 634) +++ mspsim/se/sics/mspsim/platform/esb/ESBNode.java 2009-10-26 23:03:19 UTC (rev 635) @@ -51,6 +51,7 @@ import se.sics.mspsim.extutil.jfreechart.DataSourceSampler; import se.sics.mspsim.platform.GenericNode; import se.sics.mspsim.util.ArgumentManager; +import se.sics.mspsim.util.OperatingModeStatistics; public class ESBNode extends GenericNode implements PortListener { @@ -75,9 +76,8 @@ public boolean yellowLed; private TR1001 radio; - public ESBGui gui; + private ESBGui gui; - /** * Creates a new <code>ESBNode</code> instance. * @@ -109,16 +109,10 @@ if (source == port2) { // System.out.println("ESBNode.PORT2: 0x" + Integer.toString(data,16)); redLed = (data & RED_LED) == 0; - if (DEBUG && greenLed != ((data & GREEN_LED) == 0)) { - System.out.println("Green toggled!"); - } greenLed = (data & GREEN_LED) == 0; - if (DEBUG && yellowLed != ((data & YELLOW_LED) == 0)) { - System.out.println("Yellow toggled!"); - } yellowLed = (data & YELLOW_LED) == 0; if (gui != null) { - gui.repaint(); + gui.ledsChanged(); gui.beeper.beepOn((data & BEEPER) != 0); } @@ -167,10 +161,13 @@ if (!config.getPropertyAsBoolean("nogui", true)) { gui = new ESBGui(this); + registry.registerComponent("nodegui", gui); // A HACK for some "graphs"!!! DataChart dataChart = new DataChart(registry, "Duty Cycle", "Duty Cycle"); + registry.registerComponent("dutychart", dataChart); DataSourceSampler dss = dataChart.setupChipFrame(cpu); + dataChart.addDataSource(dss, "LEDS", stats.getDataSource(getName(), 0, OperatingModeStatistics.OP_INVERT)); dataChart.addDataSource(dss, "Listen", stats.getDataSource("TR1001", TR1001.MODE_RX_ON)); dataChart.addDataSource(dss, "Transmit", stats.getDataSource("TR1001", TR1001.MODE_TXRX_ON)); dataChart.addDataSource(dss, "CPU", stats.getDataSource("MSP430 Core", MSP430.MODE_ACTIVE)); Modified: mspsim/se/sics/mspsim/platform/sky/SkyGui.java =================================================================== --- mspsim/se/sics/mspsim/platform/sky/SkyGui.java 2009-10-26 22:51:29 UTC (rev 634) +++ mspsim/se/sics/mspsim/platform/sky/SkyGui.java 2009-10-26 23:03:19 UTC (rev 635) @@ -41,31 +41,22 @@ package se.sics.mspsim.platform.sky; import java.awt.Color; -import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.net.URL; + import javax.swing.ImageIcon; -import javax.swing.JComponent; + import se.sics.mspsim.core.IOUnit; import se.sics.mspsim.core.MSP430; import se.sics.mspsim.core.USART; -import se.sics.mspsim.ui.ManagedWindow; +import se.sics.mspsim.platform.AbstractNodeGUI; import se.sics.mspsim.ui.SerialMon; -import se.sics.mspsim.ui.WindowManager; -import se.sics.mspsim.util.ComponentRegistry; -import se.sics.mspsim.util.ServiceComponent; -public class SkyGui extends JComponent implements ServiceComponent { +public class SkyGui extends AbstractNodeGUI { - /** - * - */ private static final long serialVersionUID = 7753659717805292786L; - private ServiceComponent.Status status = Status.STOPPED; - public static final int GREEN_Y = 40; public static final int BLUE_Y = 46; public static final int RED_Y = 34; @@ -81,47 +72,14 @@ private SerialMon serial; - private ImageIcon skyImage; - private ManagedWindow window; private MoteIVNode node; - private ComponentRegistry registry; - - private String name; - public SkyGui(MoteIVNode node) { + super("images/sky.jpg"); this.node = node; } - public String getName() { - return name; - } - - public void start() { - setBackground(Color.black); - setOpaque(true); - - URL imageURL = SkyGui.class.getResource("images/sky.jpg"); - if (imageURL == null) { - imageURL = SkyGui.class.getResource("/images/sky.jpg"); - } - if (imageURL != null) { - skyImage = new ImageIcon(imageURL); - } else { - skyImage = new ImageIcon("images/sky.jpg"); - } - if (skyImage.getIconWidth() == 0 || skyImage.getIconHeight() == 0) { - // Image not found - throw new IllegalStateException("image not found"); - } - setPreferredSize(new Dimension(skyImage.getIconWidth(), - skyImage.getIconHeight())); - - WindowManager wm = (WindowManager) registry.getComponent("windowManager"); - window = wm.createWindow("SkyGui"); - window.add(this); - window.setVisible(true); - + protected void startGUI() { MouseAdapter mouseHandler = new MouseAdapter() { private boolean buttonDown = false; @@ -166,11 +124,11 @@ serial = new SerialMon((USART)usart, "USART1 Port Output"); ((USART) usart).setUSARTListener(serial); } - status = Status.STARTED; } protected void paintComponent(Graphics g) { Color old = g.getColor(); + ImageIcon skyImage = getNodeImage(); int w = getWidth(), h = getHeight(); int iw = skyImage.getIconWidth(), ih = skyImage.getIconHeight(); skyImage.paintIcon(this, g, 0, 0); @@ -205,16 +163,7 @@ g.setColor(old); } - public Status getStatus() { - return status; + protected void stopGUI() { } - public void init(String name, ComponentRegistry registry) { - this.name = name; - this.registry = registry; - } - - public void stop() { - } - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |