display 模块
import display
tft = display.init()
tft.fill(display.BLACK)
display.write("你好,CoCube", 20, 40, display.YELLOW, display.BLACK)
这是什么?
display 封装了 CoCube 板载的 240 x 240 ST7789 屏幕。
通常先 init(),再通过返回的 tft 对象进行绘图;显示文字时,当前更推荐直接使用 display.write()。
初始化
tft = display.init()
常用默认值:
rotation=3inversion=Truecolor_order=st7789.RGBinit()是单例初始化;第一次创建后,再次调用会直接返回同一个tft对象
屏幕尺寸常量:
display.WIDTH
display.HEIGHT
如果你想改 rotation、inversion 或 color_order,最好在解释器重启后重新 init()。
颜色
display.BLACK
display.WHITE
display.RED
display.GREEN
display.BLUE
display.CYAN
display.MAGENTA
display.YELLOW
display.color565(r, g, b)
基础绘图
tft.fill(color)
tft.fill_rect(x, y, w, h, color)
tft.pixel(x, y, color)
tft.line(x0, y0, x1, y1, color)
tft.hline(x, y, length, color)
tft.vline(x, y, length, color)
tft.rect(x, y, w, h, color)
tft.circle(x, y, r, color)
tft.fill_circle(x, y, r, color)
多边形
tft.polygon(pts, x, y, color, angle, cx, cy)
tft.fill_polygon(pts, x, y, color, angle, cx, cy)
文字与测量
当前 CoCube 固件已经准备好默认中文字库,直接使用 display.write() 就可以显示中文、英文和数字,不需要再单独上传字库文件。
display.write(text, x, y, color=display.WHITE, bg=display.BLACK, size="normal", auto_wrap=False, line_spacing=None)
display.measure(text, size="normal")
常用参数:
text:要显示的字符串x/y:文字起始坐标color:文字颜色bg:背景色size:字号,可选small、normal、bigauto_wrap:是否自动换行line_spacing:自定义行距
字号对应关系:
size | 汉字大小 | 默认行距 |
|---|---|---|
small | 16 x 16 | 2 |
normal | 24 x 24 | 4 |
big | 32 x 32 | 6 |
display.measure() 会返回文字宽度,适合用来做居中排版。
最常见写法:
import display
tft = display.init()
tft.fill(display.BLACK)
title = "你好,CoCube"
x = (display.WIDTH - display.measure(title)) // 2
display.write(title, x, 40, display.YELLOW, display.BLACK)
图片与缓冲区
tft.jpg("/path/to/image.jpg", x, y)
tft.jpg(jpeg_bytes, x, y)
tft.png("/path/to/image.png", x, y)
tft.png("/path/to/alpha.png", x, y, True)
tft.blit_buffer(buf, x, y, w, h)
屏幕控制
tft.on()
tft.off()
tft.rotation(r)
tft.inversion_mode(True)
tft.sleep_mode(True)
tft.width()
tft.height()
最小示例
import display
tft = display.init()
tft.fill(display.BLACK)
display.write("你好,CoCube", 20, 40, display.YELLOW, display.BLACK)
tft.fill_circle(120, 120, 20, display.CYAN)