8000 Bugfix/write and drain by murilopolese · Pull Request #5 · arduino/micropython.js · GitHub
[go: up one dir, main page]

Skip to content

Bugfix/write and drain #5

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 5 commits into from
Jun 7, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add class properties for chunk size and sleep length
Also fixes casing on variables and methods
  • Loading branch information
murilopolese committed Jun 7, 2023
commit c9648561a2301d3b595ecbf66304a1f5eb2d0695
26 changes: 16 additions & 10 deletions micropython.js
AE1A
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ class MicroPythonBoard {
this.device = null
this.serial = null
this.in_raw_repl = false
this.chunk_size = 256
this.chunk_sleep = 100
}

listPorts() {
return SerialPort.list()
}

writeAndDrain(data) {
list_ports() { // backward compatibility
return this.listPorts()
}

write_and_drain(data) {
// https://serialport.io/docs/api-stream#drain-example
return new Promise((resolve, reject) => {
this.serial.write(data)
Expand Down Expand Up @@ -108,11 +114,11 @@ class MicroPythonBoard {
enter_raw_repl(timeout) {
return new Promise(async (resolve, reject) => {
// ctrl-C twice: interrupt any running program
await this.writeAndDrain(Buffer.from(`\r\x03\x03`))
await this.write_and_drain(Buffer.from(`\r\x03\x03`))
// flush input
await this.serial.flush()
// ctrl-A: enter raw REPL
await this.writeAndDrain(Buffer.from(`\r\x01`))
await this.write_and_drain(Buffer.from(`\r\x01`))

let data = await this.read_until({
ending: Buffer.from(`raw REPL; CTRL-B to exit\r\n>`),
Expand All @@ -131,7 +137,7 @@ class MicroPythonBoard {
async exit_raw_repl() {
if (this.in_raw_repl) {
// ctrl-B: enter friendly REPL
await this.writeAndDrain(Buffer.from(`\r\x02`))
await this.write_and_drain(Buffer.from(`\r\x02`))
this.in_raw_repl = false
}
return Promise.resolve()
Expand Down Expand Up @@ -301,14 +307,14 @@ class MicroPythonBoard {
const hexArray = contentString.split('').map(
c => c.charCodeAt(0).toString(16).padStart(2, '0')
)
const chunkSize = 128
const chunkSize = this.chunk_size
for (let i = 0; i < hexArray.length; i+= chunkSize) {
let slice = hexArray.slice(i, i+chunkSize)
let bytes = slice.map(h => `0x${h}`)
let line = `w(bytes([${bytes.join(',')}]))\x04`
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
await this.writeAndDrain(line)
await sleep(100)
await this.write_and_drain(line)
await sleep(this.chunk_sleep)
}
return this.exit_raw_repl()
}
Expand All @@ -327,14 +333,14 @@ class MicroPythonBoard {
const hexArray = content.split('').map(
c => c.charCodeAt(0).toString(16).padStart(2, '0')
)
const chunkSize = 128
const chunkSize = this.chunk_size
for (let i = 0; i < hexArray.length; i+= chunkSize) {
let slice = hexArray.slice(i, i+chunkSize)
let bytes = slice.map(h => `0x${h}`)
let line = `w(bytes([${bytes.join(',')}]))\x04`
data_consumer( parseInt((i / hexArray.length) * 100) + '%' )
await this.writeAndDrain(line)
await sleep(100)
await this.write_and_drain(line)
await sleep(await sleep(this.chunk_sleep))
}
return this.exit_raw_repl()
} else {
Expand Down
0