19
19
package processing .app ;
20
20
21
21
import processing .app .debug .MessageConsumer ;
22
+ import processing .app .debug .TextAreaFIFO ;
22
23
import processing .core .*;
23
24
import static processing .app .I18n ._ ;
24
25
32
33
import java .net .*;
33
34
import java .util .*;
34
35
35
- public class SerialMonitor extends JFrame implements MessageConsumer {
36
+ public class SerialMonitor extends JFrame implements MessageConsumer , ActionListener {
36
37
private Serial serial ;
37
38
private String port ;
38
- private JTextArea textArea ;
39
+ private TextAreaFIFO textArea ;
39
40
private JScrollPane scrollPane ;
40
41
private JTextField textField ;
41
42
private JButton sendButton ;
42
43
private JCheckBox autoscrollBox ;
43
44
private JComboBox lineEndings ;
44
45
private JComboBox serialRates ;
45
46
private int serialRate ;
47
+ private javax .swing .Timer updateTimer ;
48
+ private StringBuffer updateBuffer ;
46
49
47
50
public SerialMonitor (String port ) {
48
51
super (port );
@@ -70,7 +73,9 @@ public void actionPerformed(ActionEvent e) {
70
73
Font editorFont = Preferences .getFont ("editor.font" );
71
74
Font font = new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ());
72
75
73
- textArea = new JTextArea (16 , 40 );
76
+ textArea = new TextAreaFIFO (4000000 );
77
+ textArea .setRows (16 );
78
+ textArea .setColumns (40 );
74
79
textArea .setEditable (false );
75
80
textArea .setFont (font );
76
81
@@ -174,6 +179,9 @@ public void actionPerformed(ActionEvent event) {
174
179
}
175
180
}
176
181
}
182
+
183
+ updateBuffer = new StringBuffer (1048576 );
184
+ updateTimer = new javax .swing .Timer (33 , this ); // redraw serial monitor at 30 Hz
177
185
}
178
186
179
187
protected void setPlacement (int [] location ) {
@@ -230,6 +238,7 @@ public void openSerialPort() throws SerialException {
230
238
}
231
239
}
232
240
serial .addListener (this );
241
+ updateTimer .start ();
233
242
}
234
243
235
244
public void closeSerialPort () {
@@ -243,15 +252,34 @@ public void closeSerialPort() {
243
252
}
244
253
}
245
254
246
- public void message (final String s ) {
247
- SwingUtilities .invokeLater (new Runnable () {
248
- public void run () {
249
- textArea .append (s );
250
- if (autoscrollBox .isSelected ()) {
251
- textArea .setCaretPosition (textArea .getDocument ().getLength ());
252
- }
253
- }});
255
+ public void message (String s ) {
256
+ // TODO: can we pass a byte array, to avoid overhead of String
257
+ addToUpdateBuffer (s );
258
+ }
259
+
260
+ private synchronized void addToUpdateBuffer (String s ) {
261
+ updateBuffer .append (s );
262
+ }
263
+
264
+ private synchronized String consumeUpdateBuffer () {
265
+ String s = updateBuffer .toString ();
266
+ updateBuffer .setLength (0 );
267
+ return s ;
268
+ }
269
+
270
+ public void actionPerformed (ActionEvent e ) {
271
+ final String s = consumeUpdateBuffer ();
272
+ if (s .length () > 0 ) {
273
+ //System.out.println("gui append " + s.length());
274
+ boolean scroll = autoscrollBox .isSelected ();
275
+ textArea .allowTrim (scroll );
276
+ textArea .append (s );
277
+ if (scroll ) {
278
+ textArea .setCaretPosition (textArea .getDocument ().getLength ());
279
+ }
280
+ }
254
281
}
282
+
255
283
}
256
284
257
285
class FakeSerial extends Serial {
0 commit comments