8000 rejecting run promise before running again, stop and reset · arduino/micropython.js@04bc576 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04bc576

Browse files
committed
rejecting run promise before running again, stop and reset
1 parent a9001e3 commit 04bc576

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

micropython.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class MicroPythonBoard {
2828
constructor() {
2929
this.port = null
3030
this.serial = null
31+
this.reject_run = null
3132
}
3233

3334
list_ports() {
@@ -113,7 +114,10 @@ class MicroPythonBoard {
113114
}
114115

115116
async get_prompt() {
116-
const out = await this.write_and_read_until(`\r\x02\x03`, '\r\n>>>')
117+
await sleep(100)
118+
await this.stop()
119+
await sleep(100)
120+
const out = await this.write_and_read_until(`\r\x03\x02`, '\r\n>>>')
117121
return Promise.resolve(out)
118122
}
119123

@@ -147,10 +151,21 @@ class MicroPythonBoard {
147151

148152
async run(code, data_consumer) {
149153
data_consumer = data_consumer || function() {}
150-
await this.enter_raw_repl()
151-
const output = await this.exec_raw(code || '#', data_consumer)
152-
await this.exit_raw_repl()
153-
return Promise.resolve(output)
154+
return new Promise(async (resolve, reject) => {
155+
if (this.reject_run) {
156+
this.reject_run('re run')
157+
this.reject_run = null
158+
}
159+
this.reject_run = reject
160+
try {
161+
await this.enter_raw_repl()
162+
const output = await this.exec_raw(code || '#', data_consumer)
163+
await this.exit_raw_repl()
164+
return resolve(output)
165+
} catch (e) {
166+
reject(e)
167+
}
168+
})
154169
}
155170

156171
async eval(k) {
@@ -159,12 +174,20 @@ class MicroPythonBoard {
159174
}
160175

161176
async stop() {
177+
if (this.reject_run) {
178+
this.reject_run('pre stop')
179+
this.reject_run = null
180+
}
162181
// Dismiss any data with ctrl-C
163182
await this.serial.write(Buffer.from(`\x03`))
164183
return Promise.resolve()
165184
}
166185

167186
async reset() {
187+
if (this.reject_run) {
188+
this.reject_run('pre reset')
189+
this.reject_run = null
190+
}
168191
// Dismiss any data with ctrl-C
169192
await this.serial.write(Buffer.from(`\x03`))
170193
// Soft reboot

0 commit comments

Comments
 (0)
0