8000 Constructor bugfix, upgrades and new methods · arduino/micropython.js@8f9f214 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f9f214

Browse files
committed
Constructor bugfix, upgrades and new methods
- Making sure constructor doesn't throw an error if initialised without parameters - Simplifying `enter_raw_repl` parameters - Simplifying `eval` parameters - listPorts - stop - software reset
1 parent a5f7686 commit 8f9f214

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

micropython.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,28 @@ function sleep(millis) {
1313

1414
class MicroPythonBoard {
1515
constructor(options) {
16-
const { device, autoOpen = false } = options
16+
const { device, autoOpen = false } = options || {}
1717
this.device = device || {}
1818
this.in_raw_repl = false
1919
this.use_raw_paste = true
2020

21-
this.serial = new SerialPort({
22-
path: device,
23-
baudRate: 115200,
24-
lock: false,
25-
autoOpen: false
26-
})
27-
2821
if (autoOpen) {
2922
this.open()
3023
}
3124

3225
}
3326

27+
async listPorts() {
28+
return await SerialPort.list()
29+
}
30+
3431
open() {
32+
this.serial = new SerialPort({
33+
path: this.device,
34+
baudRate: 115200,
35+
lock: false,
36+
autoOpen: false
37+
})
3538
return new Promise((resolve, reject) => {
3639
this.serial.open((err) => {
3740
if (err) {
@@ -53,7 +56,9 @@ class MicroPythonBoard {
5356

5457
read_until(options) {
5558
const {
56-
ending = `\n`, timeout = null, data_consumer = () => false
59+
ending = `\n`,
60+
timeout = null,
61+
data_consumer = () => false
5762
} = options || {}
5863
return new Promise((resolve, reject) => {
5964
const parser = this.serial.pipe(new ReadlineParser({ delimiter: ending }))
@@ -71,8 +76,7 @@ class MicroPythonBoard {
7176
})
7277
}
7378

74-
enter_raw_repl(options) {
75-
const { timeout = null } = options || {}
79+
enter_raw_repl(timeout) {
7680
return new Promise(async (resolve, reject) => {
7781
// ctrl-C twice: interrupt any running program
7882
await this.serial.write(Buffer.from(`\r\x03\x03`))
@@ -168,9 +172,20 @@ class MicroPythonBoard {
168172 return this.follow({ timeout })
169173
}
170174

171-
async eval(options) {
172-
const { command = '' } = options || {}
173-
return await this.serial.write(Buffer.from(command))
175+
async eval(k) {
176+
return await this.serial.write(Buffer.from(k))
177+
}
178+
179+
async stop() {
180+
// Dismiss any data with ctrl-C
181+
await this.serial.write(Buffer.from(`\x03`))
182+
}
183+
184+
async reset() {
185+
// Dismiss any data with ctrl-C
186+
await this.serial.write(Buffer.from(`\x03`))
187+
// Soft reboot
188+
await this.serial.write(Buffer.from(`\x04`))
174189
}
175190

176191
async execfile(filePath) {
@@ -191,7 +206,6 @@ class MicroPythonBoard {
191206
})
192207
console.log(output)
193208
return this.exit_raw_repl()
194-
return Promise.reject()
195209
}
196210

197211
async fs_cat(filePath) {

0 commit comments

Comments
 (0)
0