Pixels that glow
The e-paper of lesson 99 keeps its picture without power, but a new one takes seconds. An LCD is the other way round: a new picture shows at once, and without power it is gone. Waveshare's Pico-LCD-1.14 has 240 × 135 pixels in colour, an ST7789 controller, and beside the glass a joystick and two keys. It is plugged straight onto the Pico's pins.
It talks SPI on the e-paper's pins – CLK GP10, DIN GP11, CS GP9, DC GP8, RST GP12 – so only one of the two fits on a Pico at a time. GP13 is not BUSY here but the backlight: the glass makes no light of its own, a lamp behind it does, and while GP13 is low the screen stays dark, whatever is on it.
The driver is the tab st7789.rb, the letters are font5x7.rb from lesson 99. Run the cell:
require "spi"
require "st7789"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 10_000_000,
sck_pin: 10, copi_pin: 11)
lcd = ST7789.new(spi: spi)
lcd.fill(ST7789::BLACK)
lcd.rect(0, 0, 240, 135, ST7789::WHITE)
lcd.text("Chunky Bacon", 30, 12, ST7789::YELLOW, scale: 3)
lcd.fill_rect(15, 50, 65, 40, ST7789::RED)
lcd.fill_rect(88, 50, 65, 40, ST7789::GREEN)
lcd.fill_rect(161, 50, 65, 40, ST7789::BLUE)
lcd.text("red green blue", 50, 105, ST7789::WHITE, scale: 2)
Open the tab st7789.rb. init is Waveshare's sequence: a reset, then 0x36 (which way round the picture goes; 0x70 is landscape), 0x3A with 0x05 (16 bits per pixel), a row of voltage and timing settings, 0x21 (colours inverted – this glass shows them the wrong way round otherwise), 0x11 (wake up) and 0x29 (display on).
Unlike the e-paper's driver this one keeps no picture of its own. fill_rect tells the controller a window – the columns with 0x2A, the rows with 0x2B – and after 0x2C it sends the pixels, which fill the window row by row. A whole screen is 240 × 135 × 2 = 64 800 bytes; a copy of it would take an eighth of the Pico 2 W's 520 KB of memory. Sent in one go it would already be too much for a Pico, so fill_rect sends one row after another, 480 bytes each. And the 40 and 53 in window: the controller has memory for 240 × 320 pixels, and the glass shows only part of it. Without them the picture would be shifted and cut off.
A colour is a number
Each pixel is 16 bits: 5 for red, 6 for green, 5 for blue – RGB565. Green gets the extra bit because the eye tells shades of green apart best. ST7789.rgb(255, 128, 0) keeps the top bits of each value from 0 to 255 and packs them into one number; the controller wants its high byte first. Here are the bits of orange, and a grid of colours:
require "spi"
require "st7789"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 10_000_000,
sck_pin: 10, copi_pin: 11)
lcd = ST7789.new(spi: spi)
orange = ST7789.rgb(255, 128, 0)
# 5 bits red, 6 green, 5 blue
puts orange.to_s(2)
# 24 columns of 10 pixels, 9 rows of 15
9.times do |row|
24.times do |col|
color = ST7789.rgb(col * 11, row * 31, 255 - col * 11)
lcd.fill_rect(col * 10, row * 15, 10, 15, color)
end
end
Keys and a joystick
Left of the glass sits a joystick with five moves: up (GP2), down (GP18), left (GP16), right (GP20) and pressed (GP3). On the right are the keys A (GP15) and B (GP17). Each connects its pin to GND, like the button of lesson 97: they need GPIO::PULL_UP, and pressed reads 0. On the bench you press them on the drawing or with the buttons under it. Run the cell and steer the square; A paints it red, B ends the program:
require "spi"
require "gpio"
require "st7789"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 10_000_000,
sck_pin: 10, copi_pin: 11)
lcd = ST7789.new(spi: spi)
lcd.fill(ST7789::BLACK)
lcd.text("B: stop", 195, 125, ST7789::WHITE)
pins = { up: 2, down: 18, left: 16, right: 20, a: 15, b: 17 }
keys = {}
pins.each { |name, pin| keys[name] = GPIO.new(pin, GPIO::IN | GPIO::PULL_UP) }
x = 110
y = 58
lcd.fill_rect(x, y, 20, 20, ST7789::YELLOW)
# about 10 seconds, or until B
500.times do
break if keys[:b].low?
dx = 0
dy = 0
dx = -4 if keys[:left].low?
dx = 4 if keys[:right].low?
dy = -4 if keys[:up].low?
dy = 4 if keys[:down].low?
if dx != 0 || dy != 0 || keys[:a].low?
lcd.fill_rect(x, y, 20, 20, ST7789::BLACK)
x = [[x + dx, 0].max, 220].min
y = [[y + dy, 0].max, 105].min
lcd.fill_rect(x, y, 20, 20, keys[:a].low? ? ST7789::RED : ST7789::YELLOW)
end
sleep_ms 20
end
lcd.text("bye", 105, 60, ST7789::WHITE, scale: 2)
The backlight is a PWM, like the dimmed LED of lesson 98: backlight(percent) sets its duty on GP13. Dimmer saves battery – and at 0 the picture is still in the controller's memory, only nobody sees it:
require "spi"
require "st7789"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 10_000_000,
sck_pin: 10, copi_pin: 11)
lcd = ST7789.new(spi: spi)
lcd.fill(ST7789::BLUE)
lcd.text("Dimmer", 60, 55, ST7789::WHITE, bg: ST7789::BLUE, scale: 4)
[100, 60, 30, 10, 0, 90].each do |percent|
lcd.backlight(percent)
puts "#{percent} %"
sleep_ms 600
end
160 LEDs as a screen
Another way to show colour: Waveshare's Pico-RGB-LED is a board with 160 WS2812 LEDs, 16 across and 10 down – the kind that lights the PicoGo from below in lesson 98, driven by the same community gem, the tab ws2812.rb. They all hang on one data wire (GP6 in Waveshare's demo), LED 0 at the top left, then row by row: the LED in column x and row y is number y * 16 + x. All 160 at full white would draw about 2.5 A, more than a USB port gives – one more reason why the gem starts at 5 % brightness.
require "ws2812"
matrix = WS2812.new(pin: 6, num: 160)
matrix.brightness = 20
10.times do |y|
16.times do |x|
# the hue goes round once from left to right, darker downwards
matrix.set_hsb(y * 16 + x, x * 22, 100, 100 - y * 8)
end
end
matrix.show
The real thing: a Pico 2 W with R2P2 (picoruby.org) and a Waveshare Pico-LCD-1.14, plugged onto the Pico's pins. Put st7789.rb and font5x7.rb into /lib and the cell into /home/app.rb. At 10 MHz a whole screen needs at least 52 ms on the wire, and the Ruby around it takes longer: draw what changes, not the whole screen. The driver follows Waveshare's demo and the ST7789's datasheet, but so far it has only run in this simulation – and which way round the glass sits in the controller's memory is the simulation's choice, made so that Waveshare's 0x70 reads upright. For the Pico-RGB-LED, ws2812.rb goes into /lib too; it needs picoruby-pio, which R2P2's builds for the Pico boards have.
scale: 3 to 5) – and no other digits. The program runs for about two seconds, and the exercise presses A three times. Two traps are waiting: the key needs its pull-up, and a press lasts longer than one pass of the loop but counts once.require "spi"
require "gpio"
require "st7789"
spi = SPI.new(unit: :RP2040_SPI1, frequency: 10_000_000,
sck_pin: 10, copi_pin: 11)
lcd = ST7789.new(spi: spi)
lcd.fill(ST7789::BLACK)
key_a = GPIO.new(15, GPIO::IN)
count = 0
100.times do
# when A has just been pressed: count it, show the count
sleep_ms 20
end