8000 Write bytes to board and better parsing of fs_cat · arduino/micropython.js@8b0a4a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b0a4a0

Browse files
committed
Write bytes to board and better parsing of fs_cat
1 parent f25344e commit 8b0a4a0

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

cli.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ const getFile = (args, port) => {
124124
.then(async () => {
125125
try {
126126
let output = await board.fs_cat(boardFilename)
127-
output = output.split('OK')
128-
console.log(output[2])
129-
fs.writeFileSync(diskFilename, output[2])
127+
output = output.split('raw REPL; CTRL-B to exit\r\n>OK')
128+
const content = output[1].slice(0, -1)
129+
fs.writeFileSync(diskFilename, content)
130130
} catch(e) {
131131
console.log('error', e)
132132
}

examples/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from machine import Pin
66
from time import sleep
77
pin = Pin(6, Pin.OUT)
8-
print("start")
8+
print("start OK\r\n")
99
for i in range(0, 10):
1010
pin.on()
1111
sleep(0.1)

micropython.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class MicroPythonBoard {
232232
if (filePath) {
233233
await this.enter_raw_repl()
234234
const output = await this.exec_raw({
235-
command: `with open('${filePath}') as f:\n while 1:\n b=f.read(256)\n if not b:break\n print(b,end='')`
235+
command: `with open('${filePath}', 'r') as f:\n while 1:\n b=f.read(256)\n if not b:break\n print(b,end='')`
236236
})
237237
await this.exit_raw_repl()
238238
return Promise.resolve(output)
@@ -242,18 +242,22 @@ class MicroPythonBoard {
242242

243243
async fs_put(src, dest) {
244244
if (src && dest) {
245-
const content = fs.readFileSync(path.resolve(src))
245+
const contentBuffer = fs.readFileSync(path.resolve(src))
246246
await this.enter_raw_repl()
247247
let output = await this.exec_raw({
248248
command: `f=open('${dest}','w')\nw=f.write`
249249
})
250250
await sleep(100)
251-
for (let i = 0; i < content.length; i+=128) {
252-
let slice = content.subarray(i, i+128)
253-
slice = slice.toString()
254-
slice = escape_string(slice)
255-
await this.serial.write(`w("""${slice}""")`)
256-
await this.serial.write(`\x04`)
251+
const contentString = contentBuffer.toString()
252+
const hexArray = contentString.split('').map(
253+
c => c.charCodeAt(0).toString(16).padStart(2, '0')
254+
)
255+
const chunkSize = 256
256+
for (let i = 0; i < hexArray.length; i+= chunkSize) {
257+
let slice = hexArray.slice(i, i+chunkSize)
258+
let bytes = slice.map(h => `0x${h}`)
259+
let line = `w(bytes([${bytes.join(',')}]))\x04`
260+
await this.serial.write(line)
257261
await sleep(100)
258262
}
259263
return this.exit_raw_repl()

0 commit comments

Comments
 (0)
0