8000 Merge pull request #7 from arduino/bugfix/examples · arduino/micropython.js@b86f244 · GitHub
[go: up one dir, main page]

Skip to content

Commit b86f244

Browse files
authored
Merge pull request #7 from arduino/bugfix/examples
Bugfix examples
2 parents 8b03aa8 + d327baa commit b86f244

15 files changed

+1218
-199
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,40 @@
22

33
This is an partial port of [pyboard.py](https://docs.micropython.org/en/latest/reference/pyboard.py.html) to javascript.
44

5+
## Basic usage
6+
7+
```js
8+
const Board = require('micropython.js')
9+
// 8000 Instantiate board class
10+
const board = new Board()
11+
12+
// List available boards
13+
const ports = await board.listPorts()
14+
console.log('available boards', ports)
15+
16+
// Connect to a serial path
17+
await board.open('/dev/ttyUSB0')
18+
19+
// Enter raw repl, execute command, get output and leave raw repl
20+
await board.enter_raw_repl()
21+
const output = await board.exec_raw({ command: "print(123)" })
22+
await board.exit_raw_repl()
23+
24+
// List files on the board
25+
const rootFiles = await board.fs_ils()
26+
console.log('files at /', rootFiles)
27+
28+
// Close serial
29+
await board.close()
30+
```
31+
532
## Examples
633

7-
1. Install dependencies `npm install`
8-
2. Navigate to example folder `cd examples`
9-
3. Execute files with `PORT` environment variable: `PORT=/dev/tty.SLAB_USBtoUART node 05_list_files.js`
34+
1. Navigate to example folder `cd examples`
35+
2. Execute files with `PORT` environment variable: `PORT=/dev/tty.SLAB_USBtoUART node 05_list_files.js`
1036

1137
## Command Line Interface (CLI)
1238

13-
1. Install dependencies `npm install`
1439
1. Run CLI `node cli.js [ARGUMENTS]...`
1540

1641
[Read more](CLI.md)
17-

examples/00_list_ports.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const Board = require('../micropython.js')
22

3-
let board = new Board()
3+
async function main() {
4+
const board = new Board()
5+
const ports = await board.listPorts()
6+
console.log('available ports', ports)
7+
}
48

5-
board.listPorts()
6-
.then((ports) => {
7-
console.log('available ports', ports)
8-
})
9+
main()

examples/01_execute_string.js

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,28 @@
11
const Board = require('../micropython.js')
22

3-
let board = new Board()
4-
5-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
6-
.then(() => {
7-
console.log('connected')
8-
console.log('entering raw repl')
9-
return board.enter_raw_repl()
10-
})
11-
.then(async () => {
12-
console.log('executing raw')
13-
return board.exec_raw({
14-
command: `
15-
"""
16-
Warning: You may need to change the pin number
17-
"""
3+
const command = `from time import sleep
184
from machine import Pin
19-
from time import sleep
20-
pin = Pin(6, Pin.OUT)
5+
pin = Pin(2, Pin.OUT)
6+
print("start OK \\r\\n")
217
for i in range(0, 10):
22-
pin.on()
23-
sleep(0.1)
24-
pin.off()
25-
sleep(0.1)
26-
print('duh')
8+
print('duh')
9+
pin.on()
10+
sleep(0.1)
11+
pin.off()
12+
sleep(0.1)
2713
`
28-
})
29-
})
30-
.then((out) => {
31-
console.log('output', out)
32-
console.log('exiting raw repl')
33-
return board.exit_raw_repl()
34-
})
35-
.then(() => {
36-
console.log('closing connection')
37-
return board.close()
38-
})
39-
.then(() => {
40-
console.log('disconnected')
41-
})
42-
.catch((err) => {
43-
console.log(< 10000 /span>'error')
44-
console.log(err)
45-
board.exit_raw_repl(true)
46-
board.close()
47-
})
14+
15+
async function main() {
16+
const board = new Board()
17+
await board.open(process.env.PORT)
18+
await board.enter_raw_repl()
19+
console.log('exec raw:')
20+
console.log(command)
21+
const output = await board.exec_raw({ command })
22+
console.log('output:')
23+
console.log(output)
24+
await board.exit_raw_repl()
25+
await board.close()
26+
}
27+
28+
main()

examples/02_execute_file.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
const Board = require('../micropython.js')
22

3-
console.log('connect')
3+
async function main() {
4+
const board = new Board()
5+
await board.open(process.env.PORT)
6+
console.log('executing file')
7+
const output = await board.execfile('./test.py')
8+
console.log('output')
9+
console.log(output)
10+
await board.close()
11+
}
412

5-
let board = new Board()
6-
7-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
8-
.then(async () => {
9-
try {
10-
await board.execfile('./test.py')
11-
console.log('disconnect')
12-
} catch(e) {
13-
console.log('error', e)
14-
}
15-
board.close()
16-
})
13+
main()

examples/03_put_file.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
const Board = require('../micropython.js')
22

3-
console.log('connect')
4-
let board = new Board()
5-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
6-
.then(async () => {
7-
try {
8-
await board.fs_put('./test.py', 'test.py')
9-
console.log('disconnect')
10-
} catch(e) {
11-
console.log('error', e)
12-
}
13-
board.close()
14-
})
3+
async function main() {
4+
const board = new Board()
5+
await board.open(process.env.PORT)
6+
console.log('sending file to board')
7+
await board.fs_put('./big_file.py', 'test.py', console.log)
8+
console.log('done')
9+
await board.close()
10+
}
11+
12+
main()

examples/04_remove_file.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
const Board = require('../micropython.js')
22

3-
console.log('connect')
4-
let board = new Board()
5-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
6-
.then(async () => {
7-
try {
8-
await board.fs_rm('test.py')
9-
console.log('disconnect')
10-
} catch(e) {
11-
console.log('error', e)
12-
}
13-
board.close()
14-
})
3+
async function main() {
4+
const board = new Board()
5+
await board.open(process.env.PORT)
6+
console.log('removing file from board')
7+
await board.fs_rm('test.py')
8+
console.log('done')
9+
await board.close()
10+
}
11+
12+
main()

examples/05_list_files.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
const Board = require('../micropython.js')
22

3-
console.log('connect')
4-
5-
function extractFileArray(output) {
6-
output = output.replace(/'/g, '"');
7-
output = output.split('OK')
8-
let files = output[2] || ''
9-
files = files.slice(0, files.indexOf(']')+1)
10-
files = JSON.parse(files)
11-
return files
3+
async function main() {
4+
const board = new Board()
5+
await board.open(process.env.PORT)
6+
const rootFiles = await board.fs_ils()
7+
console.log('files at /')
8+
console.log(rootFiles)
9+
const libFiles = await board.fs_ils('lib')
10+
console.log('files at /lib')
11+
console.log(libFiles)
12+
await board.close()
1213
}
1314

14-
let board = new Board()
15-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
16-
.then(async () => {
17-
try {
18-
let output = await board.fs_ls()
19-
console.log('files at "/"', output)
20-
console.log('disconnect')
21-
} catch(e) {
22-
console.log('error', e)
23-
}
24-
try {
25-
let output = await board.fs_ls('lib')
26-
console.log('files at "/lib"', output)
27-
} catch(e) {
28-
console.log('error', e)
29-
}
30-
board.close()
31-
console.log('disconnect')
32-
})
15+
main()

examples/06_get_file_contents.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
const Board = require('../micropython.js')
22

3-
console.log('connect')
4-
let board = new Board()
5-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
6-
.then(async () => {
7-
try {
8-
let output = await board.fs_cat('test.py')
9-
console.log('file contents:')
10-
console.log(output)
11-
console.log('disconnect')
12-
} catch(e) {
13-
console.log('error', e)
14-
}
15-
board.close()
16-
})
3+
async function main() {
4+
const board = new Board()
5+
await board.open(process.env.PORT)
6+
const output = await board.fs_cat('test.py')
7+
console.log('file contents:')
8+
console.log(output)
9+
await board.close()
10+
}
11+
12+
main()

examples/07_save_file.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@ const Board = require('../micropython.js')
22

33
let content = `
44
"""
5-
Blinky
5+
Test
66
"""
7-
from machine import Pin
7+
88
from time import sleep
9-
# Nano Connect rp2040 internal LED
10-
led = Pin(6, Pin.OUT)
11-
while True:
12-
print('on')
13-
led.on()
14-
sleep(0.25)
15-
print('off')
16-
led.off()
17-
sleep(0.25)
9+
from machine import Pin
10+
pin = Pin(2, Pin.OUT)
11+
print("start OK \r\n")
12+
for i in range(0, 10):
13+
print('duh')
14+
pin.on()
15+
sleep(0.1)
16+
pin.off()
17+
sleep(0.1)
18+
1819
`
1920

20-
console.log('connect')
21-
let board = new Board()
22-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
23-
.then(async () => {
24-
try {
25-
await board.fs_save(content, 'test.py')
26-
console.log('disconnect')
27-
} catch(e) {
28-
console.log('error', e)
29-
}
30-
board.close()
31-
})
21+
async function main() {
22+
const board = new Board()
23+
await board.open(process.env.PORT)
24+
console.log('saving content to file')
25+
await board.fs_save(content, 'test.py')
26+
console.log('done')
27+
await board.close()
28+
}
29+
30+
main()

examples/08_file_exists.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
const Board = require('../micropython.js')
22

3-
console.log('connect')
4-
let board = new Board()
5-
board.open(process.env.PORT || '/dev/tty.usbmodem141101')
6-
.then(async () => {
7-
try {
8-
// Check for boot.py (probably exists)
9-
let output = await board.fs_exists('boot.py')
10-
if (output) {
11-
console.log('boot.py exists')
12-
} else {
13-
console.log('boot.py does not exists')
14-
}
3+
async function main() {
4+
const board = new Board()
5+
await board.open(process.env.PORT)
156

16-
// Check for xxx (probably won't exist)
17-
output = await board.fs_exists('xxx')
18-
if (output) {
19-
console.log('xxx exists')
20-
} else {
21-
console.log('xxx does not exists')
22-
}
23-
} catch(e) {
24-
console.log('error', e)
25-
}
26-
board.close()
27-
console.log('disconnect')
28-
})
7+
const testFileExists = await board.fs_exists('test.py')
8+
if (testFileExists) {
9+
console.log('test.py exists')
10+
} else {
11+
console.log('test.py does not exist')
12+
}
13+
14+
const fakeFileExists = await board.fs_exists('xxxxxxxxxxx')
15+
if (fakeFileExists) {
16+
console.log('xxxxxxxxxxx exists')
17+
} else {
18+
console.log('xxxxxxxxxxx does not exist')
19+
}
20+
21+
await board.close()
22+
}
23+
24+
main()

0 commit comments

Comments
 (0)
0