8000 Adding data consumer callback for reporting progress · arduino/micropython.js@192d389 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 192d389

Browse files
committed
Adding data consumer callback for reporting progress
1 parent 1f630d2 commit 192d389

File tree

3 files changed

+175
-38
lines changed

3 files changed

+175
-38
lines changed

CLI.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* `--getfile fileA.py fileB.py`: Download `fileA.py` from the board to disk renaming it to `fileB.py`
1414
* `--removefile fileA.py`: Remove `fileA.py` from the board
1515
* `--removedir foldername`: Remove `foldername` from the board
16+
* `--verbose`: Prints extra logs
1617

1718
## Examples
1819

@@ -104,3 +105,107 @@ for i in range(0, 10):
104105
print('duh')
105106
```
106107

108+
### Verbose
109+
110+
```
111+
$ node cli.js --verbose --port /dev/ttyACM0 --putfile ./savetest.py savetest.py
112+
VERBOSE
113+
executing command:
114+
command --putfile
115+
arguments [ './savetest.py', 'savetest.py' ]
116+
port /dev/ttyACM0
117+
0%
118+
1%
119+
2%
120+
3%
121+
4%
122+
5%
123+
6%
124+
7%
125+
8%
126+
9%
127+
10%
128+
12%
129+
13%
130+
14%
131+
15%
132+
16%
133+
17%
134+
18%
135+
19%
136+
20%
137+
21%
138+
22%
139+
24%
140+
25%
141+
26%
142+
27%
143+
28%
144+
29%
145+
30% 9E81
146+
31%
147+
32%
148+
33%
149+
35%
150+
36%
151+
37%
152+
38%
153+
39%
154+
40%
155+
41%
156+
42%
157+
43%
158+
44%
159+
45%
160+
47%
161+
48%
162+
49%
163+
50%
164+
51%
165+
52%
166+
53%
167+
54%
168+
55%
169+
56%
170+
57%
171+
59%
172+
60%
173+
61%
174+
62%
175+
63%
176+
64%
177+
65%
178+
66%
179+
67%
180+
68%
181+
70%
182+
71%
183+
72%
184+
73%
185+
74%
186+
75%
187+
76%
188+
77%
189+
78%
190+
79%
191+
80%
192+
82%
193+
83%
194+
84%
195+
85%
196+
86%
197+
87%
198+
88%
199+
89%
200+
90%
201+
91%
202+
92%
203+
94%
204+
95%
205+
96%
206+
97%
207+
98%
208+
99%
209+
undefined
210+
command executed --putfile
211+
```

cli.js

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const fs = require('fs')
22
const path = require('path')
33
const Board = require('./micropython.js')
44

5+
const log = console.log
6+
57
const extractArguments = (args) => {
68
return args.slice(2)
79
}
@@ -34,7 +36,7 @@ const listPorts = (args) => {
3436
const board = new Board()
3537
board.listPorts()
3638
.then((ports) => {
37-
console.log('available ports', ports)
39+
log('available ports', ports)
3840
})
3941
}
4042

@@ -46,77 +48,82 @@ const listFiles = (args, port) => {
4648
const folder = args[0] || '/'
4749
try {
4850
const output = await board.fs_ls(folder)
49-
console.log(`files at "${folder}"`, output)
51+
log(`files at "${folder}"`, output)
5052
} catch(e) {
51-
console.log('error', e)
53+
log('error', e)
5254
}
5355
board.close()
5456
})
5557
}
5658

57-
const executeString = (args, port) => {
59+
const executeString = (args, port, dataConsumer) => {
5860
ensurePort(port)
5961
const board = new Board()
6062
const code = args[0] || ''
6163
board.open(port)
6264
.then(() => board.enter_raw_repl())
63-
.then(() => board.exec_raw({ command: code }))
65+
.then(() => board.exec_raw({ command: code, data_consumer: dataConsumer }))
6466
.then(async (out) => {
6567
await board.exit_raw_repl()
6668
await board.close()
67-
console.log(out)
69 F987 +
log(out)
6870
})
6971
.catch((err) => {
70-
console.log('error')
71-
console.log(err)
72+
log('error', err)
7273
board.exit_raw_repl(true)
7374
board.close()
7475
})
7576
}
7677

77-
const executeFile = (args, port) => {
78+
const executeFile = (args, port, dataConsumer) => {
7879
ensurePort(port)
7980
const board = new Board()
8081
const filename = args[0] || ''
82+
const consumer = dataConsumer || function() {}
8183
board.open(port)
8284
.then(async () => {
8385
try {
84-
const out = await board.execfile(filename)
85-
console.log(out)
86+
const out = await board.execfile(filename, consumer)
87+
log(out)
8688
} catch(e) {
87-
console.log('error', e)
89+
log('error', e)
8890
}
8991
board.close()
9092
})
9193
}
9294

93-
const putFile = (args, port) => {
95+
const putFile = (args, port, dataConsumer) => {
9496
ensurePort(port)
9597
const board = new Board()
9698
const [ diskFilename, boardFilename ] = args
97-
board.open(port)
99+
const consumer = dataConsumer || function() {}
100+
return board.open(port)
98101
.then(async () => {
99102
try {
100-
const out = await board.fs_put(diskFilename, boardFilename)
101-
console.log(out)
103+
const out = await board.fs_put(diskFilename, boardFilename, consumer)
104+
log(out)
102105
} catch(e) {
103-
console.log('error', e)
106+
log('error', e)
104107
}
105108
board.close()
109+
return Promise.resolve()
106110
})
107111
}
108112

109-
const getFile = (args, port) => {
113+
const getFile = (args, port, dataConsumer) => {
110114
ensurePort(port)
111115
const board = new Board()
112116
const [ boardFilename, diskFilename ] = args
117+
const consumer = dataConsumer || function() {}
113118
board.open(port)
114119
.then(async () => {
115120
try {
116-
let output = await board.fs_cat(boardFilename)
121+
let output = await board.fs_cat(boardFilename, consumer)
117122
fs.writeFileSync(diskFilename, output)
123+
log('output')
124+
log(output)
118125
} catch(e) {
119-
console.log('error', e)
126+
log('error', e)
120127
}
121128
board.close()
122129
})
@@ -131,9 +138,9 @@ const removeFile = (args, port) => {
131138
.then(async () => {
132139
try {
133140
const out = await board.fs_rm(boardFilename)
134-
console.log(out)
141+
log(out)
135142
} catch(e) {
136-
console.log('error', e)
143+
log('error', e)
137144
}
138145
board.close()
139146
})
@@ -148,9 +155,9 @@ const removeFolder = (args, port) => {
148155
.then(async () => {
149156
try {
150157
const out = await board.fs_rmdir(boardDirname)
151-
console.log(out)
158+
log(out)
152159
} catch(e) {
153-
console.log('error', e)
160+
log('error', e)
154161
}
155162
board.close()
156163
})
@@ -164,16 +171,34 @@ const operations = {
164171
'--putfile': putFile,
165172
'--getfile': getFile,
166173
'--removefile': removeFile,
167-
'--removefolder': removeFolder
174+
'--removefolder': removeFolder,
175+
'--verbose': () => false
168176
}
169177

170178
let args = extractArguments(process.argv)
171179
let commands = extractCommands(args)
172180
let port = commands['--port'] ? commands['--port'][0] : null
173181

174-
Object.keys(commands)
175-
.filter((command) => command !== '--port')
176-
.forEach((command) => {
177-
operations[command](commands[command], port)
178-
})
182+
if (commands['--verbose']) {
183+
log('VERBOSE')
184+
Object.keys(commands)
185+
.filter((command) => command !== '--port')
186+
.filter((command) => command !== '--verbose')
187+
.forEach((command) => {
188+
log('executing command:')
189+
log('command', command)
190+
log('arguments', commands[command])
191+
log('port', port)
192+
operations[command](commands[command], port, log)
193+
.then(() => log('command executed', command, `\r\n`))
194+
.catch((e) => log('error', e, `\r\n`))
195+
})
196+
} else {
197+
Object.keys(commands)
198+
.filter((command) => command !== '--port')
199+
.forEach((command) => {
200+
operations[command](commands[command], port)
201+
})
202+
}
203+
179204

micropython.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ class MicroPythonBoard {
163163
}
164164

165165
exec_raw(options) {
166-
const { timeout = null, command = '' } = options || {}
166+
const { timeout = null, command = '', data_consumer = () => false } = options || {}
167167
this.exec_raw_no_follow({
168168
timeout: timeout,
169-
command: command
169+
command: command,
170+
data_consumer: data_consumer
170171
})
171172
return this.follow({ timeout })
172173
}
@@ -187,12 +188,14 @@ class MicroPythonBoard {
187188
await this.serial.write(Buffer.from(`\x04`))
188189
}
189190

190-
async execfile(filePath) {
191+
async execfile(filePath, data_consumer) {
191192
if (filePath) {
192193
const content = fs.readFileSync(path.resolve(filePath))
193194
await this.enter_raw_repl()
194-
const output = await this.exec_raw({ command: content })
195-
console.log(output)
195+
const output = await this.exec_raw({
196+
command: content
197+
})
198+
data_consumer(output)
196199
return this.exit_raw_repl()
197200
}
198201
return Promise.reject()
@@ -247,7 +250,8 @@ class MicroPythonBoard {
247250
return Promise.reject(new Error(`Path to file was not specified`))
248251
}
249252

250-
async fs_put(src, dest) {
253+
async fs_put(src, dest, data_consumer) {
254+
data_consumer = data_consumer || function() {}
251255
if (src && dest) {
252256
const contentBuffer = fs.readFileSync(path.resolve(src))
253257
await this.enter_raw_repl()
@@ -265,6 +269,7 @@ class MicroPythonBoard {
265269
let slice = hexArray.slice(i, i+chunkSize)
266270
let bytes = slice.map(h => `0x${h}`)
267271
let line = `w(bytes([${bytes.join(',')}]))\x04`
272+
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
268273
await this.serial.write(line)
269274
await sleep(100)
270275
}
@@ -273,12 +278,13 @@ class MicroPythonBoard {
273278
return Promise.reject(new Error(`Must specify source and destination paths`))
274279
}
275280

276-
async fs_save(content, dest) {
281+
async fs_save(content, dest, data_consumer) {
277282
if (content && dest) {
278283
content = fixLineBreak(content)
279284
await this.enter_raw_repl()
280285
let output = await this.exec_raw({
281-
command: `f=open('${dest}','w')\nw=f.write`
286+
command: `f=open('${dest}','w')\nw=f.write`,
287+
data_consumer: (d) => console.log('data consumer', d)
282288
})
283289
await sleep(100)
284290
const hexArray = content.split('').map(
@@ -289,6 +295,7 @@ class MicroPythonBoard {
289295
let slice = hexArray.slice(i, i+chunkSize)
290296
let bytes = slice.map(h => `0x${h}`)
291297
let line = `w(bytes([${bytes.join(',')}]))\x04`
298+
data_consumer( parseInt((i / hexArray.length) * 100) + '%')
292299
await this.serial.write(line)
293300
await sleep(100)
294301
}

0 commit comments

Comments
 (0)
0