Note

This is the documentation for the latest development branch and may refer to features that are not available in released versions. If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

Display Example Explanation#

Overview#

The K230 is equipped with 1 channel of MIPI-DSI (1x4 lane), which can drive a MIPI screen or drive an HDMI display through an interface chip conversion. In addition, to facilitate debugging, we also support a virtual display. Users can choose the VIRT output device, and even without an HDMI display or LCD screen, image preview can be performed in CanMV-IDE.

Examples#

Using HDMI to Output Images#

This example outputs a 1080P image through an HDMI display.

Note

When using a larger resolution and enabling to_ide=True to transmit the image to IDE for preview, the image may occasionally flicker. This is a known issue and will be fixed in a subsequent version.

import time
import os
import urandom
import sys

from media.display import *
from media.media import *

DISPLAY_WIDTH = ALIGN_UP(1920, 16)
DISPLAY_HEIGHT = 1080

def display_test():
    print("display test")

    # Create an image for drawing
    img = image.Image(DISPLAY_WIDTH, DISPLAY_HEIGHT, image.ARGB8888)
    # Use HDMI as the display output
    Display.init(Display.LT9611, to_ide=True)
    # Initialize the media manager
    MediaManager.init()

    try:
        while True:
            img.clear()
            for i in range(10):
                x = (urandom.getrandbits(11) % img.width())
                y = (urandom.getrandbits(11) % img.height())
                r = (urandom.getrandbits(8))
                g = (urandom.getrandbits(8))
                b = (urandom.getrandbits(8))
                size = (urandom.getrandbits(30) % 64) + 32
                # Draw text at the coordinate point of the image
                img.draw_string_advanced(x, y, size, "Hello World!,你好世界!!!", color=(r, g, b))

            # Draw the result to the screen
            Display.show_image(img)

            time.sleep(1)
            os.exitpoint()
    except KeyboardInterrupt as e:
        print("用户停止: ", e)
    except BaseException as e:
        print(f"异常: {e}")

    # Destroy the display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # Release the media buffer
    MediaManager.deinit()

if __name__ == "__main__":
    os.exitpoint(os.EXITPOINT_ENABLE)
    display_test()

Using LCD to Output Images#

This example outputs an 800x480 image through an LCD (ST7701). You can also choose other supported panel types (such as NT35516, GC9503, etc.) by simply replacing Display.ST7701 with the corresponding type and setting an appropriate resolution.

import time
import os
import urandom
import sys

from media.display import *
from media.media import *

# Examples of supported panel types: Display.ST7701, Display.NT35516, Display.GC9503, Display.AML020T, etc.
DISPLAY_TYPE = Display.ST7701
DISPLAY_WIDTH = ALIGN_UP(800, 16)
DISPLAY_HEIGHT = 480

def display_test():
    print("display test")

    # Create an image for drawing
    img = image.Image(DISPLAY_WIDTH, DISPLAY_HEIGHT, image.ARGB8888)

    # Use LCD as the display output
    Display.init(DISPLAY_TYPE, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=True)
    # Initialize the media manager
    MediaManager.init()

    try:
        while True:
            img.clear()
            for i in range(10):
                x = (urandom.getrandbits(11) % img.width())
                y = (urandom.getrandbits(11) % img.height())
                r = (urandom.getrandbits(8))
                g = (urandom.getrandbits(8))
                b = (urandom.getrandbits(8))
                size = (urandom.getrandbits(30) % 64) + 32
                # Draw text at the coordinate point of the image
                img.draw_string_advanced(x, y, size, "Hello World!,你好世界!!!", color=(r, g, b))

            # Display the drawn image
            Display.show_image(img)

            time.sleep(1)
            os.exitpoint()
    except KeyboardInterrupt as e:
        print("用户停止: ", e)
    except BaseException as e:
        print(f"异常: {e}")

    # Destroy the display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # Release the media buffer
    MediaManager.deinit()

if __name__ == "__main__":
    os.exitpoint(os.EXITPOINT_ENABLE)
    display_test()

Using VIRT to Debug and Preview Images#

This example uses a virtual display output device, and users can customize the resolution and frame rate for debugging in CanMV-IDE.

import time
import os
import urandom
import sys

from media.display import *
from media.media import *

DISPLAY_WIDTH = ALIGN_UP(640, 16)
DISPLAY_HEIGHT = 480

def display_test():
    print("display test")

    # Create an image for drawing
    img = image.Image(DISPLAY_WIDTH, DISPLAY_HEIGHT, image.ARGB8888)

    # Use IDE as the output display, you can set any resolution and frame rate
    Display.init(Display.VIRT, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, fps=60)
    # Initialize the media manager
    MediaManager.init()

    try:
        while True:
            img.clear()
            for i in range(10):
                x = (urandom.getrandbits(11) % img.width())
                y = (urandom.getrandbits(11) % img.height())
                r = (urandom.getrandbits(8))
                g = (urandom.getrandbits(8))
                b = (urandom.getrandbits(8))
                size = (urandom.getrandbits(30) % 64) + 32
                # Draw text at the coordinate point of the image
                img.draw_string_advanced(x, y, size, "Hello World!,你好世界!!!", color=(r, g, b))

            # Display the drawn image
            Display.show_image(img)

            time.sleep(1)
            os.exitpoint()
    except KeyboardInterrupt as e:
        print("用户停止: ", e)
    except BaseException as e:
        print(f"异常: {e}")

    # Destroy the display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # Release the media buffer
    MediaManager.deinit()

if __name__ == "__main__":
    os.exitpoint(os.EXITPOINT_ENABLE)
    display_test()

Tip

For detailed interfaces, supported panel types, and parameters of the Display module, please refer to the API Documentation.

Comments list
Comments
Log in