BradenM / micropy-stubs

Automatically Generated Stub Packages for Micropy-Cli and whomever else
Other
29 stars 6 forks source link

Stubs for Raspberry Pi Pico #14

Open xyleth opened 3 years ago

xyleth commented 3 years ago

Hi!

Having found this wonderful tool I wanted to try and generate stubs for my shiny new Rasberry Pi Pico. However, I fell at the first hurdle as it seems that the Pico does not have all of the MicroPython modules required to enable the generation of the stubs. It first fails with an import error for the errno module, which is relatively easily fixed by copying the python source for that module onto the board.

The much bigger problem, and where I've hit the wall, is that the MicroPython port for the pico does not have the ujson module, which is required by the stubber. Presumably, as this board has no network access the Pi foundation saw no reason to include a network-focused serialization mechanism like JSON. This does leave us a bit out of luck when it comes to generating stubs though.

As far as I can tell ujson is a C module and has to be included at compile time of the MicroPython firmware. Short of compiling my own custom firmware for the Pico, which could be fun, is there another way to make this work? I'm pretty proficient with CPython, and I've done some C for Arduino before, but this is my first venture into MicroPython.

If not is there an existing set of stubs for a board with a similar feature set that might be appropriate to use?

BradenM commented 3 years ago

Hi @xyleth, sorry for my late reply.

Im not familiar with running micropython on the pi pico (or the pi pico for that matter), but I'll try and see what I can find out about it.

Are you running the official micropython firmware or one of the many forks?

Also, when running micropython on the pico, are you flashing firmware to the device (similar to say, an espressif microcontroller or the pyboard) or are you just utilizing the Unix build of micropython? Thanks.

snympi commented 3 years ago

I found this issue looking for exactly the same Pi Pico stub. I want to use VS Code with micropy as main development tool for that platform. This would lift the currently supported / recommended tool quality significantly.

@BradenM it seems that the Pico is running a customised build of micropython. It is flashed by putting the Pico in bootloader mode and copying the UF2 file to the USB Storage Device (bootloader presents the devices as storage).

xyleth commented 3 years ago

At the moment I'm running the official firmware. However, my research has suggested that what I need to do is compile a custom firmware with the missing modules needed to run the stubber baked in. I'm going to give that a go this weekend, I've been a bit distracted this week.

Thank you for coming back at all @BradenM, I recognize this is a community project so I don't expect instant responses!

BradenM commented 3 years ago

@xyleth @snympi,

Thank you for the info and consideration.

After doing some research on the pico and reading your comment, I can certainly see the value in adding support.

I managed to place an order for a pico so I can get my hands on one, but unfortunately many places were either sold out or on backorder, therefore the fastest vendor I could find has an estimated arrival date of late February.

In the meantime, I'm interested in what @xyleth is able to figure out and am happy to help anyway I can.

If you are unable or run into complications in compiling a custom build with the needed modules, feel free to provide a list of the missing modules and I'd be willing to give it a try.

It may also be beneficial to peek at the source code on my other repo, BradenM/micropy-build, where I wrote some per platform dockerfiles that make an attempt at being agnostic to which branch of micropython is being compiled. They are far from perfect and may be slightly out of date, but nevertheless could help.

cpwood commented 3 years ago

Hi all,

I've created the repo cpwood/Pico-Stub in the last few days. I'm actually here trying to figure out how to get them included in micropy-cli!

I've also released an extension named Pico-Go that will allow you to develop in VS Code in MicroPython. It's a derivative of Pymakr.

Write-ups on how to use these are available here and here.

FWIW, the main obstacle in generating the stubs was that the Pico doesn't support ujson, so I had to do a bit of a polyfill to get it to work.

So back to the point of my being here.. can somebody clue me into the next steps for getting the Pico stubs included in micropy-cli so users can install them directly from the CLI without having to manually download them first?

Thanks very much!

--

Just as a side note, I did look at getting micropy-cli itself to produce the stubs and got it to work semi-successfully. The main issues I ran into were:

  1. ujson not supported - needed polyfilling;
  2. errno not supported - required the source being on the board;
  3. Couldn't easily add the Pico rp2 module to the stubbing process - think this required a patch, but couldn't establish what was required to do this.

With Micropython-Stubber, the dependency on errno wasn't there and it was easier to add rp2, though it did require a bit of JSON manipulation afterwards for micropy-cli to recognise the stubs (directory re-organisation and adding a firmware property under the firmware object in info.json).

Wouldn't be offended if somebody wanted to build the ability to build the stubs into micropy-cli, though it would need some changes to the current code:

snympi commented 3 years ago

@cpwood I tried your extension and stubs separately. Works great and it would be very useful if the stubs were part of micropy-cli as you suggested. @BradenM will need to guide you through the process because I ended up here because of the same reason as you - wanted to use VS Code for Pico development instead of Thonny.

cpwood commented 3 years ago

Just as a follow-up, I've just updated the stubs to include fuller auto-completion (i.e. parameters) and also guidance for the machine and rp2 modules, including all the PIO stuff. I've also fixed an issue whereby you'd get errors from pylint when dealing with arrays since the stub had neither a __getitem__ or __setitem__ defined.

BradenM commented 3 years ago

Sorry for my absence, was out of town past couple of days.

@cpwood, this is great! As for exposing the stubs to micropy-cli, the first thing that comes to mind is to create a stub package in this repository.

I know the documentation is rather sparse, but checkout the example directory to see an example definition for a stub package.

Although, IIRC, it will expect that the Pico micropython branch is open source... but I recently saw a new release of micropython official v1.14, which mentioned something about an rpi port. Do you know if this is the pico port in question?

I will look into it myself further tomorrow, but am curious to see what you guys know. Thanks.

panchalnikunj commented 3 years ago

https://www.youtube.com/channel/UCA4j-J-TFW_9Dt4iTwIjtYQ

Once visit this channel for raspberry pi pico projects.