Skip to main content

Display (LCD)

Introduction

The LCD module is used to manage and control the ST7789 display. It provides rich drawing functions such as drawing text, shapes and images, as well as display control functions such as adjusting the backlight and rotating the screen.

info

The API interfaces in this document have not been fully tested yet. If you run into any strange problems, feel free to contact me and I will fix them as soon as possible!

This module is based on the MicroPython ST7789 module. For any application examples, you can search for "micropython st7789" online and you will basically find relevant references (or just ask GPT).

Usage Example

The following example shows how to use the LCD class to create an instance and perform some basic operations:

from lcd import LCD
import time
import vga2_8x16 as font

# Create an LCD instance
display = LCD(backlight=200)

# Clear the screen to black
display.fill(LCD.BLACK)

# Display text
display.text(font, "Hello World", 0, 0, LCD.WHITE, LCD.BLACK)

# Draw a rectangle
display.draw_rect(10, 10, 100, 50, LCD.RED)

# Draw a circle
display.draw_circle(60, 60, 30, LCD.BLUE)

# Main loop
while True:
time.sleep(1) # keep the main loop running

Note:

  • vga2_8x16 is a bitmap font library for strings.

LCD Class

Features

The LCD class provides a comprehensive interface for controlling the ST7789-driven TFT display, supporting graphics drawing, text rendering, image display and backlight control.

Constructor

LCD(rotation, buffer_size, options, backlight)

  • Function: Create and initialize an LCD instance.
  • Arguments:
ArgumentTypeDefaultDescription
rotationint0Screen rotation angle (0, 90, 180, 270).
buffer_sizeint0Display buffer size.
optionsint0Display options (configuration options at initialization).
backlightint200Backlight brightness (0-255).
  • Returns: an LCD instance.
  • Example:
from lcd import LCD

# Initialize the LCD screen, set rotation angle and backlight brightness
display = LCD(rotation=90, backlight=255)

# Clear the screen using the fill method
display.fill(LCD.RED)

Methods

To make the methods in the LCD class easier to understand and use, we group them by function. The following table lists each method's category and a brief description:

CategoryMethodDescription
Power ControlonTurn on the display backlight.
offTurn off the display backlight.
sleepPut the display into sleep mode.
wakeWake the display from sleep mode.
Display ControlfillFill the entire screen with a specified color.
set_rotationSet the screen rotation angle (0, 90, 180 or 270 degrees).
widthReturn the display width.
heightReturn the display height.
rotationGet or set the current rotation angle of the display.
offsetSet the offset for display window rendering.
vscrdefSet vertical scroll definition parameters.
vscsadSet the vertical scroll start address.
Drawingdraw_pixelDraw a single pixel at a specified coordinate using a given color.
draw_lineDraw a straight line between two points (x0, y0) and (x1, y1) using a specified color.
draw_rectDraw a rectangle outline with the top-left corner at (x, y) and a specified width, height and color.
fill_rectFill a rectangle with the top-left corner at (x, y) and a specified width, height and color.
draw_circleDraw a circle centered at (x, y) with radius r using a specified color.
fill_circleFill a circle centered at (x, y) with radius r using a specified color.
hlineDraw a horizontal line starting at (x, y) with a specified length and color.
vlineDraw a vertical line starting at (x, y) with a specified length and color.
Image ProcessingblitCopy a buffer to the display with a specified coordinate, width and height.
bitmapDraw a bitmap at a specified coordinate, optionally using an index for color mapping.
jpgDraw a JPEG image at a specified coordinate using a fast or slow rendering method.
jpg_decodeDecode a JPEG file and optionally resize it to fit a specified size.
pngDraw a PNG image at a specified coordinate, optionally using a mask for transparency.
Text ProcessingtextDisplay text at a specified coordinate using a font, color and background color.
writeDraw text at a specified coordinate using a bitmap font, with foreground and background colors.
write_lenReturn the width of a string rendered with a bitmap font.
drawDraw text at a specified coordinate using a vector font, with a given scale.
draw_lenReturn the width of a string rendered with a vector font, considering the scale.
Polygonpolygon_centerCalculate the center point of a given polygon.
fill_polygonDraw and fill a polygon at a specified coordinate, with a given color, angle and center offset.
polygonDraw a polygon outline at a specified coordinate, with a given color, angle and center offset.
Color Processingcolor565Convert RGB color values to 16-bit RGB565 format.
map_bitarray_to_rgb565Map a bitarray to an RGB565 color buffer using specified foreground and background colors.
Boundary ControlboundingSet or get the bounding box of the drawing area, optionally returning it as a rectangle.

