repaint labels periodically instead of every time data is sent over the serial connection

This commit is contained in:
Fredrik Osterlind 2013-08-14 13:04:37 +02:00
parent 8a084926e2
commit f59040375b

View file

@ -30,6 +30,8 @@
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
@ -45,6 +47,7 @@ import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.Timer;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
@ -91,6 +94,8 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
super("Serial Socket (SERVER) (" + mote + ")", gui, false); super("Serial Socket (SERVER) (" + mote + ")", gui, false);
this.mote = mote; this.mote = mote;
updateTimer.start();
LISTEN_PORT = 60000 + mote.getID(); LISTEN_PORT = 60000 + mote.getID();
/* GUI components */ /* GUI components */
@ -156,12 +161,11 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
/*logger.debug("out is null");*/ /*logger.debug("out is null");*/
return; return;
} }
out.write(serialPort.getLastSerialData()); out.write(serialPort.getLastSerialData());
out.flush(); out.flush();
outBytes++; outBytes++;
if (GUI.isVisualized()) {
outLabel.setText(outBytes + " bytes");
}
} catch (IOException e) { } catch (IOException e) {
cleanupClient(); cleanupClient();
} }
@ -188,10 +192,8 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
for (int i=0; i < numRead; i++) { for (int i=0; i < numRead; i++) {
serialPort.writeByte(data[i]); serialPort.writeByte(data[i]);
} }
inBytes += numRead; inBytes += numRead;
if (GUI.isVisualized()) {
inLabel.setText(inBytes + " bytes");
}
} else { } else {
cleanupClient(); cleanupClient();
break; break;
@ -254,7 +256,9 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
} }
} }
private boolean closed = false;
public void closePlugin() { public void closePlugin() {
closed = true;
cleanupClient(); cleanupClient();
serialPort.deleteSerialDataObserver(serialDataObserver); serialPort.deleteSerialDataObserver(serialDataObserver);
try { try {
@ -267,5 +271,17 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
return mote; return mote;
} }
private static final int UPDATE_INTERVAL = 150;
private Timer updateTimer = new Timer(UPDATE_INTERVAL, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (closed) {
updateTimer.stop();
return;
}
inLabel.setText(inBytes + " bytes");
outLabel.setText(outBytes + " bytes");
}
});
} }