-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tame Serial Monitor Memory & CPU Usage (Arduino issue #2233)
- Loading branch information
1 parent
d8984e7
commit 03cb017
Showing
2 changed files
with
94 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// adapted from https://community.oracle.com/thread/1479784 | ||
|
||
package processing.app.debug; | ||
|
||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.util.Date; | ||
import javax.swing.*; | ||
import javax.swing.event.*; | ||
import javax.swing.text.*; | ||
|
||
public class TextAreaFIFO extends JTextArea implements DocumentListener | ||
{ | ||
private int maxChars; | ||
private int updateCount; // limit how often we trim the document | ||
private boolean doTrim; | ||
|
||
public TextAreaFIFO(int max) { | ||
maxChars = max; | ||
updateCount = 0; | ||
doTrim = true; | ||
getDocument().addDocumentListener( this ); | ||
} | ||
|
||
public void allowTrim(boolean trim) { | ||
doTrim = trim; | ||
} | ||
|
||
public void insertUpdate(DocumentEvent e) { | ||
if (++updateCount > 150 && doTrim) { | ||
updateCount = 0; | ||
SwingUtilities.invokeLater( new Runnable() { | ||
public void run() { | ||
trimDocument(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public void removeUpdate(DocumentEvent e) {} | ||
public void changedUpdate(DocumentEvent e) {} | ||
|
||
public void trimDocument() { | ||
int len = 0; | ||
len = getDocument().getLength(); | ||
if (len > maxChars) { | ||
int n = len - maxChars; | ||
System.out.println("trimDocument: remove " + n + " chars"); | ||
try { | ||
getDocument().remove(0, n); | ||
} catch(BadLocationException ble) { | ||
} | ||
} | ||
} | ||
} |