Pydafruit Gfx
pydafruit_gfx is a python tool to quickly prototype Adafruit GFX displays in python. (yep, python!)
I find it frustrating when building a display (with Arduino + hardware), you have to: flash the board, a cable can go bad, a wire pops loose, or the board can decides to brick itself. I end up spending 10–20 seconds (if I'm lucky) just to see if moving a font three pixels to the left "feels right".
I've always wanted a faster way to prototype display layouts and graphics, so I built this python tool.
Supplies
Using the Real GFX Code
Instead of mimicking Adafruit's GFX code, why not just use it.
Pydafruit Gfx uses:
- Adafruit GFX graphics code
- SDL (a display window that works on macOS, Linux, and Windows)
- pybind11
Using Adafruit's actual GFX code is great because there's no uncertainty. Did the font copy correctly? Is that function actually implemented correctly? So instead - just use their code. Their code is the source of truth.
SDL is a backend window that supports basic pixel drawing, which is great for this tool. SDL can draw any color we want, scale the display up (64x64 pixels is tiny on a computer screen), and save the output as images. Adafruit's GFX writes tot he SDL window. You do not need to install SDL, it is built into the pip wheel.
pybind11 turns the whole thing into a python library that you can just pip install.
I've tested the wheels on macOS, Windows, and ubuntu Linux, and they all worked. I'm sure there are some edge cases, unusual setups, or old OSs that could have issues.
Setup
Option 1: Install the Wheel (pip install)
If you're on mac, linux, or windows, just download the wheel that matches your python version and platform and install it with pip.
You can find the wheels here: link. The repo's README.md also has instructions.
Option 2: Build From Source
If you want to change the code, add features (fonts!), or see how everything works, you can build it yourself.
Clone the repo (including the Adafruit GFX submodule), install SDL2, and run:
After installation, run the example:
If a window opens and displays graphics, you're ready to start prototyping.
Demo
I needed a demo, so I started writing in python using pydafruit gfx. That code is provided below. In about an hour, I had something I liked.
Next, I copied the .py file into chatGPT and said, "I want this as an Arduino .ino file for an OLED display."
I uploaded the generated code, ran it, and it worked.
I can now iterate quickly in python, get the layout and graphics right, and then convert it into Arduino code once I like it.