My loop for a display bug was edit, build, flash, pick up the panel, photograph it and look closely. Each guess took forty seconds. A simulator would have made each one take under a second, and I set it up only after the bugs were fixed.
The panel is a 0.97” e-paper strip, 184×88, driven from an ESP32-C5 over SPI. Its active area is 22.26 × 10.65 mm, about the size of a thumbnail, so checking a frame meant photographing the panel and zooming in.
Getting pixels onto it at all was a separate fight: the reset line belonged to the JTAG block, so the HAL accepted every write and moved nothing. After that, the pixels had to show the right thing.
Trap 1: symmetric test patterns hide orientation bugs
I brought the panel up with the usual geometry: border rectangle, big X, checkerboard, concentric squares. All of them rendered, and I called it working.
Every one of those patterns is vertically symmetric. The panel had been rendering upside down the whole time, and those patterns could not show it. The first frame of real text showed it.
A test pattern that looks the same upside down does not test orientation. One asymmetric glyph (a large F, a filled corner marker) catches this on the first frame and costs nothing.
Trap 2: the controller’s own 180° mode mirrors
The text came out backwards, so I started guessing: the other rotation, then the driver’s rotated init, then back again. Several flash cycles in, I took one photograph and transformed it on the host, which I should have done first:
# One of these reads correctly, and that tells you which class of defect
# you have before touching the firmware again.
Image.open('panel.jpg').rotate(180).save('cand_rot180.jpg')
ImageOps.mirror(Image.open('panel.jpg')).save('cand_mirror.jpg')
The mirrored candidate read correctly. That took ten seconds and ruled out every rotation I had been cycling through: a rotation cannot produce a mirror, so no quarter-turn could fix it.
The mirroring comes from the controller. The SSD1680 has a rotated init sequence, shipped in the vendor sample as EPD_HW_Init_180. It sets data-entry mode 0x02 (X decrement), which moves the RAM byte pointer backwards.
The bytes reverse, but the eight pixels inside each byte stay MSB-first. So the controller mirrors at byte granularity. On a 1-bit framebuffer, no combination of its other flags turns that into a rotation.
The fix was to do orientation in software, inside the DrawTarget:
let (px, py) = match self.orientation {
Orientation::Native => (lx, ly),
// panel(x, y) = logical(y, x). A transpose, not a quarter-turn: the
// panel's own scan is Y-decrement, and a vertical flip composed with a
// quarter turn collapses to this.
Orientation::Transpose => (ly, lx),
Orientation::TransposeFlip => (self.panel_w - 1 - ly, self.panel_h - 1 - lx),
};
The transform is exact and works per pixel. It costs nothing extra, because it folds into pixel writes, so there is no second buffer and no blit pass.
Trap 3: partial refresh has prerequisites
A full e-paper refresh strobes the entire panel black and white. For a status display that updates often that is unusable, so you want partial refresh: about 0.6 s here, with no flash.
Mine worked, then slowly turned grey. The vendor’s partial-update routine does three things before every partial update that I had skipped:
- a hardware reset,
- the partial border waveform (
0x3C = 0x80; full refresh uses0x05), - re-programming the RAM window, which that reset just cleared.
The grey came from the border waveform. Left at the full-refresh value, the border cycles on every partial update and the grey spreads inward. A base map also has to be seeded first. The partial waveform drives pixels relative to the previous-frame RAM bank, so an unseeded bank streaks.
None of this is in the datasheet. It is in the vendor’s sample code. The datasheet for this part ships a reference program copied from a different 122×250 panel and states the BUSY polarity backwards.
embedded-graphics had solved these problems already
Once it worked, I looked at what I had written: a 5×7 bitmap font typed out by hand, about sixty glyphs, plus text_2x and text_3x that scaled it by pixel-doubling.
It rendered, but only the glyphs that happened to appear on screen had ever been checked. Q, &, @ and % stayed untested until the first string that used one. Pixel-doubling a 7 px glyph also gives a blocky 21 px glyph, which is not the same as a 21 px typeface.
embedded-graphics is the standard here, and its crates replace most of what I had written:
| Crate | What it replaced |
|---|---|
embedded-graphics | my Canvas primitives, and the DrawTarget abstraction |
u8g2-fonts | sixty hand-typed glyphs and the pixel-doubling |
embedded-graphics-simulator | photographing a panel |
The typography matters for legibility. u8g2_font_logisoso20_tn is a 20 px face drawn at 20 px. On a 10 mm-tall active area, it makes a numeric readout legible where the scaled font was blocky.
The simulator replaced the photographs
The image at the top of this post is the embedded-graphics simulator’s own demo. Rendering a frame to PNG takes three lines:
let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(184, 88));
render(&mut display, &state)?;
display.to_rgb_output_image(&settings).save_png("frame.png")?;
Besides these lines, the integration is the DrawTarget implementation for my framebuffer, which made the panel driver and the simulator interchangeable, so one render path feeds both. A forty-second build/flash/photograph loop became a sub-second cargo run.
The simulator defaults to an OLED convention where On is a lit white pixel on black; e-paper ink is black on white, so BinaryColorTheme::Inverted is what makes the preview match the panel. Without it, a correct layout looks like a photographic negative, which after the orientation bugs above I would have read as another one. The simulator also runs headless: the SDL feature is on by default for the interactive window, but default-features = false gives you the PNG path, which is what belongs in CI.
Layout tests run on the host
With rendering on the host, layout became testable. These run in cargo test in about ten milliseconds, and two of them found real bugs immediately:
- Nothing overflows the right edge. An eight-character value at 2× is 96 px on an 88 px panel; the draw target clips without an error, so the last character had been missing.
- Long identifiers are truncated before they reach the edge.
- Region bands tile without overlapping, or a partial update of one corrupts its neighbour.
- A region repaint touches only its own band.
- A meter stays in place. Its label is variable-width, so the bar beside it used to shift as the value changed; there is now a fixed column.
The last test was wrong at first. It scanned a row shared with the value text, so it measured glyph widths instead of the bar. It now scans the meter’s bottom edge, where the 5×7 face has no descenders.
The time went into the loop
Every trap above cost about the same amount of time, and none of them was a hard problem. Each was slow to test, because every hypothesis took forty seconds and a photograph to evaluate.
I set up the simulator after all three traps. On the next display I will set it up before the first flash.
Header image: the embedded-graphics simulator demo, from the embedded-graphics repository. Copyright © 2020 James Waples and contributors, used under the project’s MIT license.