Skip to content

Allow the serial monitor to stay opened during upload, disabling it #2742

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

Merged
merged 1 commit into from
May 22, 2015
Merged
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
58 changes: 56 additions & 2 deletions app/src/processing/app/AbstractMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener {
protected JCheckBox autoscrollBox;
protected JComboBox lineEndings;
protected JComboBox serialRates;
private boolean monitorEnabled;
private boolean closed;

private Timer updateTimer;
private StringBuffer updateBuffer;
Expand All @@ -54,6 +56,7 @@ public AbstractMonitor(String title) {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
try {
closed = true;
close();
} catch (Exception e) {
// ignore
Expand Down Expand Up @@ -173,10 +176,57 @@ public void actionPerformed(ActionEvent event) {
}
}
}

updateBuffer = new StringBuffer(1048576);
updateTimer = new Timer(33, this); // redraw serial monitor at 30 Hz
updateTimer.start();

monitorEnabled = true;
closed = false;
}

public void enableWindow(boolean enable)
{
textArea.setEnabled(enable);
scrollPane.setEnabled(enable);
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);

monitorEnabled = enable;
}

// Puts the window in suspend state, closing the serial port
// to allow other entity (the programmer) to use it
public void suspend()
{
enableWindow(false);

try {
close();
}
catch(Exception e) {
//throw new SerialException("Failed closing the port");
}

}

public void resume() throws SerialException
{
// Enable the window
enableWindow(true);

// If the window is visible, try to open the serial port
if (isVisible())
try {
open();
}
catch(Exception e) {
throw new SerialException("Failed opening the port");
}

}

public void onSerialRateChange(ActionListener listener) {
Expand Down Expand Up @@ -224,10 +274,14 @@ public String getAuthorizationKey() {
return null;
}

public boolean isClosed() {
return closed;
}

public abstract void open() throws Exception;

public abstract void close() throws Exception;

public synchronized void addToUpdateBuffer(char buff[], int n) {
updateBuffer.append(buff, 0, n);
}
Expand Down
56 changes: 44 additions & 12 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2517,8 +2517,7 @@ public void run() {

try {
if (serialMonitor != null) {
serialMonitor.close();
serialMonitor.setVisible(false);
serialMonitor.suspend();
}

uploading = true;
Expand Down Expand Up @@ -2550,7 +2549,17 @@ public void run() {
uploading = false;
//toolbar.clear();
toolbar.deactivate(EditorToolbar.EXPORT);
}

// Return the serial monitor window to its initial state
try {
if (serialMonitor != null)
serialMonitor.resume();
}
catch (SerialException e) {
statusError(e);
}

}
}

// DAM: in Arduino, this is upload (with verbose output)
Expand All @@ -2559,8 +2568,7 @@ public void run() {

try {
if (serialMonitor != null) {
serialMonitor.close();
serialMonitor.setVisible(false);
serialMonitor.suspend();
}

uploading = true;
Expand Down Expand Up @@ -2592,6 +2600,16 @@ public void run() {
uploading = false;
//toolbar.clear();
toolbar.deactivate(EditorToolbar.EXPORT);

if (serialMonitor != null) {
try {
if (serialMonitor != null)
serialMonitor.resume();
}
catch (SerialException e) {
statusError(e);
}
}
}
}

Expand Down Expand Up @@ -2631,14 +2649,23 @@ protected boolean handleExportCheckModified() {


public void handleSerial() {
if (uploading) return;

if (serialMonitor != null) {
try {
serialMonitor.close();
serialMonitor.setVisible(false);
} catch (Exception e) {
// noop
// The serial monitor already exists

if (serialMonitor.isClosed()) {
// If it's closed, clear the refrence to the existing
// monitor and create a new one
serialMonitor = null;
}
else {
// If it's not closed, give it the focus
try {
serialMonitor.toFront();
serialMonitor.requestFocus();
return;
} catch (Exception e) {
// noop
}
}
}

Expand All @@ -2652,6 +2679,11 @@ public void handleSerial() {
serialMonitor = new MonitorFactory().newMonitor(port);
serialMonitor.setIconImage(getIconImage());

// If currently uploading, disable the monitor (it will be later
// enabled when done uploading)
if (uploading)
serialMonitor.suspend();

boolean success = false;
do {
if (serialMonitor.requiresAuthorization() && !PreferencesData.has(serialMonitor.getAuthorizationKey())) {
Expand Down