@@ -11,11 +11,10 @@ function sleep(millis) {
11
11
} )
12
12
}
13
13
14
- function escape_string ( string ) {
15
- string = string . replace ( / \' / g, `\\'` )
16
- string = string . replace ( / \" / g, `\\"` )
17
- string = string . replace ( / " " " / g, `\\"\\"\\"` )
18
- return string
14
+ function fixLineBreak ( str ) {
15
+ // All line breaks must be converted to \n
16
+ // https://stackoverflow.com/questions/4025760/python-file-write-creating-extra-carriage-return
17
+ return str . replace ( / \r \n / g, '\n' )
19
18
}
20
19
21
20
class MicroPythonBoard {
@@ -226,7 +225,7 @@ class MicroPythonBoard {
226
225
let output = await this . exec_raw ( { command : command } )
227
226
await this . exit_raw_repl ( )
228
227
// Convert text output to js array
229
- output = output . replace ( / ' / g, '"' ) ;
228
+ output = output . replace ( / ' / g, '"' )
230
229
output = output . split ( 'OK' )
231
230
let files = output [ 2 ] || ''
232
231
files = files . slice ( 0 , files . indexOf ( ']' ) + 1 )
@@ -238,7 +237,7 @@ class MicroPythonBoard {
238
237
if ( filePath ) {
239
238
await this . enter_raw_repl ( )
240
239
const output = await this . exec_raw ( {
241
- command : `with open('${ filePath } ', 'r') as f:\n while 1:\n b=f.read(256)\n if not b:break\n print(b,end='')`
240
+ command : `with open('${ filePath } ','r') as f:\n while 1:\n b=f.read(256)\n if not b:break\n print(b,end='')`
242
241
} )
243
242
await this . exit_raw_repl ( )
244
243
const outputArray = output . split ( 'raw REPL; CTRL-B to exit\r\n>OK' )
@@ -256,7 +255,8 @@ class MicroPythonBoard {
256
255
command : `f=open('${ dest } ','w')\nw=f.write`
257
256
} )
258
257
await sleep ( 100 )
259
- const contentString = contentBuffer . toString ( )
258
+ let contentString = contentBuffer . toString ( )
259
+ contentString = fixLineBreak ( contentString )
260
260
const hexArray = contentString . split ( '' ) . map (
261
261
c => c . charCodeAt ( 0 ) . toString ( 16 ) . padStart ( 2 , '0' )
262
262
)
@@ -275,20 +275,22 @@ class MicroPythonBoard {
275
275
276
276
async fs_save ( content , dest ) {
277
277
if ( content && dest ) {
278
- if ( typeof content === 'string' ) {
279
- content = Buffer . from ( content )
280
- }
278
+ content = fixLineBreak ( content )
281
279
await this . enter_raw_repl ( )
282
280
let output = await this . exec_raw ( {
283
281
command : `f=open('${ dest } ','w')\nw=f.write`
284
282
} )
285
- for ( let i = 0 ; i < content . length ; i += 64 ) {
286
- let slice = content . slice ( i , i + 64 )
287
- slice = slice . toString ( )
288
- slice = escape_string ( slice )
289
- await this . serial . write ( `w("""${ slice } """)\n` )
290
- await this . serial . write ( `\x04` )
291
- await sleep ( 50 )
283
+ await sleep ( 100 )
284
+ const hexArray = content . split ( '' ) . map (
285
+ c => c . charCodeAt ( 0 ) . toString ( 16 ) . padStart ( 2 , '0' )
286
+ )
287
+ const chunkSize = 256
288
+ for ( let i = 0 ; i < hexArray . length ; i += chunkSize ) {
289
+ let slice = hexArray . slice ( i , i + chunkSize )
290
+ let bytes = slice . map ( h => `0x${ h } ` )
291
+ let line = `w(bytes([${ bytes . join ( ',' ) } ]))\x04`
292
+ await this . serial . write ( line )
293
+ await sleep ( 100 )
292
294
}
293
295
return this . exit_raw_repl ( )
294
296
} else {
0 commit comments