10000 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
Show file tree
Hide file tree
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
46 changes: 32 additions & 14 deletions micropython.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,32 @@ class MicroPythonBoard {
this.device = null
this.serial = null
this.in_raw_repl = false
this.chunk_size = 200
this.chunk_sleep = 100
}

listPorts() {
return SerialPort.list()
}

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)
this.serial.drain((err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}

async open(device) {
if (device) {
this.device = device
Expand Down Expand Up @@ -94,11 +114,11 @@ class MicroPythonBoard {
enter_raw_repl(timeout) {
return new Promise(async (resolve, reject) => {
// ctrl-C twice: interrupt any running program
await this.serial.write(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.serial.write(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 @@ -117,7 +137,7 @@ class MicroPythonBoard {
async exit_raw_repl() {
if (this.in_raw_repl) {
// ctrl-B: enter friendly REPL
await this.serial.write(Buffer.from(`\r\x02`))
await this.write_and_drain(Buffer.from(`\r\x02`))
this.in_raw_repl = false
}
return Promise.resolve()
Expand All @@ -135,7 +155,6 @@ class MicroPythonBoard {
})
}


exec_raw_no_follow(options) {
const { timeout = null, command = '' } = options || {}
return new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -173,7 +192,7 @@ class MicroPythonBoard {
}

async eval(k) {
return await this.serial.write(Buffer.from(k))
return this.serial.write(Buffer.from(k))
}

async stop() {
Expand Down Expand Up @@ -288,14 +307,14 @@ class MicroPythonBoard {
const hexArray = contentString.split('').map(
c => c.charCodeAt(0).toString(16).padStart(2, '0')
)
const chunkSize = 256
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.serial.write(line)
await sleep(100)
await this.write_and_drain(line)
await sleep(this.chunk_sleep)
}
return this.exit_raw_repl()
}
Expand All @@ -308,21 +327,20 @@ class MicroPythonBoard {
content = fixLineBreak(content)
await this.enter_raw_repl()
let output = await this.exec_raw({
command: `f=open('${dest}','w')\nw=f.write`,
data_consumer: (d) => console.log('data consumer', d)
command: `f=open('${dest}','w')\nw=f.write`
})
await sleep(100)
const hexArray = content.split('').map(
c => c.charCodeAt(0).toString(16).padStart(2, '0')
)
const chunkSize = 256
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.serial.write(line)
await sleep(100)
data_consumer( parseInt((i / hexArray.length) * 100) + '%' )
await this.write_and_drain(line)
await sleep(await sleep(this.chunk_sleep))
}
return this.exit_raw_repl()
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "micropython.js",
"version": "1.3.2",
"version": "1.3.3",
"description": "Interpretation of pyboard.py in javascript",
"main": "micropython.js",
"scripts": {
Expand Down
0