如何在ST7789屏幕(树莓派Pico)上显示bytearray数组?
ST7789屏幕显示bytearray数组的实现方法(树莓派Pico)
针对树莓派Pico上的ST7789屏幕,根据你使用的库类型,提供两种常见的bytearray显示方案:
方案一:基于framebuf的ST7789库(主流Micropython库)
如果你的ST7789库集成了framebuf支持,可通过帧缓冲转换bytearray后显示:
1. 单色像素bytearray(如1位深度图像)
假设bytearray中每个字节存储8个水平排列的像素,代码示例:
import framebuf from machine import SPI, Pin import st7789 # 初始化ST7789屏幕 spi = SPI(0, baudrate=40000000, sck=Pin(2), mosi=Pin(3)) display = st7789.ST7789(spi, 240, 240, reset=Pin(0), dc=Pin(1), cs=Pin(5)) display.init() # 替换为你的实际bytearray数据(示例为100x100单色图像) width, height = 100, 100 img_bytearray = bytearray((width * height + 7) // 8) # 创建帧缓冲,匹配单色像素存储格式 fb = framebuf.FrameBuffer(img_bytearray, width, height, framebuf.MONO_HLSB) # 将帧缓冲显示到屏幕指定位置(x=20, y=20) display.blit(fb, 20, 20)
2. 彩色RGB565格式bytearray
ST7789默认支持RGB565(每个像素占2字节),直接转换为帧缓冲显示:
# 初始化屏幕同上 width, height = 100, 100 # 替换为你的RGB565格式bytearray(总字节数=width*height*2) img_bytearray = bytearray(width * height * 2) # 创建RGB565格式帧缓冲 fb = framebuf.FrameBuffer(img_bytearray, width, height, framebuf.RGB565) # 全屏显示帧缓冲内容 display.blit(fb, 0, 0)
方案二:无framebuf集成的轻量ST7789库
如果你的库未集成framebuf,可直接操作屏幕的显存写入函数:
from machine import SPI, Pin import st7789 # 初始化屏幕同上 width, height = 100, 100 img_bytearray = bytearray(width * height * 2) # RGB565格式数据 # 设置屏幕显示区域(左上角(0,0)到右下角(width-1, height-1)) display.set_window(0, 0, width-1, height-1) # 直接写入bytearray数据到屏幕显存 display.write_data(img_bytearray)
关键注意事项
- 确认bytearray的像素格式(单色/彩色、字节顺序)与屏幕配置匹配,ST7789常用RGB565格式。
- 单色bytearray需注意帧缓冲的存储模式(
MONO_HLSB/MONO_VLSB),需和你的数据排列一致。 - 部分库的
blit函数支持颜色反转参数,可根据需求添加(如display.blit(fb, x, y, invert=True))。
内容的提问来源于stack exchange,提问作者Efekan
相关产品推荐
相关产品推荐

