Skip to content

Commit 181211e

Browse files
committed
Merge pull request #2742 from Wackerbarth/Issue_2180
Allow the serial monitor to stay opened during upload, disabling it
2 parents 94b16a5 + f48df59 commit 181211e

File tree

2 files changed

+100
-14
lines changed

2 files changed

+100
-14
lines changed

app/src/processing/app/AbstractMonitor.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener {
4444
protected JCheckBox autoscrollBox;
4545
protected JComboBox lineEndings;
4646
protected JComboBox serialRates;
47+
private boolean monitorEnabled;
48+
private boolean closed;
4749

4850
private Timer updateTimer;
4951
private StringBuffer updateBuffer;
@@ -54,6 +56,7 @@ public AbstractMonitor(String title) {
5456
addWindowListener(new WindowAdapter() {
5557
public void windowClosing(WindowEvent event) {
5658
try {
59+
closed = true;
5760
close();
5861
} catch (Exception e) {
5962
// ignore
@@ -173,10 +176,57 @@ public void actionPerformed(ActionEvent event) {
173176
}
174177
}
175178
}
176-
179+
177180
updateBuffer = new StringBuffer(1048576);
178181
updateTimer = new Timer(33, this); // redraw serial monitor at 30 Hz
179182
updateTimer.start();
183+
184+
monitorEnabled = true;
185+
closed = false;
186+
}
187+
188+
public void enableWindow(boolean enable)
189+
{
190+
textArea.setEnabled(enable);
191+
scrollPane.setEnabled(enable);
192+
textField.setEnabled(enable);
193+
sendButton.setEnabled(enable);
194+
autoscrollBox.setEnabled(enable);
195+
lineEndings.setEnabled(enable);
196+
serialRates.setEnabled(enable);
197+
198+
monitorEnabled = enable;
199+
}
200+
201+
// Puts the window in suspend state, closing the serial port
202+
// to allow other entity (the programmer) to use it
203+
public void suspend()
204+
{
205+
enableWindow(false);
206+
207+
try {
208+
close();
209+
}
210+
catch(Exception e) {
211+
//throw new SerialException("Failed closing the port");
212+
}
213+
214+
}
215+
216+
public void resume() throws SerialException
217+
{
218+
// Enable the window
219+
enableWindow(true);
220+
221+
// If the window is visible, try to open the serial port
222+
if (isVisible())
223+
try {
224+
open();
225+
}
226+
catch(Exception e) {
227+
throw new SerialException("Failed opening the port");
228+
}
229+
180230
}
181231

182232
public void onSerialRateChange(ActionListener listener) {
@@ -224,10 +274,14 @@ public String getAuthorizationKey() {
224274
return null;
225275
}
226276

277+
public boolean isClosed() {
278+
return closed;
279+
}
280+
227281
public abstract void open() throws Exception;
228282

229283
public abstract void close() throws Exception;
230-
284+
231285
public synchronized void addToUpdateBuffer(char buff[], int n) {
232286
updateBuffer.append(buff, 0, n);
233287
}

app/src/processing/app/Editor.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,8 +2517,7 @@ public void run() {
25172517

25182518
try {
25192519
if (serialMonitor != null) {
2520-
serialMonitor.close();
2521-
serialMonitor.setVisible(false);
2520+
serialMonitor.suspend();
25222521
}
25232522

25242523
uploading = true;
@@ -2550,7 +2549,17 @@ public void run() {
25502549
uploading = false;
25512550
//toolbar.clear();
25522551
toolbar.deactivate(EditorToolbar.EXPORT);
2553-
}
2552+
2553+
// Return the serial monitor window to its initial state
2554+
try {
2555+
if (serialMonitor != null)
2556+
serialMonitor.resume();
2557+
}
2558+
catch (SerialException e) {
2559+
statusError(e);
2560+
}
2561+
2562+
}
25542563
}
25552564

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

25602569
try {
25612570
if (serialMonitor != null) {
2562-
serialMonitor.close();
2563-
serialMonitor.setVisible(false);
2571+
serialMonitor.suspend();
25642572
}
25652573

25662574
uploading = true;
@@ -2592,6 +2600,16 @@ public void run() {
25922600
uploading = false;
25932601
//toolbar.clear();
25942602
toolbar.deactivate(EditorToolbar.EXPORT);
2603+
2604+
if (serialMonitor != null) {
2605+
try {
2606+
if (serialMonitor != null)
2607+
serialMonitor.resume();
2608+
}
2609+
catch (SerialException e) {
2610+
statusError(e);
2611+
}
2612+
}
25952613
}
25962614
}
25972615

@@ -2631,14 +2649,23 @@ protected boolean handleExportCheckModified() {
26312649

26322650

26332651
public void handleSerial() {
2634-
if (uploading) return;
2635-
26362652
if (serialMonitor != null) {
2637-
try {
2638-
serialMonitor.close();
2639-
serialMonitor.setVisible(false);
2640-
} catch (Exception e) {
2641-
// noop
2653+
// The serial monitor already exists
2654+
2655+
if (serialMonitor.isClosed()) {
2656+
// If it's closed, clear the refrence to the existing
2657+
// monitor and create a new one
2658+
serialMonitor = null;
2659+
}
2660+
else {
2661+
// If it's not closed, give it the focus
2662+
try {
2663+
serialMonitor.toFront();
2664+
serialMonitor.requestFocus();
2665+
return;
2666+
} catch (Exception e) {
2667+
// noop
2668+
}
26422669
}
26432670
}
26442671

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

2682+
// If currently uploading, disable the monitor (it will be later
2683+
// enabled when done uploading)
2684+
if (uploading)
2685+
serialMonitor.suspend();
2686+
26552687
boolean success = false;
26562688
do {
26572689
if (serialMonitor.requiresAuthorization() && !PreferencesData.has(serialMonitor.getAuthorizationKey())) {

0 commit comments

Comments
 (0)