displayio.OnDiskBitmap on Pico 2 W/CircuitPython 9

Last post Raspberry Pi Pico 2/CircuitPython 9 exercise to display bmp on ST7789 LCD using adafruit_imageload/adafruit_slideshow, here is another method on CircuitPython to display bmp, using displayio.OnDiskBitmap, tested on my new Raspberry Pi Pico 2 W running CircuitPython 9.2.1.


Connection, same as in last post.

To prepare 240x240 bmp, read Resize jpg and convert to bmp in RGB888 and RGB565 mode, using Python/GIMP

Exercise Code:

cpy_pico2w_OnDiskBitmap.py, display bmp using OnDiskBitmap()
"""
Raspberry Pi Pico 2/CircuitPython 9
to display on 1.54" 240x240 ST7789 SPI IPS

Display bmp using OnDiskBitmap()

Connection:
-----------
GND   GND
VCC   3V3
SCL   GP18
SDA   GP19
RES   GP20
DC    GP21
CS    GP17
BLK   GP22

CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy

ref:
displayio.OnDiskBitmap() -
https://docs.circuitpython.org/en/latest/shared-bindings/displayio/index.html#displayio.OnDiskBitmap
"""

import os, sys
import board
import busio
import displayio
import adafruit_st7789

disp_sck  = board.GP18
disp_mosi = board.GP19
disp_res  = board.GP20
disp_dc   = board.GP21
disp_cs   = board.GP17
disp_blk  = board.GP22

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

disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)

display_bus = displayio.FourWire(spi_bus=disp_spi,
                                 command=disp_dc,
                                 chip_select=disp_cs,
                                 reset=disp_res)

display = adafruit_st7789.ST7789(display_bus,
                                 width=240,
                                 height=240,
                                 rowstart=80,
                                 rotation=180,
                                 backlight_pin=disp_blk)

sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
                "\nrun on " + os.uname()[4] +\
                "\n" + adafruit_st7789.__name__ + " " + adafruit_st7789.__version__
print("====================================")
print(sys_info_text)
print("====================================")
print()

#images bmp stored in CIRCUITPY's /images/ folder
images_folder = "/images/"
#image_file = "img_01_pythonConverted_240x240_rgb888.bmp"
image_file = "img_02_pythonConverted_240x240_rgb565.bmp"

bitmap = displayio.OnDiskBitmap(images_folder+image_file)
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)

group = displayio.Group()
group.append(tile_grid)
display.root_group = group

while True:
    pass


cpy_pico2w_OnDiskBitmap_2.py, in SlideShow form.
"""
Raspberry Pi Pico 2 W/CircuitPython 9
to display bmp on 1.54" 240x240 ST7789 SPI IPS

Display bmp using OnDiskBitmap(), in SlideShow.

Connection:
-----------
GND   GND
VCC   3V3
SCL   GP18
SDA   GP19
RES   GP20
DC    GP21
CS    GP17
BLK   GP22

CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy

ref:
displayio.OnDiskBitmap() -
https://docs.circuitpython.org/en/latest/shared-bindings/displayio/index.html#displayio.OnDiskBitmap
"""

import os, sys
import board
import busio
import displayio
import adafruit_st7789

import time

disp_sck  = board.GP18
disp_mosi = board.GP19
disp_res  = board.GP20
disp_dc   = board.GP21
disp_cs   = board.GP17
disp_blk  = board.GP22

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

disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)

display_bus = displayio.FourWire(spi_bus=disp_spi,
                                 command=disp_dc,
                                 chip_select=disp_cs,
                                 reset=disp_res)

display = adafruit_st7789.ST7789(display_bus,
                                 width=240,
                                 height=240,
                                 rowstart=80,
                                 rotation=180,
                                 backlight_pin=disp_blk)

sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
                "\nrun on " + os.uname()[4] +\
                "\n" + adafruit_st7789.__name__ + " " + adafruit_st7789.__version__
print("====================================")
print(sys_info_text)
print("====================================")
print()

#images bmp stored in CIRCUITPY's /images/ folder
images_folder = "/images/"
images_file = ["img_01_pythonConverted_240x240_rgb888.bmp",
               "img_02_pythonConverted_240x240_rgb565.bmp",
               "img_03_pythonConverted_240x240_rgb888.bmp",
               "img_04_pythonConverted_240x240_rgb565.bmp",
               "img_05_pythonConverted_240x240_rgb888.bmp",
               "img_06_pythonConverted_240x240_rgb565.bmp",
               "img_07_pythonConverted_240x240_rgb888.bmp",
               "img_08_pythonConverted_240x240_rgb565.bmp",
               "img_09_pythonConverted_240x240_rgb888.bmp",
               "img_10_pythonConverted_240x240_rgb565.bmp"]

