Skip to content

Commit 35d2029

Browse files
author
Stefania
authored
Merge pull request #194 from arduino/retouch-board
fix bug about board reapping on different ports after upload
2 parents a348b25 + c404d59 commit 35d2029

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

demo/app.jsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ const scrollToBottom = (target) => {
3636
const daemon = new Daemon('https://builder.arduino.cc/v3/boards', chromeExtensionID);
3737
const firmwareUpdater = new FirmwareUpdater(daemon);
3838

39-
const handleUpload = () => {
40-
const target = {
41-
board: 'arduino:samd:mkr1000',
42-
port: '/dev/ttyACM0',
43-
network: false
44-
};
45-
46-
// Upload a compiled sketch.
47-
daemon.uploadSerial(target, 'serial_mirror', { bin: HEX });
48-
};
49-
5039
const handleBootloaderMode = (e, port) => {
5140
e.preventDefault();
5241
daemon.setBootloaderMode(port);
@@ -98,14 +87,16 @@ class App extends React.Component {
9887
downloadStatus: '',
9988
downloadError: '',
10089
serialInput: '',
101-
supportedBoards: []
90+
supportedBoards: [],
91+
uploadingPort: ''
10292
};
10393
this.handleOpen = this.handleOpen.bind(this);
10494
this.handleClose = this.handleClose.bind(this);
10595
this.handleSend = this.handleSend.bind(this);
10696
this.handleChangeSerial = this.handleChangeSerial.bind(this);
10797
this.showError = this.showError.bind(this);
10898
this.clearError = this.clearError.bind(this);
99+
this.handleUpload = this.handleUpload.bind(this);
109100
}
110101

111102
componentDidMount() {
@@ -191,6 +182,24 @@ class App extends React.Component {
191182
this.setState({ serialInput: '' });
192183
}
193184

185+
handleUpload() {
186+
const target = {
187+
board: 'arduino:samd:mkr1000',
188+
port: '/dev/ttyACM1',
189+
network: false
190+
};
191+
192+
this.setState({ uploadingPort: target.port });
193+
daemon.boardPortAfterUpload.subscribe(portStatus => {
194+
if (portStatus.hasChanged) {
195+
this.setState({ uploadingPort: portStatus.newPort });
196+
}
197+
});
198+
199+
// Upload a compiled sketch.
200+
daemon.uploadSerial(target, 'serial_mirror', { bin: HEX });
201+
}
202+
194203
render() {
195204
const listSerialDevices = this.state.serialDevices.map((device, i) => <li key={i}>
196205
{device.Name} - IsOpen: <span className={device.IsOpen ? 'open' : 'closed'}>
@@ -306,8 +315,9 @@ class App extends React.Component {
306315

307316
<div className="section">
308317
<h2>Upload a sample sketch on a MKR1000 at /dev/ttyACM0</h2>
309-
<button onClick={ handleUpload } disabled={ this.state.uploadStatus === daemon.UPLOAD_IN_PROGRESS }>Upload Sketch</button><br/>
318+
<button onClick={ this.handleUpload } disabled={ this.state.uploadStatus === daemon.UPLOAD_IN_PROGRESS }>Upload Sketch</button><br/>
310319
<div>Upload status: <span className={ uploadClass }> { this.state.uploadStatus }</span> <span>{ this.state.uploadError }</span></div>
320+
<div>Uploading port: { this.state.uploadingPort || '-' }</div>
311321
</div>
312322

313323
{ daemon.downloading ? <div className="section">

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arduino-create-agent-js-client",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"description": "JS module providing discovery of the Arduino Create Plugin and communication with it",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/daemon.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export default class Daemon {
7979
this.downloadingError = this.downloading.pipe(filter(download => download.status === this.DOWNLOAD_ERROR))
8080
.pipe(first())
8181
.pipe(takeUntil(this.downloadingDone));
82+
83+
this.boardPortAfterUpload = new Subject().pipe(first());
84+
this.uploadingPort = null;
8285
}
8386

8487
notifyUploadError(err) {
@@ -113,7 +116,9 @@ export default class Daemon {
113116
* @param {boolean} verbose
114117
*/
115118
uploadSerial(target, sketchName, compilationResult, verbose = true) {
116-
this.uploading.next({ status: this.UPLOAD_IN_PROGRESS });
119+
this.uploadingPort = target.port;
120+
this.uploading.next({ status: this.UPLOAD_IN_PROGRESS, msg: 'Upload started' });
121+
this.serialDevicesBeforeUpload = this.devicesList.getValue().serial;
117122

118123
this.closeSerialMonitor(target.port);
119124

@@ -139,6 +144,31 @@ export default class Daemon {
139144
data: compilationResult[ext] // For chromeOS plugin, consider to align this
140145
};
141146

147+
this.uploadingDone.subscribe(() => {
148+
this.waitingForPortToComeUp = timer(1000).subscribe(() => {
149+
const currentSerialDevices = this.devicesList.getValue().serial;
150+
let boardFound = currentSerialDevices.find(device => device.Name === this.uploadingPort);
151+
if (!boardFound) {
152+
boardFound = currentSerialDevices.find(d => this.serialDevicesBeforeUpload.every(e => e.Name !== d.Name));
153+
if (boardFound) {
154+
this.uploadingPort = boardFound.Name;
155+
this.boardPortAfterUpload.next({
156+
hasChanged: true,
157+
newPort: this.uploadingPort
158+
});
159+
}
160+
}
161+
162+
if (boardFound) {
163+
this.waitingForPortToComeUp.unsubscribe();
164+
this.uploadingPort = null;
165+
this.serialDevicesBeforeUpload = null;
166+
this.boardPortAfterUpload.next({
167+
hasChanged: false
168+
});
169+
}
170+
});
171+
});
142172
this._upload(uploadPayload, uploadCommandInfo);
143173
});
144174
}

0 commit comments

Comments
 (0)