Skip to content

Commit 2be7853

Browse files
committed
Merge remote-tracking branch 'adafruit/main' into audiobusio_examples
2 parents acb4fcb + 342c1c1 commit 2be7853

File tree

10 files changed

+184
-12
lines changed

10 files changed

+184
-12
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ jobs:
5050
- name: Pre-commit hooks
5151
run: |
5252
pre-commit run --all-files
53-
- name: PyLint
54-
run: |
55-
pylint $( find . -path './adafruit*.py' )
56-
([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace $( find . -path "./examples/*.py" ))
5753
- name: Build assets
5854
run: circuitpython-build-bundles --filename_prefix ${{ steps.repo-name.outputs.repo-name }} --library_location .
5955
- name: Archive bundles

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,18 @@ repos:
1717
- id: check-yaml
1818
- id: end-of-file-fixer
1919
- id: trailing-whitespace
20+
- repo: https://github.com/pycqa/pylint
21+
rev: pylint-2.7.1
22+
hooks:
23+
- id: pylint
24+
name: pylint (library code)
25+
types: [python]
26+
exclude: "^(docs/|examples/|setup.py$)"
27+
- repo: local
28+
hooks:
29+
- id: pylint_examples
30+
name: pylint (examples code)
31+
description: Run pylint rules on "examples/*.py" files
32+
entry: /usr/bin/env bash -c
33+
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
34+
language: system

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ ignore-comments=yes
250250
ignore-docstrings=yes
251251

252252
# Ignore imports when computing similarities.
253-
ignore-imports=no
253+
ignore-imports=yes
254254

255255
# Minimum lines number of a similarity.
256256
min-similarity-lines=4

adafruit_pioasm.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
def assemble(text_program):
3333
"""Converts pioasm text to encoded instruction bytes"""
34-
# pylint: disable=too-many-branches,too-many-statements
34+
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
3535
assembled = []
3636
program_name = None
3737
labels = {}
@@ -55,7 +55,10 @@ def assemble(text_program):
5555
elif line.startswith(".side_set"):
5656
sideset_count = int(line.split()[1])
5757
elif line.endswith(":"):
58-
labels[line[:-1]] = len(instructions)
58+
label = line[:-1]
59+
if label in labels:
60+
raise SyntaxError(f"Duplicate label {repr(label)}")
61+
labels[label] = len(instructions)
5962
elif line:
6063
# Only add as an instruction if the line isn't empty
6164
instructions.append(line)
@@ -85,10 +88,13 @@ def assemble(text_program):
8588
elif instruction[0] == "jmp":
8689
# instr delay cnd addr
8790
assembled.append(0b000_00000_000_00000)
88-
if instruction[-1] in labels:
89-
assembled[-1] |= labels[instruction[-1]]
91+
target = instruction[-1]
92+
if target[:1] in "0123456789":
93+
assembled[-1] |= int(target)
94+
elif instruction[-1] in labels:
95+
assembled[-1] |= labels[target]
9096
else:
91-
assembled[-1] |= int(instruction[-1])
97+
raise SyntaxError(f"Invalid jmp target {repr(target)}")
9298

9399
if len(instruction) > 2:
94100
assembled[-1] |= CONDITIONS.index(instruction[1]) << 5

examples/getting-started/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2020 Jeff Epler, written for Adafruit Industries
3+
4+
SPDX-License-Identifier: MIT
5+
-->
6+
7+
These examples are adapted from [Getting started with MicroPython on the Raspberry Pi Pico](https://www.adafruit.com/product/4898). You can also get an [electonic copy](https://hackspace.raspberrypi.org/books/micropython-pico/). PIO is covered in Appendix C.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Adapted from the an example in Appendix C of RPi_PiPico_Digital_v10.pdf
6+
7+
import time
8+
import board
9+
import rp2pio
10+
import adafruit_pioasm
11+
12+
led_quarter_brightness = adafruit_pioasm.assemble(
13+
"""
14+
set pins, 0 [2]
15+
set pins, 1
16+
"""
17+
)
18+
19+
led_half_brightness = adafruit_pioasm.assemble(
20+
"""
21+
set pins, 0
22+
set pins, 1
23+
"""
24+
)
25+
26+
led_full_brightness = adafruit_pioasm.assemble(
27+
"""
28+
set pins, 1
29+
"""
30+
)
31+
32+
while True:
33+
sm = rp2pio.StateMachine(
34+
led_quarter_brightness, frequency=10000, first_set_pin=board.LED
35+
)
36+
time.sleep(1)
37+
sm.deinit()
38+
39+
sm = rp2pio.StateMachine(
40+
led_half_brightness, frequency=10000, first_set_pin=board.LED
41+
)
42+
time.sleep(1)
43+
sm.deinit()
44+
45+
sm = rp2pio.StateMachine(
46+
led_full_brightness, frequency=10000, first_set_pin=board.LED
47+
)
48+
time.sleep(1)
49+
sm.deinit()

examples/pico-examples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2020 Jeff Epler, written for Adafruit Industries
3+
4+
SPDX-License-Identifier: MIT
5+
-->
6+
7+
These examples are adapted from the pio folder of the [Raspberry Pi Pico SDK Examples](https://github.com/raspberrypi/pico-examples).

examples/pico-examples/blink.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/pio_blink
6+
7+
import array
8+
import time
9+
import board
10+
import rp2pio
11+
import adafruit_pioasm
12+
13+
blink = adafruit_pioasm.assemble(
14+
"""
15+
.program blink
16+
pull block ; These two instructions take the blink duration
17+
out y, 32 ; and store it in y
18+
forever:
19+
mov x, y
20+
set pins, 1 ; Turn LED on
21+
lp1:
22+
jmp x-- lp1 ; Delay for (x + 1) cycles, x is a 32 bit number
23+
mov x, y
24+
set pins, 0 ; Turn LED off
25+
lp2:
26+
jmp x-- lp2 ; Delay for the same number of cycles again
27+
jmp forever ; Blink forever!
28+
"""
29+
)
30+
31+
32+
while True:
33+
for freq in [5, 8, 30]:
34+
with rp2pio.StateMachine(
35+
blink,
36+
frequency=125_000_000,
37+
first_set_pin=board.LED,
38+
wait_for_txstall=False,
39+
) as sm:
40+
data = array.array("I", [sm.frequency // freq])
41+
sm.write(data)
42+
time.sleep(3)
43+
time.sleep(0.5)

examples/pico-examples/hello.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Adapted from the example https://github.com/raspberrypi/pico-examples/tree/master/pio/hello_pio
6+
7+
import time
8+
import board
9+
import rp2pio
10+
import adafruit_pioasm
11+
12+
hello = """
13+
.program hello
14+
loop:
15+
pull
16+
out pins, 1
17+
; This program uses a 'jmp' at the end to follow the example. However,
18+
; in a many cases (including this one!) there is no jmp needed at the end
19+
; and the default "wrap" behavior will automatically return to the "pull"
20+
; instruction at the beginning.
21+
jmp loop
22+
"""
23+
24+
assembled = adafruit_pioasm.assemble(hello)
25+
26+
sm = rp2pio.StateMachine(
27+
assembled,
28+
frequency=2000,
29+
first_out_pin=board.LED,
30+
)
31+
print("real frequency", sm.frequency)
32+
33+
while True:
34+
sm.write(bytes((1,)))
35+
time.sleep(0.5)
36+
sm.write(bytes((0,)))
37+
time.sleep(0.5)

examples/pioasm_neopixel.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
import rp2pio
77
import board
8+
import microcontroller
89
import adafruit_pioasm
910

1011
# NeoPixels are 800khz bit streams. Zeroes are 1/3 duty cycle (~416ns) and ones
@@ -25,19 +26,30 @@
2526

2627
assembled = adafruit_pioasm.assemble(program)
2728

29+
# If the board has a designated neopixel, then use it. Otherwise use
30+
# GPIO16 as an arbitrary choice.
31+
if hasattr(board, "NEOPIXEL"):
32+
NEOPIXEL = board.NEOPIXEL
33+
else:
34+
NEOPIXEL = microcontroller.pin.GPIO16
35+
2836
sm = rp2pio.StateMachine(
2937
assembled,
3038
frequency=800000 * 6, # 800khz * 6 clocks per bit
31-
first_sideset_pin=board.D12,
39+
first_sideset_pin=NEOPIXEL,
3240
auto_pull=True,
3341
out_shift_right=False,
3442
pull_threshold=8,
3543
)
3644
print("real frequency", sm.frequency)
3745

38-
for i in range(100):
46+
for i in range(30):
3947
sm.write(b"\x0a\x00\x00")
4048
time.sleep(0.1)
49+
sm.write(b"\x00\x0a\x00")
50+
time.sleep(0.1)
51+
sm.write(b"\x00\x00\x0a")
52+
time.sleep(0.1)
4153
print("writes done")
4254

4355
time.sleep(2)

0 commit comments

Comments
 (0)