display Module
import display
tft = display.init()
tft.fill(display.BLACK)
display.write("Hello, CoCube", 20, 40, display.YELLOW, display.BLACK)
What is this?
display wraps the 240 x 240 ST7789 screen built into CoCube.
The usual pattern is to call init() first, then draw through the returned tft object. For text, display.write() is the recommended entry point.
Initialization
tft = display.init()
Common defaults:
rotation=3inversion=Truecolor_order=st7789.RGBinit()is a singleton initializer; once the first display object is created, later calls return the sametft
Screen size constants:
display.WIDTH
display.HEIGHT
If you want to change rotation, inversion, or color_order, it is best to restart the interpreter and call init() again.
Colors
display.BLACK
display.WHITE
display.RED
display.GREEN
display.BLUE
display.CYAN
display.MAGENTA
display.YELLOW
display.color565(r, g, b)
Basic drawing
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)
Polygons
tft.polygon(pts, x, y, color, angle, cx, cy)
tft.fill_polygon(pts, x, y, color, angle, cx, cy)
Text and measuring
The current CoCube firmware already includes a default Chinese font, so display.write() can show Chinese, English, and digits directly without uploading a separate font file.
display.write(text, x, y, color=display.WHITE, bg=display.BLACK, size="normal", auto_wrap=False, line_spacing=None)
display.measure(text, size="normal")
Common parameters:
text: the string to displayx/y: the starting coordinatescolor: text colorbg: background colorsize:small,normal, orbigauto_wrap: whether to wrap automaticallyline_spacing: custom line spacing
Size mapping:
size | Character size | Default line spacing |
|---|---|---|
small | 16 x 16 | 2 |
normal | 24 x 24 | 4 |
big | 32 x 32 | 6 |
display.measure() returns the text width and is especially useful for centered layout.
Common pattern:
import display
tft = display.init()
tft.fill(display.BLACK)
title = "Hello, CoCube"
x = (display.WIDTH - display.measure(title)) // 2
display.write(title, x, 40, display.YELLOW, display.BLACK)
Images and buffers
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)
Screen control
tft.on()
tft.off()
tft.rotation(r)
tft.inversion_mode(True)
tft.sleep_mode(True)
tft.width()
tft.height()
Minimum example
import display
tft = display.init()
tft.fill(display.BLACK)
display.write("Hello, CoCube", 20, 40, display.YELLOW, display.BLACK)
tft.fill_circle(120, 120, 20, display.CYAN)