8000 make `writeAndDrain` a method · arduino/micropython.js@cab501d · GitHub
[go: up one dir, main page]

Skip to content

Commit cab501d

Browse files
committed
make writeAndDrain a method
1 parent 4b815d8 commit cab501d

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

micropython.js

Lines changed: 20 additions & 20 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@ function fixLineBreak(str) {
1717
return str.replace(/\r\n/g, '\n')
1818
}
1919

20-
function writeAndDrain(serial, data) {
21-
// https://serialport.io/docs/api-stream#drain-example
22-
return new Promise((resolve, reject) => {
23-
serial.write(data)
24-
serial.drain((err) => {
25-
if (err) {
26-
reject(err)
27-
} else {
28-
resolve()
29-
}
30-
})
31-
})
32-
}
33-
3420
class MicroPythonBoard {
3521
constructor() {
3622
this.device = null
@@ -42,6 +28,20 @@ class MicroPythonBoard {
4228
return SerialPort.list()
4329
}
4430

31+
writeAndDrain(data) {
32+
// https://serialport.io/docs/api-stream#drain-example
33+
return new Promise((resolve, reject) => {
34+
this.serial.write(data)
35+
this.serial.drain((err) => {
36+
if (err) {
37+
reject(err)
38+
} else {
39+
resolve()
40+
}
41+
})
42+
})
43+
}
44+
4545
async open(device) {
4646
if (device) {
4747
this.device = device
@@ -108,11 +108,11 @@ class MicroPythonBoard {
108108
enter_raw_repl(timeout) {
109109
return new Promise(async (resolve, reject) => {
110110
// ctrl-C twice: interrupt any running program
111-
await this.serial.write(Buffer.from(`\r\x03\x03`))
111+
await this.writeAndDrain(Buffer.from(`\r\x03\x03`))
112112
// flush input
113113
await this.serial.flush()
114114
// ctrl-A: enter raw REPL
115-
await this.serial.write(Buffer.from(`\r\x01`))
115+
await this.writeAndDrain(Buffer.from(`\r\x01`))
116116

117117
let data = await this.read_until({
118118
ending: Buffer.from(`raw REPL; CTRL-B to exit\r\n>`),
@@ -131,7 +131,7 @@ class MicroPythonBoard {
131131
async exit_raw_repl() {
132132
if (this.in_raw_repl) {
133133
// ctrl-B: enter friendly REPL
134-
await this.serial.write(Buffer.from(`\r\x02`))
134+
await this.writeAndDrain(Buffer.from(`\r\x02`))
135135
this.in_raw_repl = false
136136
}
137137
return Promise.resolve()
@@ -186,7 +186,7 @@ class MicroPythonBoard {
186186
}
187187

188188
async eval(k) {
189-
return await this.serial.write(Buffer.from(k))
189+
return this.serial.write(Buffer.from(k))
190190
}
191191

192192
async stop() {
@@ -307,7 +307,7 @@ class MicroPythonBoard {
307307
let bytes = slice.map(h => `0x${h}`)
308308
let line = `w(bytes([${bytes.join(',')}]))\x04`
309309
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
310-
await writeAndDrain(this.serial, line)
310+
await this.writeAndDrain(line)
311311
await sleep(100)
312312
}
313313
return this.exit_raw_repl()
@@ -333,7 +333,7 @@ class MicroPythonBoard {
333333
let bytes = slice.map(h => `0x${h}`)
334334
let line = `w(bytes([${bytes.join(',')}]))\x04`
335335
data_consumer( parseInt((i / hexArray.length) * 100) + '%' )
336-
await writeAndDrain(this.serial, line)
336+
await this.writeAndDrain(line)
337337
await sleep(100)
338338
}
339339
return this.exit_raw_repl()

0 commit comments

Comments
 (0)
0