@@ -17,20 +17,6 @@ function fixLineBreak(str) {
17
17
return str . replace ( / \r \n / g, '\n' )
18
18
}
19
19
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
-
34
20
class MicroPythonBoard {
35
21
constructor ( ) {
36
22
this . device = null
@@ -42,6 +28,20 @@ class MicroPythonBoard {
42
28
return SerialPort . list ( )
43
29
}
44
30
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
+
45
45
async open ( device ) {
46
46
if ( device ) {
47
47
this . device = device
@@ -108,11 +108,11 @@ class MicroPythonBoard {
108
108
enter_raw_repl ( timeout ) {
109
109
return new Promise ( async ( resolve , reject ) => {
110
110
// 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` ) )
112
112
// flush input
113
113
await this . serial . flush ( )
114
114
// ctrl-A: enter raw REPL
115
- await this . serial . write ( Buffer . from ( `\r\x01` ) )
115
+ await this . writeAndDrain ( Buffer . from ( `\r\x01` ) )
116
116
117
117
let data = await this . read_until ( {
118
118
ending : Buffer . from ( `raw REPL; CTRL-B to exit\r\n>` ) ,
@@ -131,7 +131,7 @@ class MicroPythonBoard {
131
131
async exit_raw_repl ( ) {
132
132
if ( this . in_raw_repl ) {
133
133
// ctrl-B: enter friendly REPL
134
- await this . serial . write ( Buffer . from ( `\r\x02` ) )
134
+ await this . writeAndDrain ( Buffer . from ( `\r\x02` ) )
135
135
this . in_raw_repl = false
136
136
}
137
137
return Promise . resolve ( )
@@ -186,7 +186,7 @@ class MicroPythonBoard {
<
8000
/tr>186
186
}
187
187
188
188
async eval ( k ) {
189
- return await this . serial . write ( Buffer . from ( k ) )
189
+ return this . serial . write ( Buffer . from ( k ) )
190
190
}
191
191
192
192
async stop ( ) {
@@ -307,7 +307,7 @@ class MicroPythonBoard {
307
307
let bytes = slice . map ( h => `0x${ h } ` )
308
308
let line = `w(bytes([${ bytes . join ( ',' ) } ]))\x04`
309
309
data_consumer ( parseInt ( ( i / hexArray . length ) * 100 ) + '%' )
310
- await writeAndDrain ( this . serial , line )
310
+ await this . writeAndDrain ( line )
311
311
await sleep ( 100 )
312
312
}
313
313
return this . exit_raw_repl ( )
@@ -333,7 +333,7 @@ class MicroPythonBoard {
333
333
let bytes = slice . map ( h => `0x${ h } ` )
334
334
let line = `w(bytes([${ bytes . join ( ',' ) } ]))\x04`
335
335
data_consumer ( parseInt ( ( i / hexArray . length ) * 100 ) + '%' )
336
- await writeAndDrain ( this . serial , line )
336
+ await this . writeAndDrain ( line )
337
337
await sleep ( 100 )
338
338
}
339
339
return this . exit_raw_repl ( )
0 commit comments