Use MicroPython decorators to drastically improve the speed of your scripts. In this episode of The Factory we double the speed of our GlowBit WS2812 matrix with "Viper" - an experimental MicroPython feature. We also fill you in on production news and show off some shots of our new machines in action.

Transcript

Welcome back to the factory. We have been doing some production this week.

First up, we've gone through some new GlowBit LED modules. We're finally getting into it. We're manufacturing the new GlowBit modules, this time with the black WS2812 LEDs, just to remove that visual impact of the white LEDs.

A little bit of construction work happening next door, but we're just gonna have to deal with it, so bear with me. Just so you can see now an in-the-flesh comparison between those LEDs. And I think that for our new line of GlowBit hardware, this is gonna be a really nice touch.

So we've got the 4x4 matrix, which tiles in one direction very easily. We've got the GlowBit stick, which tiles in one direction very easily as well. That has eight, that has 16 LEDs. Looking nice.

We're testing them right now, we're bagging them, and we should be releasing them pretty soon. Stay tuned.

We actually made the call with the 4x4 to remove all the front-facing artwork because all the pin labels are on the back. So we'll just keep that face nice and blank, nice and clean for everyone that wants a very clean display.

And I can tell you that using the new production line equipment is an absolute dream. The really, really big changes were the oven, which now has the rails, so we can go straight from the pick-and-place machine into the oven with no human interaction. And really importantly, the oven now feeds into the PCB loader. So once that PCB comes out of the oven, it just gets automatically loaded into a cassette, which for panels like these that only take a few minutes, that's a really big deal.

Before we'd have to be getting up every couple of minutes just to catch up. The panel as it comes out of the oven, but now it just gets automatically pushed into a PCB cassette and you'll see that that cassette just indexes down to the next row. And that gives us the capacity for 100 panels, theoretically 100 panels without any human interaction, which is, oh, it's gonna be good.

It's been a little while since we've released a PiicoDev module. We just released the PiicoDev three-channel capacitive touch sensor earlier this week, and we're hoping to get out the RGB module very soon, as well as the light sensor. So we'll be doing more productions in the near future.

Now here's the thing, putting LEDs on a PCB and soldering them is really the easy part. There's actually only a quarter of the effort that goes into making a product like this. Really what's going on behind the scenes with these GlowBit modules, it's all the work that Brenton's putting into them to get them really beautiful and easy to use in MicroPython.

Things like turning the LED index into an XY coordinate, so you can just treat the thing like a screen with XY pixels, getting really pleasing demos together so that your first experience is like really gratifying straight away. You can be inspired with what you can do with these modules.

And so in his journey in creating these drivers and these examples, Brenton's actually made what I think is a really non-trivial contribution to MicroPython. And that is he's sped up the WS2812 LED driver a lot. It's actually kind of bonkers. We started off with the example that was in the data sheet for the Raspberry Pi Pico, and we've made it at least a couple of times faster.

So I'll hand things over. To Brenton so he can walk you through his changes.

So over the last week, I have been hard at work trying to make GlowBit.py, the GlowBit MicroPython driver to allow you to get started with our GlowBit products as fast as possible on something like the Raspberry Pi Pico.

Now, one of the beautiful things about MicroPython is it is really, really easy to develop on compared to other programming methods like C or Arduino, but it comes at the cost as mostly being a little bit slower or significantly slower depending on what you're doing.

So one of the things I've been trying to work out is how to make MicroPython code run as fast as possible. The feature we'll be talking about today is the MicroPython Viper decorator.

Effectively, the MicroPython Viper decorator will compile a small snippet of MicroPython code, basically just a function that you've written, and it will compile it to the native machine code of the device you're running on.

So for the Raspberry Pi Pico we've got here, it will try to compile it to the machine code for the Cortex, the ARM Cortex M0+, which is an ARM thumb assembly device.

So let's look at the code we'll be demonstrating today. It's currently running the demonstration on your right there. We've started with the code that's on the Core Electronics website, just in the guide for getting started on the Raspberry Pi Pico with the WS2812, or the device that the GlowBits are made from.

And at the bottom of this code, I've got some code here that runs the demonstration in the background. I don't wanna talk too much about how this is working. At the moment, the code's a bit rushed and ugly. A more polished version of this is going.To be included in the GlowBit library. I do, however, want to talk about two particular functions.

One of them is this drawDrop function here, which is, as you can see, an unrolled loop, which draws several pixels to the screen at different X, Y coordinates.

