@@ -22,12 +22,32 @@ class MicroPythonBoard {
22
22
this . device = null
23
23
this . serial = null
24
24
this . in_raw_repl = false
25
+ this . chunk_size = 200
26
+ this . chunk_sleep = 100
25
27
}
26
28
27
29
listPorts ( ) {
28
30
return SerialPort . list ( )
29
31
}
30
32
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
+
31
51
async open ( device ) {
32
52
if ( device ) {
33
53
this . device = device
@@ -94,11 +114,11 @@ class MicroPythonBoard {
94
114
enter_raw_repl ( timeout ) {
95
115
return new Promise ( async ( resolve , reject ) => {
96
116
// 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` ) )
98
118
// flush input
99
119
await this . serial . flush ( )
100
120
// ctrl-A: enter raw REPL
101
- await this . serial . write ( Buffer . from ( `\r\x01` ) )
121
+ await this . write_and_drain ( Buffer . from ( `\r\x01` ) )
102
122
103
123
let data = await this . read_until ( {
104
124
ending : Buffer . from ( `raw REPL; CTRL-B to exit\r\n>` ) ,
@@ -117,7 +137,7 @@ class MicroPythonBoard {
117
137
async exit_raw_repl ( ) {
118
138
if ( this . in_raw_repl ) {
119
139
// 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` ) )
121
141
this . in_raw_repl = false
122
142
}
123
143
return Promise . resolve ( )
@@ -135,7 +155,6 @@ class MicroPythonBoard {
135
155
} )
136
156
}
137
157
138
-
139
158
exec_raw_no_follow ( options ) {
140
159
const { timeout = null , command = '' } = options || { }
141
160
return new Promise ( async ( resolve , reject ) => {
@@ -173,7 +192,7 @@ class MicroPythonBoard {
173
192
}
174
193
175
194
async eval ( k ) {
176
- return await this . serial . write ( Buffer . from ( k ) )
195
+ return this . serial . write ( Buffer . from ( k ) )
177
196
}
178
197
179
198
async stop ( ) {
@@ -288,14 +307,14 @@ class MicroPythonBoard {
288
307
const hexArray = contentString . split ( '' ) . map (
289
308
c => c . charCodeAt ( 0 ) . toString ( 16 ) . padStart ( 2 , '0' )
290
309
)
291
- const chunkSize = 256
310
+ const chunkSize = this . chunk_size
292
311
for ( let i = 0 ; i < hexArray . length ; i += chunkSize ) {
293
312
let slice = hexArray . slice ( i , i + chunkSize )
294
313
let bytes = slice . map ( h => `0x${ h } ` )
295
314
let line = `w(bytes([${ bytes . join ( ',' ) } ]))\x04`
296
315
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 )
299
318
}
300
319
return this . exit_raw_repl ( )
301
320
}
@@ -308,21 +327,20 @@ class MicroPythonBoard {
308
327
content = fixLineBreak ( content )
309
328
await this . enter_raw_repl ( )
310
329
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`
313
331
} )
314
332
await sleep ( 100 )
315
333
const hexArray = content . split ( '' ) . map (
316
334
c => c . charCodeAt ( 0 ) . toString ( 16 ) . padStart ( 2 , '0' )
317
335
)
318
- const chunkSize = 256
336
+ const chunkSize = this . chunk_size
319
337
for ( let i = 0 ; i < hexArray . length ; i += chunkSize ) {
320
338
let slice = hexArray . slice ( i , i + chunkSize )
321
339
let bytes = slice . map ( h => `0x${ h } ` )
322
340
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 ) )
326
344
}
327
345
return this . exit_raw_repl ( )
328
346
} else {
0 commit comments