display - Display

The display module let’s you draw on the card10’s display. Pixels are addressed from top left to bottom right with a range of x: 0 to 159 and y: 0 to 79.

0,0
   +---------------------+
   |                     |
   |                     |
   |                     |
   |                     |
   +---------------------+
                        159,79

Drawing operations are clipped, pixels outside of the screen will be ignored.

class display.Display[source]

The display class provides methods to allow the lcd display in card10 to be used in a safe way. All draw methods return the display object so that it is possible to chain calls. It is recommended to use a context manager as following:

import display
with display.open() as disp:
    disp.clear().update()
classmethod open()[source]

Opens the display. Will fail the display can’t be locked

static close()[source]

Closes and unlocks the display. To be able to use it again, it is necessary to open and lock it again with Display.open()

update()[source]

Updates the display based on the changes previously made by various draw functions

clear(col=None)[source]

Clears the display using the color provided, or the default color black

Parameters

col – Clearing color (expects RGB triple)

print(text, *, fg=None, bg=None, posx=0, posy=0, font=3)[source]

Prints a string on the display.

Parameters
  • text – Text to print

  • fg – Foreground color (expects RGB triple)

  • bg – Background color (expects RGB triple) or None for transparent background

  • posx – X-Position of the first character

  • posy – Y-Position of the first character

  • font – 0 <= font <= 4 (currently) selects a font

Avaiable Fonts:

  • display.FONT8

  • display.FONT12

  • display.FONT16

  • display.FONT20

  • display.FONT24

Example:

with display.open() as d:
    d.clear()
    d.print('Hello Earth!', font=display.FONT24)
    d.update()

Changed in version 1.11: Added transparent background printing.

pixel(x, y, *, col=None)[source]

Draws a pixel on the display

Parameters
  • x – X coordinate

  • y – Y coordinate

  • col – color of the pixel (expects RGB tripple)

blit(x, y, w, h, img, rgb565=False, alpha=None)[source]

Draws an image on the display.

Parameters
  • x – X coordinate

  • y – Y coordinate

  • w – Image width

  • h – Image height

  • img – Buffer with pixel data. Default format is RGB with 8 bits per channel.

  • alpha – Alpha mask for img

  • rgb565 – Set to True if the data supplied is in rgb565 format instead of 8 bit RGB.

Note

Alpha mask support is not yet implemented.

New in version 1.17.

Example with RGB data:

import display
import color

# Draw a blue 32x20 pixel rectangle:
# Each pixel is 3 bytes big. Order is red, green, blue
img = bytes(color.BLUE) * 32 * 20
with display.open() as d:
    d.clear()
    d.blit(10, 10, 32, 20, img)
    d.update()

Example with rgb565 data:

import array
import display

# Draw a green 32x20 pixel rectangle:
# 0x07E0 has all bits for green set to 1. Order is RRRR RGGG GGGB BBBB
img = array.array('H', [0x07E0 for x in range(32 * 20)])
with display.open() as d:
    d.clear()
    d.blit(10, 10, 32, 20, img, rgb565=True)
    d.update()

Example with a MicroPython FrameBuffer:

import framebuf
import display

# Create a 160x80 pixel frame buffer and write "Hello World" on the display
f = framebuf.FrameBuffer(bytearray(160 * 80 * 2), 160, 80, framebuf.RGB565)
with display.open() as d:
    f.text("Hello World", 0, 0, 0xF800) # red
    d.blit(0, 0, 160, 80, f, rgb565=True)
    d.update()
backlight(brightness)[source]

Set display backlight brightness

Parameters

brightness – backlight brightness 0 <= brightness <= 100

line(xs, ys, xe, ye, *, col=None, dotted=False, size=1)[source]

Draws a line on the display.

Parameters
  • xs – X start coordinate

  • ys – Y start coordinate

  • xe – X end coordinate

  • ye – Y end coordinate

  • col – color of the line (expects RGB triple)

  • dotted – whether the line should be dotted or not (questionable implementation: draws every other pixel white, draws white squares at higher pixel sizes)

  • size – size of the individual pixels, ranges from 1 to 8

rect(xs, ys, xe, ye, *, col=None, filled=True, size=1)[source]

Draws a rectangle on the display.

Parameters
  • xs – X start coordinate

  • ys – Y start coordinate

  • xe – X end coordinate

  • ye – Y end coordinate

  • col – color of the outline and fill (expects RGB triple)

  • filled – whether the rectangle should be filled or not

  • size – size of the individual pixels, ranges from 1 to 8

circ(x, y, rad, *, col=None, filled=True, size=1)[source]

Draws a circle on the display.

Parameters
  • x – center x coordinate

  • y – center y coordinate

  • rad – radius

  • col – color of the outline and fill (expects RGB triple)

  • filled – whether the circle should be filled or not

  • size – size of the individual pixels, ranges from 1 to 8

display.open()

Opens the display. Will fail the display can’t be locked

display.close()

Closes and unlocks the display. To be able to use it again, it is necessary to open and lock it again with Display.open()