I don’t want to exactly copy the code from the Micropython calendar, because part of the point of Yarg is that it makes using an event-loop style of programming much easier.
For Box 3, two buttons are supplied, and that sounds a lot like my button.ya test code. It’s lightly edited, so that the GPIO lines are the same as ThePiHut suggest:
Running it today, on 0.2.1, it responds to the first interrupt only, and then halts with a fatal exception. That’s a puzzle, as the test code was working when I made the release!
So I will break out the debugger tomorrow, and see what is going wrong.
This is almost the same as hello_led, but I have removed a couple of the constant definitions, to minimise typing.
Of course, typing line-by-line will get old quick, so we need a way to load a whole Yarg file at once. That’s the job of hostyarg, which currently requires go to be installed. This tool will take an existing Yarg uf2 file and add a Yarg file to it.
So head over to the go website, and install go for your O/S. Follow the release README to then install hostyarg.
Once you have that, you’ll want to flash an LED like the MicroPython folks. Let’s stop poking raw registers for GPIO, and use a library. In fact we’ll use two – one for GPIO (called, ‘gpio’!), and one for a sleep function (‘timer’).
import("gpio");
import("timer");
const blocky_LED = 0d14;
gpio_init(blocky_LED);
gpio_set_direction(blocky_LED, GPIO_OUT);
var n = 4;
while (n > 0) {
gpio_put(blocky_LED, !gpio_get(blocky_LED));
sleep_ms(500);
n = n - 1;
}
Yarg files are plain text (officially utf8, but ascii will be fine for this example).
We need to use hostyarg to add it to the uf2 from the release. First we make a copy (I’ll assume you’re using the repo’s file structure), and then add the file:
box2.uf2 now can be flashed onto the pico in the usual way. Once that boots up, and presents the > prompt (maybe press return to get it), use the import function to run it:
> import("flash");
And we see the LED flash! That’s it for box2.
You’ll see a box2.uf2 in the repo if you can’t get hostyarg working. If you want to edit the flash.ya file, you’ll need to copy the base uf2 and use addle each time. There’s no way to edit an existing file at this time.
There are 12 boxes in the PiHut Let it Glow calendar. The first couple of days also illustrate that Yarg will need a bit of work for some of them. As such, I won’t be posting every calendar day! I’m also going to consider this post series successful if I complete them in some of the time after Christmas. There is a lot on at this time of year, and I’ve done no preparation for these posts.