Pico 2 W/CircuitPython 10.0.3 Display bmp images in SD, using displayio.OnDiskBitmap()/adafruit_imageload.load().

Raspberry Pi Pico 2 W/CircuitPython 10.0.3 display bmp images in SD, using displayio.OnDiskBitmap()/adafruit_imageload.load().

Reference:
displayio.OnDiskBitmap()
adafruit_imageload.load()


Connection:



                    Raspberry Pi Pico 2 W
                    =====================
                    		GP28|
                    		GND | 
                    		GP27| 
                    		GP26| 
                    		Run |
                    		GP22| ------- LCD_BLK ------------+
                    		GND |                             |
                    		GP21| ------- LCD_DC ---------+   |
                    		GP20| ------- LCD_RES-------+ |   |
    +------- SD_MISO|GP12	GP19| MOSI  - SPI_SDA ----+ | |   |
    | +----- SD_CS  |GP13	GP18| SCK   - SPI_SCL --+ | | |   |
    | |             |GND	GND |		    	| | | |   |
    | | +--- SD_SCK |GP14	GP17| SS    - LCD_CS ---|-|-|-|-+ |
    | | | +- SD_MOSI|GP15	GP16| MISO  - no use    | | | | | |
    | | | |         +---------------+		    	| | | | | |
    | | | |                                             | | | | | |
    | | | |         SD Module                           | | | | | |	GC9A01
    | | | |         =========                           | | | | | |     ======
    | | | | GND --- GND                                 | | | | | +----	BLK
    +-|-|-|-------- MISO                                | | | | +------ CS
      | +-|-------- CLK                                 | | | +-------- DC
      |   +-------- MOSI                                | | +---------- RES
      +------------ CS                                  | +------------ SDA
            3V3 --- 3V3                                 +-------------- SCL
                                                                3V3 --- VCC
                                                                GND --- GND



Exercise code:

cpy_rpPicoW_sd_display_bmp.py
"""
Raspberry Pi Pico 2 W/CircuitPython 10.0.3
Display bmp images in SD,
using displayio.OnDiskBitmap()/adafruit_imageload.load().

details:
https://coxxect.blogspot.com/2026/02/pico-2-wcircuitpython-1003-display-bmp.html

Lib needed:
- adafruit_gc9a01a.mpy
- adafruit_imageload folder
- adafruit_display_text folder

To install libraries using circup:
circup install adafruit_gc9a01a adafruit_imageload adafruit_display_text

"""
import os, sys
import board
import busio
import sdcardio
import storage
import time

import displayio
from fourwire import FourWire
import adafruit_gc9a01a
import adafruit_imageload

import terminalio
from adafruit_display_text import label

displayio.release_displays()

tft_blk = board.GP22
tft_cs  = board.GP17
tft_dc  = board.GP21
tft_res = board.GP20
tft_sda = board.GP19
tft_scl = board.GP18

sd_miso = board.GP12
sd_cs   = board.GP13
sd_clk  = board.GP14
sd_mosi = board.GP15

info = os.uname()[4] + "\n" + \
       sys.implementation[0] + " " + os.uname()[3] + "\n" + \
       adafruit_gc9a01a.__name__ + " " + adafruit_gc9a01a.__version__ + "\n" + \
       displayio.__name__ + "\n" + \
       adafruit_imageload.__name__ + " " + adafruit_imageload.__version__
print("=======================================")
print(info)
print("=======================================")
time.sleep(2)

# Init SD
sd_spi = busio.SPI(clock=sd_clk, MOSI=sd_mosi, MISO=sd_miso)
sd = sdcardio.SDCard(sd_spi, sd_cs)
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')

def list_dir(path, indent=0):
    """Recursive SD card walker (like a mini os.walk())"""
    for name in os.listdir(path):
        full_path = path + "/" + name
        st = os.stat(full_path)
        mode = st[0]

        if mode & 0x4000:  # directory
            print("  " * indent + "[DIR] " + name)
            list_dir(full_path, indent + 1)  # recurse into subfolder
        else:
            print("  " * indent + name)

def list_files_with_ext(path, ext):
    """Return sorted list of files in path with given extension."""
    files = []
    for name in os.listdir(path):
        full_path = path + "/" + name
        st = os.stat(full_path)
        mode = st[0]

        # check regular file (0x8000) only
        if mode & 0x8000:
            if name.lower().endswith("." + ext.lower()):
                files.append(name)

    files.sort()
    return files

# usage
list_dir("/sd")
print()

bmp_folder = "/sd/bmp/"
bmp_files = list_files_with_ext(bmp_folder, "bmp")
print(bmp_files)

# Release any resources currently in use for the displays
displayio.release_displays()

disp_spi = busio.SPI(clock=tft_scl, MOSI=tft_sda)

display_bus = FourWire(spi_bus=disp_spi,
                       command=tft_dc,
                       chip_select=tft_cs,
                       reset=tft_res)
display = adafruit_gc9a01a.GC9A01A(display_bus, width=240, height=240, backlight_pin=tft_blk)

#====================
# Create a dummy bitmap
dummy_bitmap = displayio.Bitmap(240, 240, 1)
dummy_palette = displayio.Palette(1)
dummy_palette[0] = 0x000000
bmpTileGrid = displayio.TileGrid(dummy_bitmap, pixel_shader=dummy_palette)

# Create Label
text_group = displayio.Group(scale=2, x=40, y=200)
text_area = label.Label(terminalio.FONT, text="Info", color=0xFFFFFF, background_color=None)
text_group.append(text_area)  # Subgroup for text scaling

sub_group = displayio.Group() # used to switch bitmap
sub_group.append(bmpTileGrid)

group = displayio.Group()
group.append(sub_group)
group.append(text_group)

display.root_group = group

#Load images using displayio.OnDiskBitmap()/adafruit_imageload.load()
#Update tile_grid
def update_images(bmp_folder, bmp_file):
    text_area.hidden = True
    
    path_to_bmp = bmp_folder+bmp_file

    # Un-comment/comment to select between
    # displayio.OnDiskBitmap()/adafruit_imageload.load()
    
    # using displayio.OnDiskBitmap()
    print("displayio.OnDiskBitmap():", path_to_bmp)
    bitmap = displayio.OnDiskBitmap(path_to_bmp)
    palette = bitmap.pixel_shader
    """
    # using adafruit_imageload
    print("adafruit_imageload.load():", path_to_bmp)
    bitmap, palette = adafruit_imageload.load(
        path_to_bmp, bitmap=displayio.Bitmap, palette=displayio.Palette)
    """
    
    print(bitmap.width, "x", bitmap.height)
    tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)

    # Replace old tilegrid if exists, else append
    if len(sub_group) > 0:
        sub_group[0] = tile_grid
    else:
        sub_group.append(tile_grid)
        
    text_area.text = bmp_file
    text_area.hidden = False

while True:
    for f in bmp_files:
        update_images(bmp_folder, f)
        time.sleep(3)



Comments

Popular posts from this blog

Drive 320x240 ILI9341 SPI TFT using ESP32-S3 (NodeMCU ESP-S3-12K-Kit) using TFT_eSPI library, in Arduino Framework.

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library