And the other function here, panelTransform, which takes an X, Y coordinate on our three-by-three tiled matrix here and turns it into the address for that individual pixel.

You'll see at the top of these functions, I've got a commented outline that says, at micropython.viper. This is what's called a MicroPython decorator. Effectively, what's happening here is the MicroPython interpreter is calling a function called viper, and then that function, in turn, is running the code inside the function that we've written. This is all built into the MicroPython interpreter. And effectively, what this viper function does is it takes your MicroPython code and compiles it to machine code. It's basically a very basic assembly compiler, you know, Python to assembly compiler.

And down the bottom here, I've got a frames per second counter. At the moment, we're sitting around 10 frames per second, which is gonna be fine for a lot of animations, but let's see how fast we can get this to go.

Before we actually run it at full speed, we need to talk about a little bit of extra syntax that's been thrown in here. You'll see in the arguments list for this function, we've got this syntax here, which just tells the viper compiler, or tells Python, that X is going to be an integer. Likewise, our Y coordinate is also going to be an integer. And this little bit of syntax on the right here says that the return value willBe an integer. This is one of the compromises you need to expect if you want to try to write MicroPython Viper code. You have to specify what the argument data type is. Effectively, it needs to know what the data type of every single variable is, or it doesn't know what to do with it.

If we scroll down to our drawDrop function, it's got exactly the same thing. It's got a self argument because this is actually inside a class. I don't really wanna talk about that right now, it's beyond the scope of the video, but the X and Y coordinates that are being passed to it again, they are specified as integers.

So let's just quickly run this without this commented out, we'll bring in the MicroPython.Viper on that function, and we'll bring in the MicroPython.Viper there, and hit control R to run this. And you'll see straight away, we've jumped from 10 to about 22, 23 frames per second. This varies, it varies with the number of raindrops that are being drawn on the screen, but we've got almost a doubling in speed improvement just by using MicroPython.Viper decorators on two of our crucial functions.

There's a few things you should probably be aware of before you start jumping in and using MicroPython.Viper everywhere. One is that you can't have default values on your arguments. So if I say that X is by default zero here and try to run that, we're going to get an invalid syntax error. We get a syntax error.

If you're specifying the data type for a Viper function, you can't specify a default value. Another limit is that the return value can just be, basically it has to be an integer. There's a few other return values that it supports, but you're basically limited.To clarify, the transcript has been formatted into paragraphs as requested. Here is the formatted transcript:

MicroPython.Viper is a feature in MicroPython that allows you to write low-level code in Python. It is an experimental feature and has a few limitations. Firstly, it only works with functions and methods, not with classes or modules. Secondly, it only supports a subset of Python syntax and does not include features like exceptions or generators.

Another limitation is that it only allows you to return single values. You cannot return lists of Python objects or perform emulated floating point operations. If you try to use floating point calculations with MicroPython.Viper, you will encounter errors. Additionally, using MicroPython.Viper may break portability with CPython, causing errors when running the code on different platforms.

However, there are workarounds being developed, such as for the Glowbit library, which will address some of these limitations. It is important to be aware of the potential issues and limitations when using MicroPython.Viper, as it is still an experimental feature. If it works for your specific use case, that's great. If not, you may need to find alternative solutions. If you would like more information on how to use the MicroPython.Viper decorator, or have any other questions, please let us know.The documentation calls it the code emitter. We'll leave a link to the documentation in the description below. We've also been assembling our PiicoDev OLED module. And this is probably the most challenging module to date. It actually requires a hand assembly step. The final step in the process after we pick and place the boards, we run them through the oven, we clean them, we dry them. There's still one more process to do before testing, which is to solder on the OLED module and then tape it down in place and then test. And so this is a manual operation for now. There are machines that will do this, but man, at the moment, it's such a niche application, soldering this 30 pin flat flex cable, that for now we're doing it by hand with like this T-bar soldering tip.

So we soldered that in place in a jig that we made just out of some FR4, and then we tape the OLED module down and test it in our jig. And that's just a test pattern where we illuminate every pixel and then just like remove or print in black Picodev, just so we know that the data that's coming through is sensible. It's not just blanking white for some other reason. So we're working through all our productions on Picodev, Globit, Makerverse, and that means that you want to see a bunch of juicy new hardware hitting the website very soon. Stay tuned.

So there you have it, a bunch of new hardware goodies and some major, major improvements to the MicroPython WS2812 drivers. If you have any questions or if you just want to have a chat, open a thread in the Core Electronics forums. And until next time, thanks for watching.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.