Skip to content

Add "show timestamp" to serial monitor #6178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion app/src/processing/app/AbstractTextMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.Box;
import javax.swing.BoxLayout;
Expand All @@ -22,7 +24,9 @@
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Document;

import cc.arduino.packages.BoardPort;

Expand All @@ -36,11 +40,15 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JButton sendButton;
protected JButton clearButton;
protected JCheckBox autoscrollBox;
protected JCheckBox addTimeStampBox;
protected JComboBox lineEndings;
protected JComboBox serialRates;

private SimpleDateFormat logDateFormat;

public AbstractTextMonitor(BoardPort boardPort) {
super(boardPort);
logDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
}

protected void onCreateWindow(Container mainPane) {
Expand Down Expand Up @@ -90,6 +98,7 @@ public void windowGainedFocus(WindowEvent e) {
pane.setBorder(new EmptyBorder(4, 4, 4, 4));

autoscrollBox = new JCheckBox(tr("Autoscroll"), true);
addTimeStampBox = new JCheckBox(tr("Show timestamp"), false);

noLineEndingAlert = new JLabel(I18n.format(tr("You've pressed {0} but nothing was sent. Should you select a line ending?"), tr("Send")));
noLineEndingAlert.setToolTipText(noLineEndingAlert.getText());
Expand All @@ -108,6 +117,15 @@ public void actionPerformed(ActionEvent event) {
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
if (PreferencesData.get("serial.show_timestamp") != null) {
addTimeStampBox.setSelected(PreferencesData.getBoolean("serial.show_timestamp"));
}
addTimeStampBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PreferencesData.setBoolean("serial.show_timestamp", addTimeStampBox.isSelected());
}
});

lineEndings.setMaximumSize(lineEndings.getMinimumSize());

serialRates = new JComboBox();
Expand All @@ -118,6 +136,7 @@ public void actionPerformed(ActionEvent event) {
serialRates.setMaximumSize(serialRates.getMinimumSize());

pane.add(autoscrollBox);
pane.add(addTimeStampBox);
pane.add(Box.createHorizontalGlue());
pane.add(noLineEndingAlert);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
Expand All @@ -138,6 +157,7 @@ protected void onEnableWindow(boolean enable)
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
addTimeStampBox.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);
}
Expand All @@ -158,7 +178,24 @@ public void onSerialRateChange(ActionListener listener) {
public void message(final String s) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(s);
if (addTimeStampBox.isSelected()) {
String[] lines = s.split("(?<=\\n)");
Document doc = textArea.getDocument();
for (String currentLine : lines) {
try {
if (doc.getLength() == 0 || ((int) doc.getText(doc.getLength() - 1, 1).charAt(0) == 10)) {
textArea.append(logDateFormat.format(new Date()) + " -> " + currentLine);
} else {
textArea.append(currentLine);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
} else {
textArea.append(s);
}

if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
}
Expand Down
24 changes: 22 additions & 2 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public boolean test(SketchController sketch) {
private Runnable exportAppHandler;
private Runnable timeoutUploadHandler;

private Map<String, Tool> internalToolCache = new HashMap<String, Tool>();

public Editor(Base ibase, File file, int[] storedLocation, int[] defaultLocation, Platform platform) throws Exception {
super("Arduino");
this.base = ibase;
Expand Down Expand Up @@ -962,8 +964,7 @@ public void updateKeywords(PdeKeywords keywords) {

JMenuItem createToolMenuItem(String className) {
try {
Class<?> toolClass = Class.forName(className);
final Tool tool = (Tool) toolClass.newInstance();
final Tool tool = getOrCreateToolInstance(className);

JMenuItem item = new JMenuItem(tool.getMenuTitle());

Expand All @@ -982,6 +983,20 @@ public void actionPerformed(ActionEvent e) {
}
}

private Tool getOrCreateToolInstance(String className) {
Tool internalTool = internalToolCache.get(className);
if (internalTool == null) {
try {
Class<?> toolClass = Class.forName(className);
internalTool = (Tool) toolClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
internalToolCache.put(className, internalTool);
}
return internalTool;
}

private void addInternalTools(JMenu menu) {
JMenuItem item;
Expand Down Expand Up @@ -1996,6 +2011,11 @@ private boolean handleSave2() {
statusNotice(tr("Saving..."));
boolean saved = false;
try {
if (PreferencesData.getBoolean("editor.autoformat_currentfile_before_saving")) {
Tool formatTool = getOrCreateToolInstance("cc.arduino.packages.formatter.AStyle");
formatTool.run();
}

boolean wasReadOnly = sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
String previousMainFilePath = sketch.getMainFilePath();
saved = sketchController.save();
Expand Down