8000 Merge pull request #5 from arduino/bugfix/write-and-drain · arduino/micropython.js@b78d9ff · GitHub
[go: up one dir, main page]

Skip to content

Commit b78d9ff

Browse files
authored
Merge pull request #5 from arduino/bugfix/write-and-drain
Bugfix/write and drain
2 parents d00ad22 + 3c29108 commit b78d9ff

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

micropython.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,32 @@ class MicroPythonBoard {
2222
this.device = null
2323
this.serial = null
2424
this.in_raw_repl = false
25+
this.chunk_size = 200
26+
this.chunk_sleep = 100
2527
}
2628

2729
listPorts() {
2830
return SerialPort.list()
2931
}
3032

33+
list_ports() { // backward compatibility
34+
return this.listPorts()
35+
}
36+
37+
write_and_drain(data) {
38+
// https://serialport.io/docs/api-stream#drain-example
39+
return new Promise((resolve, reject) => {
40+
this.serial.write(data)
41+
this.serial.drain((err) => {
42+
if (err) {
43+
reject(err)
44+
} else {
45+
resolve()
46+
}
47+
})
48+
})
49+
}
50+
3151
async open(device) {
3252
if (device) {
3353
this.device = device
@@ -94,11 +114,11 @@ class MicroPythonBoard {
94114
enter_raw_repl(timeout) {
95115
return new Promise(async (resolve, reject) => {
96116
// ctrl-C twice: interrupt any running program
97-
await this.serial.write(Buffer.from(`\r\x03\x03`))
117+
await this.write_and_drain(Buffer.from(`\r\x03\x03`))
98118
// flush input
99119
await this.serial.flush()
100120
// ctrl-A: enter raw REPL
101-
await this.serial.write(Buffer.from(`\r\x01`))
121+
await this.write_and_drain(Buffer.from(`\r\x01`))
102122

103123
let data = await this.read_until({
104124
ending: Buffer.from(`raw REPL; CTRL-B to exit\r\n>`),
@@ -117,7 +137,7 @@ class MicroPythonBoard {
117137
async exit_raw_repl() {
118138
if (this.in_raw_repl) {
119139
// ct 8000 rl-B: enter friendly REPL
120-
await this.serial.write(Buffer.from(`\r\x02`))
140+
await this.write_and_drain(Buffer.from(`\r\x02`))
121141
this.in_raw_repl = false
122142
}
123143
return Promise.resolve()
@@ -135,7 +155,6 @@ class MicroPythonBoard {
135155
})
136156
}
137157

138-
139158
exec_raw_no_follow(options) {
140159
const { timeout = null, command = '' } = options || {}
141160
return new Promise(async (resolve, reject) => {
@@ -173,7 +192,7 @@ class MicroPythonBoard {
173192
}
174193

175194
async eval(k) {
176-
return await this.serial.write(Buffer.from(k))
195+
return this.serial.write(Buffer.from(k))
177196
}
178197

179198
async stop() {
@@ -288,14 +307,14 @@ class MicroPythonBoard {
288307
const hexArray = contentString.split('').map(
289308
c => c.charCodeAt(0).toString(16).padStart(2, '0')
290309
)
291-
const chunkSize = 256
310+
const chunkSize = this.chunk_size
292311
for (let i = 0; i < hexArray.length; i+= chunkSize) {
293312
let slice = hexArray.slice(i, i+chunkSize)
294313
let bytes = slice.map(h => `0x${h}`)
295314
let line = `w(bytes([${bytes.join(',')}]))\x04`
296315
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
297-
await this.serial.write(line)
298-
await sleep(100)
316+
await this.write_and_drain(line)
317+
await sleep(this.chunk_sleep)
299318
}
300319
return this.exit_raw_repl()
301320
}
@@ -308,21 +327,20 @@ class MicroPythonBoard {
308327
content = fixLineBreak(content)
309328
await this.enter_raw_repl()
310329
let output = await this.exec_raw({
311-
command: `f=open('${dest}','w')\nw=f.write`,
312-
data_consumer: (d) => console.log('data consumer', d)
330+
command: `f=open('${dest}','w')\nw=f.write`
313331
})
314332
await sleep(100)
315333
const hexArray = content.split('').map(
316334
c => c.charCodeAt(0).toString(16).padStart(2, '0')
317335
)
318-
const chunkSize = 256
336+
const chunkSize = this.chunk_size
319337
for (let i = 0; i < hexArray.length; i+= chunkSize) {
320338
let slice = hexArray.slice(i, i+chunkSize)
321339
let bytes = slice.map(h => `0x${h}`)
322340
let line = `w(bytes([${bytes.join(',')}]))\x04`
323-
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
324-
await this.serial.write(line)
325-
await sleep(100)
341+
data_consumer( parseInt((i / hexArray.length) * 100) + '%' )
342+
await this.write_and_drain(line)
343+
await sleep(await sleep(this.chunk_sleep))
326344
}
327345
return this.exit_raw_repl()
328346
} else {

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.2",
3+
"version": "1.3.3",
44
"description": "Interpretation of pyboard.py in javascript",
55
"main": "micropython.js",
66
"scripts": {

0 commit comments

Comments
 (0)
0