8000 Add reference to serial and async functions · arduino/micropython.js@9da6c20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9da6c20

Browse files
committed
Add reference to serial and async functions
1 parent d11251c commit 9da6c20

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

micropython.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,25 @@ function sleep(millis) {
1414
class MicroPythonBoard {
1515
constructor() {
1616
this.device = null
17+
this.serial = null
1718
this.in_raw_repl = false
1819
}
1920

2021
listPorts() {
2122
return SerialPort.list()
2223
}
2324

24-
open(device) {
25+
async open(device) {
2526
if (device) {
2627
this.device = device
2728
}
2829
if (!this.device) {
2930
throw new Error(`No device specified`)
3031
}
32+
if (this.serial && this.serial.isOpen) {
33+
await this.serial.close()
34+
this.serial = null
35+
}
3136

3237
this.serial = new SerialPort({
3338
path: this.device,
@@ -37,7 +42,7 @@ class MicroPythonBoard {
3742
})
3843

3944
return new Promise((resolve, reject) => {
40-
this.serial.open((err) => {
45+
this.serial.open(async (err) => {
4146
if (err) {
4247
reject(err)
4348
} else {
@@ -212,13 +217,14 @@ class MicroPythonBoard {
212217
let output = await this.exec_raw({
213218
command: `f=open('${dest}','w')\nw=f.write`
214219
})
215-
for (let i = 0; i < content.length; i+=64) {
216-
let slice = content.slice(i, i+64)
220+
await sleep(100)
221+
for (let i = 0; i < content.length; i+=128) {
222+
let slice = content.slice(i, i+128)
217223
slice = slice.toString()
218224
slice = slice.replace(/"""/g, `\\"\\"\\"`)
219225
await this.serial.write(`w("""${slice}""")`)
220226
await this.serial.write(`\x04`)
221-
await sleep(50)
227+
await sleep(100)
222228
}
223229
return this.exit_raw_repl()
224230
}
@@ -238,7 +244,8 @@ class MicroPythonBoard {
238244
let slice = content.slice(i, i+64)
239245
slice = slice.toString()
240246
slice = slice.replace(/"""/g, `\\"\\"\\"`)
241-
await this.serial.write(`w("""${slice}""")`)
247+
// slice = slice.replace(//g, ``)
248+
await this.serial.write(`w("""${slice}""")\n`)
242249
await this.serial.write(`\x04`)
243250
await sleep(50)
244251
}
@@ -266,7 +273,6 @@ class MicroPythonBoard {
266273
const output = await this.exec_raw({
267274
command: `import uos\nuos.rmdir('${filePath}')`
268275
})
269-
console.log(output)
270276
return this.exit_raw_repl()
271277
}
272278
return Promise.reject()
@@ -278,7 +284,17 @@ class MicroPythonBoard {
278284
const output = await this.exec_raw({
279285
command: `import uos\nuos.remove('${filePath}')`
280286
})
281-
console.log(output)
287+
return this.exit_raw_repl()
288+
}
289+
return Promise.reject()
290+
}
291+
292+
async fs_rename(oldFilePath, newFilePath) {
293+
if (oldFilePath && newFilePath) {
294+
await this.enter_raw_repl()
295+
const output = await this.exec_raw({
296+
command: `import uos\nuos.rename('${oldFilePath}', '${newFilePath}')`
297+
})
282298
return this.exit_raw_repl()
283299
}
284300
return Promise.reject()

0 commit comments

Comments
 (0)
0