text(font, text, x, y, color, background)

  • Function: Display text on the screen.
  • Arguments:
ArgumentTypeDescription
fontFontText font (e.g. Monospace, Sans)
textstrThe text content to display
xintStarting X coordinate of the text
yintStarting Y coordinate of the text
colorintText color (RGB565 format)
backgroundintBackground color (RGB565 format)
  • Returns: None
  • Example:
from lcd import LCD
import vga2_8x16 as font

# Initialize the LCD
display = LCD()

# Display text on the screen
display.text(font, "Hello, World!", 10, 20, LCD.WHITE, LCD.BLACK)

fill(color)

  • Function: Fill the screen with a color.
  • Arguments:
ArgumentTypeDescription
colorintFill color (RGB565 format)
  • Returns: None
  • Example:
display.fill(LCD.BLUE)

on()

  • Function: Turn on the backlight.
  • Arguments: none
  • Returns: None
  • Example:
display.on()

off()

  • Function: Turn off the backlight.
  • Arguments: none
  • Returns: None
  • Example:
display.off()

sleep()

  • Function: Enter sleep mode.
  • Arguments: none
  • Returns: None
  • Example:
display.sleep()

wake()

  • Function: Exit sleep mode.
  • Arguments: none
  • Returns: None
  • Example:
display.wake()

set_rotation(rotation)

  • Function: Set the screen rotation.
  • Arguments:
ArgumentTypeDescription
rotationintRotation angle (0, 90, 180, 270)
  • Returns: None
  • Example:
display.set_rotation(90)

draw_pixel(x, y, color)

  • Function: Draw a single pixel.
  • Arguments:
ArgumentTypeDescription
xintX coordinate of the pixel
yintY coordinate of the pixel
colorintPixel color (RGB565 format)
  • Returns: None
  • Example:
display.draw_pixel(15, 10, LCD.RED)

draw_line(x0, y0, x1, y1, color)

  • Function: Draw a line.
  • Arguments:
ArgumentTypeDescription
x0intStarting X coordinate
y0intStarting Y coordinate
x1intEnding X coordinate
y1intEnding Y coordinate
colorintLine color (RGB565 format)
  • Returns: None
  • Example:
display.draw_line(10, 20, 100, 200, LCD.GREEN)

draw_rect(x, y, width, height, color)

  • Function: Draw a rectangle.
  • Arguments:
ArgumentTypeDescription
xintStarting X coordinate of the rectangle
yintStarting Y coordinate of the rectangle
widthintRectangle width
heightintRectangle height
colorintRectangle outline color (RGB565 format)
  • Returns: None
  • Example:
display.draw_rect(10, 20, 50, 30, LCD.YELLOW)

fill_rect(x, y, width, height, color)

  • Function: Fill a rectangle.
  • Arguments:
ArgumentTypeDescription
xintStarting X coordinate of the rectangle
yintStarting Y coordinate of the rectangle
widthintRectangle width
heightintRectangle height
colorintFill color (RGB565 format)
  • Returns: None
  • Example:
display.fill_rect(10, 20, 50, 30, LCD.RED)

draw_circle(x, y, r, color)

  • Function: Draw a circle.
  • Arguments:
ArgumentTypeDescription
xintX coordinate of the circle center
yintY coordinate of the circle center
rintCircle radius
colorintCircle color (RGB565 format)
  • Returns: None
  • Example:
display.draw_circle(50, 50, 20, LCD.CYAN)

fill_circle(x, y, r, color)

  • Function: Fill a circle.
  • Arguments:
