Repository navigation
Cool Coding Projects — ST7789 Digital Rain
Put your MicroPython skills to the test! This project brings the iconic Matrix falling code effect to your uCompute board equipped with the ST7789 240x240 IPS display.

By leveraging the advanced performance of framebuf2, this script bypasses standard slow rendering to offer a rock-solid, fluid, and high-frequency animation using hardware-accelerated double buffering.
Standard generic ST7789 displays often suffer from an inverted RGB/BGR color matrix due to manufacturing layout discrepancies.
To prevent messy software color conversion tables that slow down framebuf rendering, ot can be permanently patches in the library. By intercepting the MADCTL (36h) register inside the rotation matrix, the driver forces the D3 bit to 1 (BGR encoding):
# Hardware patched rotation matrix inside uCompute's st7789.py
# Forces D3='1' to fix inverted IPS panel color maps seamlessly
ROTATIONS = [0x08, 0x68, 0xc8, 0xa8] This ensures that native MicroPython standard colors like st7789.GREEN map perfectly out of the box without processing overhead.
-
Double Buffering: The entire 240x240 matrix is drawn onto a RAM-allocated
bytearraymatching the 16-bit RGB565 color format (2 bytes per pixel). -
ST7789 Bit-blitting: Once the text layers (dark green trails, bright green mid-sections, and pure white lead characters) are calculated,
display.blit_buffer()pushes the full memory block onto the SPI bus in one single burst. -
SPI Mode 3 Handling: The configuration forces
polarity=1andphase=1to perfectly drive the generic 7-pin IPS screens without requiring a physical CS pin connection.
Create a new file named main.py or run this snippet directly onto your uCompute hardware via Thonny:
# ---------------------------------------------------------
# uCompute Digital Rain for ST7789 IPS Display
# Optimized for framebuf2 and high refresh rates
# ---------------------------------------------------------
from machine import I2C, Pin, SPI, PWM
import st7789 as st7789
import time
import framebuf2
import random
# ---------------------------------------------------------------
# uCompute standard pin definition
_SSD1306_ADDR = 0x3C
_Buzzer = 11 # piezo buzzer (GP11)
_MicroSD_Detect = 7 # MicroSD card detection card (GP7)
_MicroSD_Select = 13 # SDCARD SPI Select (GP13)
_SPI1_SCK = 14 # SPI1_shared Clock
_SPI1_MOSI = 15 # SPI1_shared MOSI
_SPI1_MISO = 12 # SPI1_shared MISO
_Led_System = 25 # System status LED (GP25)
_I2C_SDA = 20 # I2C(0) Data (GP20)
_I2C_SCL = 21 # I2C(0) SCL (GP21)
_Boutons = 26 # Analog resistor ladder buttons (GP26)
_ST7789_SCK = 2 # ST7789 SPI0 Clock (GP2)
_ST7789_MOSI = 3 # ST7789 SPI0 MOSI (GP3)
_ST7789_RESET = 4 # ST7789 Hardware Reset (GP4)
_ST7789_DC = 5 # ST7789 Data/Command Select (GP5)
_ST7789_BL = 6 # ST7789 Backlight PWM/Control (GP6)
# 1. Screen and Hardware SPI Configuration
screen_width = 240
screen_height = 240
screen_rotation = 1
spi = SPI(0, baudrate=20000000, polarity=1, phase=1, bits=8, firstbit=SPI.MSB, sck=Pin(_ST7789_SCK), mosi=Pin(_ST7789_MOSI))
display = st7789.ST7789(
spi,
screen_width,
screen_height,
reset=Pin(_ST7789_RESET, Pin.OUT),
dc=Pin(_ST7789_DC, Pin.OUT),
backlight=Pin(_ST7789_BL, Pin.OUT),
rotation=screen_rotation
)
# 2. RGB565 Matrix Color Palette
GREEN_DARK = st7789.GREEN
GREEN_MID = st7789.GREENYELLOW
WHITE = st7789.WHITE
BLACK = st7789.BLACK
# 3. Double-buffered RAM FrameBuffer initialization
buffer_width = screen_width
buffer_height = screen_height
buffer = bytearray(buffer_width * buffer_height * 2) # 2 bytes per RGB565 pixel
fbuf = framebuf2.FrameBuffer(buffer, buffer_width, buffer_height, framebuf2.RGB565)
# 4. Splash Screen Verification
fbuf.fill(BLACK)
fbuf.large_text('Booting', 60, 100, 2, GREEN_DARK)
display.blit_buffer(buffer, 0, 0, buffer_width, buffer_height)
time.sleep(1)
# 5. Rain Drop Simulation Parameters
FONT_WIDTH = 8
FONT_HEIGHT = 8
COLS = buffer_width // FONT_WIDTH # Spawns exactly 30 parallel streams
CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*#\$@%&+-=<>:?"
# Array tracking the vertical placement and step speed of individual rain tracks
y_positions = [random.randint(-FONT_HEIGHT * 6, 0) for _ in range(COLS)]
speeds = [random.randint(4, 8) for _ in range(COLS)]
# 6. Main Infinite Animation Loop
while True:
fbuf.fill(BLACK) # Clear the virtual workspace
for col in range(COLS):
x_pos = col * FONT_WIDTH
y_pos = y_positions[col]
if y_pos >= 0:
# Step A: Draw the fading tail (Dark Green)
if y_pos - (FONT_HEIGHT * 2) < buffer_height:
char_top = random.choice(CHARS)
fbuf.text(char_top, x_pos, y_pos - (FONT_HEIGHT * 2), GREEN_DARK)
# Step B: Draw the core body (Mid Green-Yellow)
if y_pos - FONT_HEIGHT < buffer_height:
char_mid = random.choice(CHARS)
fbuf.text(char_mid, x_pos, y_pos - FONT_HEIGHT, GREEN_MID)
# Step C: Draw the lightning lead head (Pure White)
if y_pos < buffer_height:
char_lead = random.choice(CHARS)
fbuf.text(char_lead, x_pos, y_pos, WHITE)
# Shift the drop coordinate downwards
y_positions[col] += speeds[col]
# Stream recycle routine once it leaves the viewport bounds
if y_positions[col] >= screen_height + (FONT_HEIGHT * 2):
y_positions[col] = random.randint(-FONT_HEIGHT * 6, 0)
speeds[col] = random.randint(4, 8)
# Atomic screen update over SPI bus
display.blit_buffer(buffer, 0, 0, buffer_width, buffer_height)
# Tiny cycle cooldown to normalize framerate stability
time.sleep(0.01)If your specific 240x240 panel shows scrambled pixels or completely mismatched colors (like red or blue trails instead of green), this is due to an internal controller endianness mismatch. You can fix it instantly by adapting your color bytes inside the script using st7789.color565(r, g, b).