raspberrypi / pico-micropython-examples

Examples to accompany the "Raspberry Pi Pico Python SDK" book.
BSD 3-Clause "New" or "Revised" License
1.01k stars 228 forks source link

pio/pio_blink.py: fix missing rp2 import. #14

Closed hoihu closed 3 years ago

hoihu commented 3 years ago

Example did run when copy&pasting in REPL, but did not when importing the file

aallan commented 3 years ago

Just tested in Thonny with the launch version of MicroPython, pico_micropython_20210121.uf2. Code works as described, and the LED blinks for 3 seconds. There is an PIO issue with the latest release of MicroPython, see micropython/micropython#6837 for details. Try installing the launch version and see if that helps?

hoihu commented 3 years ago

Thanks for testing, I'm aware of the issue you've linked, but this is not related to it. I'm using a local build that doesn't have this problem.

The problem really is that rp2 is not imported in the namespace of the file. It will work when copy&pasting via REPL / raw REPL (and perhaps Thonny is doing that?)

So to recreate the issue: copy over the pio_blink.py file, then issue the following on the REPL:

Type "help()" for more information.
>>> import pio_blink
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pio_blink.py", line 7, in <module>
NameError: name 'rp2' isn't defined
>>> 
lurch commented 3 years ago

@aallan I agree that this fix is "correct". All our micropython PIO examples are either using import rp2 at the top, and then referencing e.g. rp2.asm_pio, rp2.PIO.OUT_LOW and rp2.StateMachine in the code; or they're doing from rp2 import PIO, StateMachine, asm_pio at the top, and then referencing asm_pio, PIO.OUT_LOW and StateMachine in the code.

This PR makes pio_blink.py consistent with all our other examples. Also, it seems like https://github.com/raspberrypi/pico-micropython-examples/blob/master/pio/pio_spi.py is also missing an import rp2 line.

aallan commented 3 years ago

This PR makes pio_blink.py consistent with all our other examples. Also, it seems like https://github.com/raspberrypi/pico-micropython-examples/blob/master/pio/pio_spi.py is also missing an import rp2 line.

Okay thanks.