ArgumentTypeDescription
xintX coordinate of the circle center
yintY coordinate of the circle center
rintCircle radius
colorintFill color (RGB565 format)
  • Returns: None
  • Example:
display.fill_circle(50, 50, 20, LCD.MAGENTA)

blit(buffer, x, y, width, height)

  • Function: Copy buffer contents to the screen.
  • Arguments:
ArgumentTypeDescription
bufferbytesImage data buffer
xintStarting X coordinate
yintStarting Y coordinate
widthintImage width
heightintImage height
  • Returns: None
  • Example:
display.blit(image_data, 0, 0, 135, 240)

hline(x, y, length, color)

  • Function: Draw a horizontal line.
  • Arguments:
ArgumentTypeDescription
xintStarting X coordinate
yintY coordinate
lengthintLine length
colorintLine color (RGB565 format)
  • Returns: None
  • Example:
display.hline(10, 20, 100, LCD.YELLOW)

vline(x, y, length, color)

  • Function: Draw a vertical line.
  • Arguments:
ArgumentTypeDescription
xintX coordinate
yintStarting Y coordinate
lengthintLine length
colorintLine color (RGB565 format)
  • Returns: None
  • Example:
display.vline(20, 10, 100, LCD.GREEN)

bitmap(bitmap, x, y, index=None)

  • Function: Draw a bitmap.
  • Arguments:
ArgumentTypeDescription
bitmapbytesBitmap data
xintStarting X coordinate
yintStarting Y coordinate
indexintBitmap index (optional)
  • Returns: None
  • Example:
display.bitmap(bitmap_data, 0, 0)

write(bitmap_font, s, x, y, fg=WHITE, bg=BLACK, background_tuple=None, fill_flag=False)

  • Function: Draw text using a bitmap font.
  • Arguments:
ArgumentTypeDescription
bitmap_fontFontBitmap font
sstrThe text content to display
xintStarting X coordinate of the text
yintStarting Y coordinate of the text
fgintForeground color (RGB565 format)
bgintBackground color (RGB565 format)
background_tupletupleBackground parameters (optional)
fill_flagboolFill flag (optional)
  • Returns: None
  • Example:
display.write(bitmap_font, "Hello", 10, 20, LCD.WHITE, LCD.BLACK)

write_len(bitmap_font, s)

  • Function: Get the width of the text.
  • Arguments:
ArgumentTypeDescription
bitmap_fontFontBitmap font
sstrThe text content to display
  • Returns: int - text width
  • Example:
width = display.write_len(bitmap_font, "Hello")

draw(vector_font, s, x, y, fg=WHITE, scale=1.0)

  • Function: Draw text using a vector font.
  • Arguments:
ArgumentTypeDescription
vector_fontFontVector font
sstrThe text content to display
xintStarting X coordinate of the text
yintStarting Y coordinate of the text
fgintForeground color (RGB565 format)
scalefloatFont scale
  • Returns: None
  • Example:
display.draw(vector_font, "Hello", 10, 20, LCD.WHITE, scale=1.5)

draw_len(vector_font, s, scale=1.0)

  • Function: Get the width of vector font text.
  • Arguments:
ArgumentTypeDescription
vector_fontFontVector font
sstrThe text content to display
scalefloatFont scale
  • Returns: int - text width
  • Example:
width = display.draw_len(vector_font, "Hello", scale=1.5)

jpg(jpg, x, y, method='FAST')

  • Function: Draw a JPEG image.
  • Arguments:
ArgumentTypeDescription
jpgbytesJPEG image data
xintStarting X coordinate
yintStarting Y coordinate
methodstrDecode method ('FAST' or 'SLOW')
  • Returns: None
  • Example:
display.jpg(jpeg_data, 0, 0)

jpg_decode(jpg_filename, x=0, y=0, width=None, height=None)

  • Function: Decode a JPEG file.
  • Arguments:
ArgumentTypeDescription
jpg_filenamestrJPEG file name
xintStarting X coordinate
yintStarting Y coordinate
widthintImage width (optional)
heightintImage height (optional)
  • Returns: None
  • Example:
display.jpg_decode("image.jpg")

png(png_filename, x, y, mask=False)

  • Function: Draw a PNG image.
  • Arguments:
