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
25
26
import java .awt .*;
26
27
import java .awt .event .*;
27
28
import javax .swing .*;
28
29
import javax .swing .border .*;
29
- import javax .swing .event .*;
30
30
import javax .swing .text .*;
31
31
32
- public class SerialMonitor extends JFrame implements MessageConsumer {
32
+ public class SerialMonitor extends JFrame implements MessageConsumer , ActionListener {
33
33
private Serial serial ;
34
34
private String port ;
35
- private JTextArea textArea ;
35
+ private TextAreaFIFO textArea ;
36
36
private JScrollPane scrollPane ;
37
37
private JTextField textField ;
38
38
private JButton sendButton ;
39
39
private JCheckBox autoscrollBox ;
40
40
private JComboBox lineEndings ;
41
41
private JComboBox serialRates ;
42
42
private int serialRate ;
43
+ private javax .swing .Timer updateTimer ;
44
+ private StringBuffer updateBuffer ;
43
45
44
46
public SerialMonitor (String port ) {
45
47
super (port );
@@ -67,7 +69,9 @@ public void actionPerformed(ActionEvent e) {
67
69
Font editorFont = Preferences .getFont ("editor.font" );
68
70
Font font = new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ());
69
71
70
- textArea = new JTextArea (16 , 40 );
72
+ textArea = new TextAreaFIFO (4000000 );
73
+ textArea .setRows (16 );
74
+ textArea .setColumns (40 );
71
75
textArea .setEditable (false );
72
76
textArea .setFont (font );
73
77
@@ -171,6 +175,9 @@ public void actionPerformed(ActionEvent event) {
171
175
}
172
176
}
173
177
}
178
+
179
+ updateBuffer = new StringBuffer (1048576 );
180
+ updateTimer = new javax .swing .Timer (33 , this ); // redraw serial monitor at 30 Hz
174
181
}
175
182
176
183
protected void setPlacement (int [] location ) {
@@ -203,9 +210,9 @@ private void send(String s) {
203
210
204
211
public void openSerialPort () throws SerialException {
205
212
if (serial != null ) return ;
206
-
207
213
serial = new Serial (port , serialRate );
208
214
serial .addListener (this );
215
+ updateTimer .start ();
209
216
}
210
217
211
218
public void closeSerialPort () {
@@ -219,13 +226,32 @@ public void closeSerialPort() {
219
226
}
220
227
}
221
228
222
- public void message (final String s ) {
223
- SwingUtilities .invokeLater (new Runnable () {
224
- public void run () {
225
- textArea .append (s );
226
- if (autoscrollBox .isSelected ()) {
227
- textArea .setCaretPosition (textArea .getDocument ().getLength ());
228
- }
229
- }});
229
+ public void message (String s ) {
230
+ // TODO: can we pass a byte array, to avoid overhead of String
231
+ addToUpdateBuffer (s );
230
232
}
233
+
234
+ private synchronized void addToUpdateBuffer (String s ) {
235
+ updateBuffer .append (s );
236
+ }
237
+
238
+ private synchronized String consumeUpdateBuffer () {
239
+ String s = updateBuffer .toString ();
240
+ updateBuffer .setLength (0 );
241
+ return s ;
242
+ }
243
+
244
+ public void actionPerformed (ActionEvent e ) {
245
+ final String s = consumeUpdateBuffer ();
246
+ if (s .length () > 0 ) {
247
+ //System.out.println("gui append " + s.length());
248
+ boolean scroll = autoscrollBox .isSelected ();
249
+ textArea .allowTrim (scroll );
250
+ textArea .append (s );
251
+ if (scroll ) {
252
+ textArea .setCaretPosition (textArea .getDocument ().getLength ());
253
+ }
254
+ }
255
+ }
256
+
231
257
}
0 commit comments