-
-
Notifications
You must be signed in to change notification settings - Fork 3
Bugfix/run twice #10
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
Bugfix/run twice #10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ class MicroPythonBoard { | |
constructor() { | ||
this.port = null | ||
this.serial = null | ||
this.reject_run = null | ||
} | ||
|
||
list_ports() { | ||
|
@@ -113,7 +114,10 @@ class MicroPythonBoard { | |
} | ||
|
||
async get_prompt() { | ||
const out = await this.write_and_read_until(`\r\x02\x03`, '\r\n>>>') | ||
await sleep(100) | ||
await this.stop() | ||
await sleep(100) | ||
const out = await this.write_and_read_until(`\r\x03\x02`, '\r\n>>>') | ||
return Promise.resolve(out) | ||
} | ||
|
||
|
@@ -147,10 +151,22 @@ class MicroPythonBoard { | |
|
||
async run(code, data_consumer) { | ||
data_consumer = data_consumer || function() {} | ||
await this.enter_raw_repl() | ||
const output = await this.exec_raw(code || '#', data_consumer) | ||
await this.exit_raw_repl() | ||
return Promise.resolve(output) | ||
return new Promise(async (resolve, reject) => { | ||
if (this.reject_run) { | ||
this.reject_run(new Error('re-run')) | ||
this.reject_run = null | ||
} | ||
this.reject_run = reject | ||
try { | ||
await this.enter_raw_repl() | ||
const output = await this.exec_raw(code || '#', data_consumer) | ||
await this.exit_raw_repl() | ||
return resolve(output) | ||
} catch (e) { | ||
reject(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: if there is an error here, and the promise is rejected, then the next |
||
this.reject_run = null | ||
} | ||
}) | ||
} | ||
|
||
async eval(k) { | ||
|
@@ -159,12 +175,20 @@ class MicroPythonBoard { | |
} | ||
|
||
async stop() { | ||
if (this.reject_run) { | ||
this.reject_run(new Error('pre stop')) | ||
this.reject_run = null | ||
} | ||
// Dismiss any data with ctrl-C | ||
await this.serial.write(Buffer.from(`\x03`)) | ||
return Promise.resolve() | ||
} | ||
|
||
async reset() { | ||
if (this.reject_run) { | ||
this.reject_run(new Error('pre reset')) | ||
this.reject_run = null | ||
} | ||
// Dismiss any data with ctrl-C | ||
await this.serial.write(Buffer.from(`\x03`)) | ||
// Soft reboot | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is not necessary here, but if you want to keep setting the rejected promise to
null
, maybe you can have a method for all three occurrences.Any reason you prefer rejecting with a
string
instead of anError
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it should definitely be an error. I just don't find the helper function really helpful here. I'm also not sure if this is going to be the final solution ™️ so I'd prefer keeping things a bit more unrolled.