@@ -14,20 +14,25 @@ function sleep(millis) {
14
14
class MicroPythonBoard {
15
15
constructor ( ) {
16
16
this . device = null
17
+ this . serial = null
17
18
this . in_raw_repl = false
18
19
}
19
20
20
21
listPorts ( ) {
21
22
return SerialPort . list ( )
22
23
}
23
24
24
- open ( device ) {
25
+ async open ( device ) {
25
26
if ( device ) {
26
27
this . device = device
27
28
}
28
29
if ( ! this . device ) {
29
30
throw new Error ( `No device specified` )
30
31
}
32
+ if ( this . serial && this . serial . isOpen ) {
33
+ await this . serial . close ( )
34
+ this . serial = null
35
+ }
31
36
32
37
this . serial = new SerialPort ( {
33
38
path : this . device ,
@@ -37,7 +42,7 @@ class MicroPythonBoard {
37
42
} )
38
43
39
44
return new Promise ( ( resolve , reject ) => {
40
- this . serial . open ( ( err ) => {
45
+ this . serial . open ( async ( err ) => {
41
46
if ( err ) {
42
47
reject ( err )
43
48
} else {
@@ -212,13 +217,14 @@ class MicroPythonBoard {
212
217
let output = await this . exec_raw ( {
213
218
command : `f=open('${ dest } ','w')\nw=f.write`
214
219
} )
215
- for ( let i = 0 ; i < content . length ; i += 64 ) {
216
- let slice = content . slice ( i , i + 64 )
220
+ await sleep ( 100 )
221
+ for ( let i = 0 ; i < content . length ; i += 128 ) {
222
+ let slice = content . slice ( i , i + 128 )
217
223
slice = slice . toString ( )
218
224
slice = slice . replace ( / " " " / g, `\\"\\"\\"` )
219
225
await this . serial . write ( `w("""${ slice } """)` )
220
226
await this . serial . write ( `\x04` )
221
- await sleep ( 50 )
227
+ await sleep ( 100 )
222
228
}
223
229
return this . exit_raw_repl ( )
224
230
}
@@ -238,7 +244,8 @@ class MicroPythonBoard {
238
244
let slice = content . slice ( i , i + 64 )
239
245
slice = slice . toString ( )
240
246
slice = slice . replace ( / " " " / g, `\\"\\"\\"` )
241
- await this . serial . write ( `w("""${ slice } """)` )
247
+ // slice = slice.replace(//g, ``)
248
+ await this . serial . write ( `w("""${ slice } """)\n` )
242
249
await this . serial . write ( `\x04` )
243
250
await sleep ( 50 )
244
251
}
@@ -266,7 +273,6 @@ class MicroPythonBoard {
266
273
const output = await this . exec_raw ( {
267
274
command : `import uos\nuos.rmdir('${ filePath } ')`
268
275
} )
269
- console . log ( output )
270
276
return this . exit_raw_repl ( )
271
277
}
272
278
return Promise . reject ( )
@@ -278,7 +284,17 @@ class MicroPythonBoard {
278
284
const output = await this . exec_raw ( {
279
285
command : `import uos\nuos.remove('${ filePath } ')`
280
286
} )
281
- console . log ( output )
287
+ return this . exit_raw_repl ( )
288
+ }
289
+ return Promise . reject ( )
290
+ }
291
+
292
+ async fs_rename ( oldFilePath , newFilePath ) {
293
+ if ( oldFilePath && newFilePath ) {
294
+ await this . enter_raw_repl ( )
295
+ const output = await this . exec_raw ( {
296
+ command : `import uos\nuos.rename('${ oldFilePath } ', '${ newFilePath } ')`
297
+ } )
282
298
return this . exit_raw_repl ( )
283
299
}
284
300
return Promise . reject ( )
0 commit comments