I’ve been interested in giving Zig a spin for quite a while, and once my Raspberry Pi Zero W2 and OLED display3 arrived in the post, I decided that this would be my best opportunity to try it out. I’m not really going to cover the process of wiring up the hardware, suffice to say that once you’ve got your Pi Zero you’ll need to be able to SSH into it, and that you’ll need a [solderless] GPIO header4 to plug the OLED display into. I recommend the Zero W because the W means ‘WiFi’, which means that if you connect it to your network you can SSH in without faffing around with USB cables and what not. It’s not a requirement, though.
With that out of the way, let’s see if we can write something in Zig to power this little display. It’s going to be a simple program that simply fills the entire screen by turning the pixels from black (off) to white (on). As an extra challenge, we will do this without pulling in dependencies like WiringPi5, or relying on existing drivers, as lovely as they are.
A half-hour to learn Zig
Futureproof is a live editor for GPU shaders, built on Zig, Neovim, and WebGPU.
It’s designed for a quick feedback loop, recompiling shaders and marking errors live!
The name is tongue-in-cheek, because it builds on as many unproven new technologies as possible:
It is written in Zig, which is a “better C”
The editor is an embedded Neovim, which is a modernized Vim
Graphics are done with WebGPU, which is an in-development next-gen API
The system also uses FreeType for font rasterization and GLFW for windowing, but those are both relatively mature, so I don’t get any points for them.
Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Warning: the latest major release is 0.7 - Zig is still pre-1.0; usage in production is still not recommended and you may run into compiler bugs.
To follow this guide, we assume you have:
Prior experience programming
Some understanding of low-level programming concepts
Knowing a language like C, C++, Rust, Go, Pascal or similar will be helpful for following this guide. You should have an editor, terminal and internet connection available to you. This guide is unofficial and unaffiliated with the project, and is designed to be read in order from the start.
This release solves 69 bugs.
The Zig programming language is a relatively recent entrant into the “systems programming” realm; it looks to interoperate with C, while adding safety features without sacrificing performance. The language has been gaining some attention of late and has announced progress toward a Zig compiler written in Zig in September. That change will allow LLVM to become an optional component, which will be a big step forward for the “maturity and stability” of Zig.