10000 reimplement execfile and add run · arduino/micropython.js@51fe0a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 51fe0a8

Browse files
committed
reimplement execfile and add run
1 parent f5cd531 commit 51fe0a8

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

micropython.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,16 @@ class MicroPythonBoard {
9898

9999
async write_and_read_until(cmd, expect, data_consumer) {
100100
this.serial.pause()
101+
const chunkSize = 128
102+
for (let i = 0; i < cmd.length; i+=chunkSize) {
103+
const s = cmd.slice(i, i+chunkSize)
104+
await this.serial.write(Buffer.from(s))
105+
await sleep(10)
106+
}
101107
let o
102-
await this.serial.write(Buffer.from(cmd))
103-
await sleep(10)
104-
if(expect) o = await this.read_until(expect, data_consumer)
108+
if(expect) {
109+
o = await this.read_until(expect, data_consumer)
110+
}
105111
await this.serial.flush()
106112
await sleep(10)
107113
this.serial.resume()
@@ -129,6 +135,25 @@ class MicroPythonBoard {
129135
return Promise.resolve(out)
130136
}
131137

138+
async execfile(filePath, data_consumer) {
139+
data_consumer = data_consumer || function() {}
140+
if (filePath) {
141+
const content = fs.readFileSync(path.resolve(filePath))
142+
await this.enter_raw_repl()
143+
const output = await this.exec_raw(content.toString(), data_consumer)
144+
await this.exit_raw_repl()
145+
return Promise.resolve(output)
146+
}
147+
return Promise.reject()
148+
}
149+
150+
async run(code, data_consumer) {
151+
data_consumer = data_consumer || function() {}
152+
await this.enter_raw_repl()
153+
const output = await this.exec_raw(code || '#', data_consumer)
154+
await this.exit_raw_repl()
155+
}
156+
132157
async eval(k) {
133158
await this.serial.write(Buffer.from(k))
134159
return Promise.resolve()

0 commit comments

Comments
 (0)
0