Skip to main content

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=3
  • inversion=True
  • color_order=st7789.RGB
  • init() is a singleton initializer; once the first display object is created, later calls return the same tft

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 display
  • x / y: the starting coordinates
  • color: text color
  • bg: background color
  • size: small, normal, or big
  • auto_wrap: whether to wrap automatically
  • line_spacing: custom line spacing

Size mapping:

sizeCharacter sizeDefault line spacing
small16 x 162
normal24 x 244
big32 x 326

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)