ArgumentTypeDescription
png_filenamestrPNG file name
xintStarting X coordinate
yintStarting Y coordinate
maskboolUse a mask (optional)
  • Returns: None
  • Example:
display.png("image.png", 0, 0)

polygon_center(polygon)

  • Function: Calculate the center point of a polygon.
  • Arguments:
ArgumentTypeDescription
polygonlistList of polygon vertices
  • Returns: tuple - (x, y) center coordinate
  • Example:
center = display.polygon_center([(10, 10), (20, 10), (20, 20), (10, 20)])

fill_polygon(polygon, x, y, color, angle=0, center_x=0, center_y=0)

  • Function: Draw a filled polygon.
  • Arguments:
ArgumentTypeDescription
polygonlistList of polygon vertices
xintStarting X coordinate
yintStarting Y coordinate
colorintFill color (RGB565 format)
angleintRotation angle (optional)
center_xintCenter X coordinate (optional)
center_yintCenter Y coordinate (optional)
  • Returns: None
  • Example:
display.fill_polygon([(10, 10), (20, 10), (20, 20), (10, 20)], 0, 0, LCD.GREEN)

polygon(polygon, x, y, color, angle=0, center_x=0, center_y=0)

  • Function: Draw a polygon.
  • Arguments:
ArgumentTypeDescription
polygonlistList of polygon vertices
xintStarting X coordinate
yintStarting Y coordinate
colorintOutline color (RGB565 format)
angleintRotation angle (optional)
center_xintCenter X coordinate (optional)
center_yintCenter Y coordinate (optional)
  • Returns: None
  • Example:
display.polygon([(10, 10), (20, 10), (20, 20), (10, 20)], 0, 0, LCD.RED)

bounding(status=None, as_rect=False)

  • Function: Set or get the drawing area boundary.
  • Arguments:
ArgumentTypeDescription
statusboolSet the boundary (optional)
as_rectboolWhether to return the boundary as a rectangle (optional)
  • Returns: tuple - (tfa, height, bfa) boundary parameters (if getting)
  • Example:
boundaries = display.bounding()

vscrdef(tfa, height, bfa)

  • Function: Set vertical scroll parameters.
  • Arguments:
ArgumentTypeDescription
tfaintTop boundary
heightintTotal height
bfaintBottom boundary
  • Returns: None
  • Example:
display.vscrdef(0, 240, 0)

vscsad(vssa)

  • Function: Set the vertical scroll start address.
  • Arguments:
ArgumentTypeDescription
vssaintVertical scroll start address
  • Returns: None
  • Example:
display.vscsad(0)

color565(r, g, b)

  • Function: Convert RGB color to a 16-bit color.
  • Arguments:
ArgumentTypeDescription
rintRed component
gintGreen component
bintBlue component
  • Returns: int - 16-bit color value
  • Example:
color = display.color565(255, 0, 0) # red

map_bitarray_to_rgb565(bitarray, buffer, width, color=WHITE, bg_color=BLACK)

  • Function: Convert a bitarray to an RGB565 color buffer.
  • Arguments:
ArgumentTypeDescription
bitarraybytesBitarray
bufferbytesColor buffer
widthintImage width
colorintColor (RGB565 format)
bg_colorintBackground color (RGB565 format)
  • Returns: None
  • Example:
display.map_bitarray_to_rgb565(bitarray_data, buffer, 135)

width()

  • Function: Get the display width.
  • Arguments: none
  • Returns: int - display width
  • Example:
screen_width = display.width()

height()

  • Function: Get the display height.
  • Arguments: none
  • Returns: int - display height
  • Example:
screen_height = display.height()

rotation(r)

  • Function: Set the display rotation.
  • Arguments:
ArgumentTypeDescription
rintRotation angle (0, 90, 180, 270)
  • Returns: None
  • Example:
display.rotation(90)

offset(x_start, y_start)

  • Function: Set the display offset.
  • Arguments:
ArgumentTypeDescription
x_startintX coordinate offset
y_startintY coordinate offset
  • Returns: None
  • Example:
display.offset(10, 20)