# 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)

group = displayio.Group()
group.append(bmpTileGrid)
display.root_group = group

def fade_in():
    for i in range(101):
        display.brightness = 0.01 * i
        time.sleep(0.01)
    
def fade_out():
    for i in range(100, -1, -1):
        display.brightness = 0.01 * i
        time.sleep(0.01)

fade_out()
def update_bmpTileGrid(path_to_bmp):
    odb = displayio.OnDiskBitmap(path_to_bmp)
    bmpTileGrid.bitmap = odb
    bmpTileGrid.pixel_shader = odb.pixel_shader
    display.refresh()
    fade_in()
    time.sleep(3)
    fade_out()

while True:
    for f in images_file:
        update_bmpTileGrid(images_folder+f)

while True:
    pass


cpy_pico2w_OnDiskBitmap_3.py, compare OnDiskBitmap and adafruit_imageload.
"""
Raspberry Pi Pico 2 W/CircuitPython 9
to display bmp on 1.54" 240x240 ST7789 SPI IPS

Compare OnDiskBitmap and adafruit_imageload

Connection:
-----------
GND   GND
VCC   3V3
SCL   GP18
SDA   GP19
RES   GP20
DC    GP21
CS    GP17
BLK   GP22

CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy
- adafruit_imageload folder

ref:
displayio.OnDiskBitmap() -
https://docs.circuitpython.org/en/latest/shared-bindings/displayio/index.html#displayio.OnDiskBitmap
adafruit_imageload
https://docs.circuitpython.org/projects/imageload/
"""

import os, sys
import board
import busio
import displayio
import adafruit_st7789
import adafruit_imageload

import time
import gc

disp_sck  = board.GP18
disp_mosi = board.GP19
disp_res  = board.GP20
disp_dc   = board.GP21
disp_cs   = board.GP17
disp_blk  = board.GP22

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

disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)

display_bus = displayio.FourWire(spi_bus=disp_spi,
                                 command=disp_dc,
                                 chip_select=disp_cs,
                                 reset=disp_res)

display = adafruit_st7789.ST7789(display_bus,
                                 width=240,
                                 height=240,
                                 rowstart=80,
                                 rotation=180,
                                 backlight_pin=disp_blk)

sys_info_text = sys.implementation[0] + " " + os.uname()[3] +\
                "\nrun on " + os.uname()[4] +\
                "\n" + adafruit_st7789.__name__ + " " + adafruit_st7789.__version__ +\
                "\n" + adafruit_imageload.__name__ + " " + adafruit_imageload.__version__
print("====================================")
print(sys_info_text)
print("====================================")
print()

#images bmp stored in CIRCUITPY's /images/ folder
images_folder = "/images/"
images_file = ["img_01_pythonConverted_240x240_rgb888.bmp",
               "img_02_pythonConverted_240x240_rgb565.bmp",
               "img_03_pythonConverted_240x240_rgb888.bmp",
               "img_04_pythonConverted_240x240_rgb565.bmp",
               "img_05_pythonConverted_240x240_rgb888.bmp",
               "img_06_pythonConverted_240x240_rgb565.bmp",
               "img_07_pythonConverted_240x240_rgb888.bmp",
               "img_08_pythonConverted_240x240_rgb565.bmp",
               "img_09_pythonConverted_240x240_rgb888.bmp",
               "img_10_pythonConverted_240x240_rgb565.bmp"
               ]

# 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)

group = displayio.Group()
group.append(bmpTileGrid)
display.root_group = group
    
# remark: my Python code converting RGB565
#         not work for adafruit_imageload.
def update_images(path_to_bmp):
    
    # using displayio.OnDiskBitmap()
    image = displayio.OnDiskBitmap(path_to_bmp)
    palette = image.pixel_shader
    """
    # using adafruit_imageload
    image, palette = adafruit_imageload.load(
        path_to_bmp, bitmap=displayio.Bitmap, palette=displayio.Palette)
    """
    bmpTileGrid.bitmap = image
    bmpTileGrid.pixel_shader = palette
    display.refresh()

counter = 1
while True:
    print("======== Loop Start " + str(counter) + " ========")
    print(gc.mem_free())
    gc.collect()
    print(gc.mem_free())
    
    tick_ms_start = time.monotonic()
    for f in images_file:
        update_images(images_folder+f)
    tick_ms_end = time.monotonic()
    
    print("--- Loop End ---")
    print(gc.mem_free())
    gc.collect()
    print(gc.mem_free())
    
    print("Time for one loop:",
          tick_ms_end-tick_ms_start,
          "s")
    
    counter = counter+1
    
while True:
    pass

next:
Pico 2 W/CircuitPython download bmp via WiFi, and display using OnDiskBitmap.


Comments

Popular posts from this blog

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

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator