8000 Merge pull request #10 from arduino/bugfix/run-twice · arduino/micropython.js@4648998 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4648998

Browse files
authored
Merge pull request #10 from arduino/bugfix/run-twice
Bugfix/run twice
2 parents a9001e3 + 17661ab commit 4648998

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

micropython.js

Lines changed: 29 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,22 @@ 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(new Error('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+
this.reject_run = null
168+
}
169+
})
154170
}
155171

156172
async eval(k) {
@@ -159,12 +175,20 @@ class MicroPythonBoard {
159175
}
160176

161177
async stop() {
178+
if (this.reject_run) {
179+
this.reject_run(new Error('pre stop'))
180+
this.reject_run = null
181+
}
162182
// Dismiss any data with ctrl-C
163183
await this.serial.write(Buffer.from(`\x03`))
164184
return Promise.resolve()
165185
}
166186

167187
async reset() {
188+
if (this.reject_run) {
189+
this.reject_run(new Error('pre reset'))
190+
this.reject_run = null
191+
}
168192
// Dismiss any data with ctrl-C
169193
await this.serial.write(Buffer.from(`\x03`))
170194
// Soft reboot

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "micropython.js",
3-
"version": "1.3.5",
3+
"version": "1.4.1",
44
"description": "Interpretation of pyboard.py in javascript",
55
"main": "micropython.js",
66
"scripts": {

0 commit comments

Comments
 (0)
0