Skip to content

Tentative: Implemented binary output from fs_cat. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions micropython.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ function extract(out) {
return out.slice(2, -3)
}

function extractBytes(out, cut_before = 2, cut_after = 3) {
bytes = out.slice(cut_before, -cut_after)
bytes = bytes.split(',').map(Number)
return bytes
}

class MicroPythonBoard {
constructor() {
this.port = null
Expand Down Expand Up @@ -250,6 +256,19 @@ class MicroPythonBoard {
return Promise.resolve(files)
}

async fs_cat_binary(filePath) {
if (filePath) {
await this.enter_raw_repl()
let output = await this.exec_raw(
`with open('${filePath}','rb') as f:\n while 1:\n b=f.read(256)\n if not b:break\n print(",".join(str(e) for e in b),end=',')`
)
await this.exit_raw_repl()
output = extractBytes(output, 2, 4)
return Promise.resolve((output))
}
return Promise.reject(new Error(`Path to file was not specified`))
}

async fs_cat(filePath) {
if (filePath) {
await this.enter_raw_repl()
Expand Down