E-paper: a picture that stays
An e-paper display works like a printed page: tiny black and white particles are pushed into place by a voltage and then stay there. The picture stays without power, even with the Pico unplugged. The price is speed: a full refresh takes about two seconds, and the display flashes black and white while the particles settle.
The Waveshare Pico-ePaper-2.13 (250 × 122 pixels, black and white) is plugged onto the Pico and talks SPI, a bus of a few wires: the clock CLK (GP10) sets the pace, and on every tick one bit goes out on the data wire DIN (GP11). CS (GP9) chooses the chip: it only listens while CS is low. DC (GP8) says what the bytes are: low for a command, high for the data that belongs to it. RST (GP12) resets the display, and BUSY (GP13) tells the Pico that it is still at work.
The driver is plain Ruby in the tab epd2in13.rb, the letters are in font5x7.rb. require "epd2in13" loads it, as it would load /lib/epd2in13.rb on a real Pico. Run the cell and watch the display on the bench flash:
require "spi"
require "epd2in13"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 4_000_000,
sck_pin: 10, copi_pin: 11)
epd = EPD2in13.new(spi: spi, cs_pin: 9, dc_pin: 8,
rst_pin: 12, busy_pin: 13)
epd.clear
epd.text(10, 10, "Chunky bacon!", scale: 2)
epd.text(10, 40, "Ruby on e-paper")
epd.refresh
Open the tab epd2in13.rb. command puts DC low, sends one byte with @spi.write, then puts DC high and sends the data. init is the sequence from Waveshare's own demo code: a reset, a software reset (0x12), the number of lines, the window of the display's memory. refresh sends the picture (command 0x24 and 4000 bytes, one bit per pixel) and starts the update (0x22, 0x20).
Then busy_wait asks GP13 every 10 ms and waits while it reads 1: on this controller, BUSY high means busy. PicoRuby has a driver of its own for another e-paper chip, the UC8151 of Pimoroni's Badger, and it waits the other way round, because there BUSY low means busy. Two chips, two answers: that is why a driver starts with the datasheet.
Everything you draw goes into a buffer in the Pico's memory first: pixel, line, rect and text only change bits there, and nothing shows until refresh. With fill: true a rectangle is filled. A bar chart from an Array:
require "spi"
require "epd2in13"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 4_000_000,
sck_pin: 10, copi_pin: 11)
epd = EPD2in13.new(spi: spi, cs_pin: 9, dc_pin: 8,
rst_pin: 12, busy_pin: 13)
# slices of bacon, one number per day
eaten = [3, 5, 2, 7, 4]
epd.clear
epd.rect(0, 0, 250, 122)
epd.text(8, 6, "Bacon per day", scale: 2)
eaten.each_with_index do |n, i|
x = 30 + i * 42
# a bar: 11 pixels per slice, standing on the line
h = n * 11
epd.rect(x, 108 - h, 26, h, fill: true)
epd.text(x + 11, 112, n.to_s)
end
epd.line(20, 108, 240, 108)
epd.refresh
Partial refresh
refresh(partial: true) sends the new picture, and the controller compares it with the one before and moves only the pixels that changed: about 0.3 seconds, no flash. The price is ghosting, faint traces of old pixels (the simulated display shows none). Waveshare's advice: after a few partial refreshes do a full one, and in a long-running project don't refresh more often than every three minutes. A counter:
require "spi"
require "epd2in13"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 4_000_000,
sck_pin: 10, copi_pin: 11)
epd = EPD2in13.new(spi: spi, cs_pin: 9, dc_pin: 8,
rst_pin: 12, busy_pin: 13)
epd.clear
epd.text(10, 10, "Counting", scale: 2)
# a full refresh first: the base for the partial ones
epd.refresh
1.upto(5) do |n|
# white over the old digit, then the new one
epd.rect(100, 45, 60, 45, EPD2in13::WHITE, fill: true)
epd.text(118, 50, n.to_s, scale: 5)
# partial: only the changed pixels, no flash
epd.refresh(partial: true)
sleep_ms 1000
end
A camera gives you numbers
A camera is a grid of tiny light sensors, and what comes out of it is one number per pixel: 0 for black up to 255 for white. Camera.new.capture gives you 160 × 120 of them in a String, one byte each, row after row: byte y * 160 + x is the pixel at (x, y). The e-paper only knows black and white, so every number becomes a decision. Run the cell, then choose another picture under the bench and run it again:
Or press Use my webcam under the bench, and the camera takes your own picture. That is optional and happens only after the click: your browser asks first, and the pictures stay in this browser – the page scales them down to 160 × 120 greys and hands them to the simulated Pico, nothing is uploaded. Without a webcam, without permission, or on a page without https, the drawn pictures stay.
require "spi"
require "camera"
require "epd2in13"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 4_000_000,
sck_pin: 10, copi_pin: 11)
epd = EPD2in13.new(spi: spi, cs_pin: 9, dc_pin: 8,
rst_pin: 12, busy_pin: 13)
# 160 x 120 bytes, row after row: 0 = black, 255 = white
grey = Camera.new.capture
epd.clear
120.times do |y|
160.times do |x|
# darker than 128? a black pixel
epd.pixel(45 + x, 1 + y) if grey.getbyte(y * 160 + x) < 128
end
end
epd.refresh
A fixed threshold loses every grey. Dithering keeps it: grey becomes a pattern of black and white dots, more black where it is darker. Try it in the cell: compare with [[64, 192], [240, 128]][y % 2][x % 2] instead of 128.
Honestly: Camera is the simulation's stand-in, PicoRuby has no camera driver. Cameras for the Pico exist: the Arducam HM01B0 delivers grey pixels like these, but it needs the Pico's PIO and DMA to keep up with its clock, and no PicoRuby library does that today. The Arducam Mega sends JPEG files, too big to decode on a Pico in Ruby. So this cell shows what you do with the numbers, not how to get them on real hardware.
The real thing: a Pico 2 W with the R2P2-PICORUBY firmware (picoruby.org) and a Waveshare Pico-ePaper-2.13 (V4), plugged straight onto the Pico's pins. Put the two tabs into /lib as /lib/epd2in13.rb and /lib/font5x7.rb, the cell as /home/app.rb, for example with the upload of the R2P2 web terminal. PicoRuby's own UC8151 driver is not in the default firmware, and it drives another chip anyway. A driver that sets pixel after pixel in Ruby is slow on a microcontroller: take the Pico 2 W, not the older Pico.
text, and refresh.require "spi"
require "epd2in13"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 4_000_000,
sck_pin: 10, copi_pin: 11)
badge = EPD2in13.new(spi: spi, cs_pin: 9, dc_pin: 8,
rst_pin: 12, busy_pin: 13)
badge.clear
# a frame around the whole display, your name inside, then refresh