8000 Implement ls and cat to resolve with output and update examples · arduino/micropython.js@8e5f4e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e5f4e7

Browse files
committed
Implement ls and cat to resolve with output and update examples
1 parent 28fa5a6 commit 8e5f4e7

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

examples/05_list_files.js

Lines changed: 18 additions & 1 deletion
< 10000 td data-grid-cell-id="diff-0190d1e676a7b28be4df0763eac9e5c48918eeeb59a9dcadbe0da12c95f4e356-3-17-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
const Board = require('../micropython.js')
22

33
console.log('connect')
4+
5+
function extractFileArray(output) {
6+
output = output.replace(/'/g, '"');
7+
output = output.split('OK')
8+
let files = output[2] || ''
9+
// let files = output.find((data) => {
10+
// if (data.indexOf('[') === 0 && data.indexOf(']') !== -1) {
11+
// return true
12+
// }
13+
// })
14+
files = files.slice(0, files.indexOf(']')+1)
15+
files = JSON.parse(files)
16+
return files
17+
}
18+
419
let board = new Board()
520
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
621
.then(async () => {
722
try {
8-
await board.fs_ls()
23+
let output = await board.fs_ls()
24+
let files = extractFileArray(output)
25+
console.log('files', files)
926
console.log('disconnect')
1027
} catch(e) {
1128
console.log('error', e)

examples/06_get_file_contents.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ let board = new Board()
55
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
66
.then(async () => {
77
try {
8-
await board.fs_cat('test.py')
8+
let output = await board.fs_cat('test.py')
9+
output = output.split('OK')
10+
console.log('file contents:')
11+
console.log(output[2])
912
console.log('disconnect')
1013
} catch(e) {
1114
console.log('error', e)

micropython.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ class MicroPythonBoard {
189189
const output = await this.exec_raw({
190190
command: `import uos\nprint(uos.listdir())`
191191
})
192-
console.log(output)
193-
return this.exit_raw_repl()
192+
await this.exit_raw_repl()
193+
return Promise.resolve(output)
194194
}
195195

196196
async fs_cat(filePath) {
@@ -199,30 +199,32 @@ class MicroPythonBoard {
199199
const output = await this.exec_raw({
200200
command: `with open('${filePath}') as f:\n while 1:\n b=f.read(256)\n if not b:break\n print(b,end='')`
201201
})
202-
console.log(output)
203-
return this.exit_raw_repl()
202+
await this.exit_raw_repl()
203+
return Promise.resolve(output)
204204
}
205-
return Promise.reject()
205+
return Promise.reject(new Error(`Path to file was not specified`))
206206
}
207207

208208
async fs_put(src, dest) {
209-
if (src) {
209+
if (src && dest) {
210210
const content = fs.readFileSync(path.resolve(src))
211211
await this.enter_raw_repl()
212212
let output = await this.exec_raw({
213213
command: `f=open('${dest}','w')\nw=f.write`
214214
})
215215
for (let i = 0; i < content.length; i+=64) {
216216
let slice = content.slice(i, i+64)
217-
slice = slice.toString()
217+
slice = slice.toString()
218218
slice = slice.replace(/"""/g, `\\"\\"\\"`)
219219
await this.serial.write(`w("""${slice}""")`)
220220
await this.serial.write(`\x04`)
221221
await sleep(50)
222222
}
223223
return this.exit_raw_repl()
224224
}
225-
return Promise.reject()
225+
return Promise.reject(new Error(`Must specify source and destination paths`))
226+
}
227+
226228
}
227229

228230
async fs_mkdir() {

0 commit comments

Comments
 (0)
0