Example Programs

The examples directory contains programs that demonstrate the use of the driver. These examples run on a T-HMI ESP32 board with a 320x240 display.

chango.py

Test for font2bitmap converter for the driver. See the font2bitmap program in the utils directory.

 1"""
 2chango.py
 3
 4    Test for font2bitmap converter for the driver.
 5    See the font2bitmap program in the utils directory.
 6"""
 7
 8from machine import freq
 9import thmi
10import gc
11import chango_16 as font_16
12import chango_32 as font_32
13import chango_64 as font_64
14
15gc.collect()
16
17
18def main():
19    # enable display and clear screen
20    tft = thmi.THMI(0)
21    tft.clear()
22
23    row = 0
24    tft.write(font_16, "abcdefghijklmnopqrst", 0, row, thmi.RED)
25    row += font_16.HEIGHT
26
27    tft.write(font_32, "abcdefghij", 0, row, thmi.GREEN)
28    row += font_32.HEIGHT
29
30    tft.write(font_64, "abcd", 0, row, thmi.BLUE)
31    row += font_64.HEIGHT
32
33
34freq(240_000_000)
35main()

feathers.py

  1"""
  2feathers.py
  3
  4    Smoothly scroll mirrored rainbow colored random curves across the display.
  5
  6"""
  7
  8import random
  9import math
 10import utime
 11from machine import freq
 12import thmi
 13
 14
 15def between(left, right, along):
 16    """returns a point along the curve from left to right"""
 17    dist = (1 - math.cos(along * math.pi)) / 2
 18    return left * (1 - dist) + right * dist
 19
 20
 21def color_wheel(position):
 22    """returns a 565 color from the given position of the color wheel"""
 23    position = (255 - position) % 255
 24
 25    if position < 85:
 26        return thmi.color565(255 - position * 3, 0, position * 3)
 27
 28    if position < 170:
 29        position -= 85
 30        return thmi.color565(0, position * 3, 255 - position * 3)
 31
 32    position -= 170
 33    return thmi.color565(position * 3, 255 - position * 3, 0)
 34
 35
 36def main():
 37    """
 38    The big show!
 39    """
 40    # enable display and clear screen
 41
 42    tft = thmi.THMI(1)
 43
 44    height = tft.height  # height of display in pixels
 45    width = tft.width  # width if display in pixels
 46
 47    tfa = 0  # top free area when scrolling
 48    bfa = 0  # bottom free area when scrolling
 49
 50    scroll = 0  # scroll position
 51    wheel = 0  # color wheel position
 52
 53    tft.vscrdef(tfa, width, bfa)  # set scroll area
 54    tft.vscsad(scroll + tfa)  # set scroll position
 55    tft.clear()  # clear screen
 56
 57    half = (height >> 1) - 1  # half the height of the dislay
 58    interval = 0  # steps between new points
 59    increment = 0  # increment per step
 60    counter = 1  # step counter, overflow to start
 61    current_y = 0  # current_y value (right point)
 62    last_y = 0  # last_y value (left point)
 63
 64    # segment offsets
 65    x_offsets = [x * (width // 8) - 1 for x in range(2, 9)]
 66
 67    while True:
 68        # when the counter exceeds the interval, save current_y to last_y,
 69        # choose a new random value for current_y between 0 and 1/2 the
 70        # height of the display, choose a new random interval then reset
 71        # the counter to 0
 72
 73        if counter > interval:
 74            last_y = current_y
 75            current_y = random.randint(0, half)
 76            counter = 0
 77            interval = random.randint(10, 100)
 78            increment = 1 / interval  # increment per step
 79
 80        # clear the first column of the display and scroll it
 81        tft.vline(scroll, 0, height, thmi.BLACK)
 82        tft.vscsad(scroll + tfa)
 83
 84        # get the next point between last_y and current_y
 85        tween = int(between(last_y, current_y, counter * increment))
 86
 87        # draw mirrored pixels across the display at the offsets using the color_wheel effect
 88        for i, x_offset in enumerate(x_offsets):
 89            tft.pixel(
 90                (scroll + x_offset) % width, half + tween, color_wheel(wheel + (i << 2))
 91            )
 92            tft.pixel(
 93                (scroll + x_offset) % width, half - tween, color_wheel(wheel + (i << 2))
 94            )
 95
 96        # increment scroll, counter, and wheel
 97        scroll = (scroll + 1) % width
 98        wheel = (wheel + 1) % 256
 99        counter += 1
100
101
102freq(240_000_000)
103main()

fonts.py

 1"""
 2fonts.py
 3
 4    Pages through all characters of four fonts on the display.
 5
 6"""
 7from machine import freq
 8import utime
 9import thmi
10
11# Choose fonts
12
13# import vga1_8x8 as font
14import vga2_8x8 as font1
15
16# import vga1_8x16 as font
17import vga2_8x16 as font2
18
19# import vga1_16x16 as font
20# import vga1_bold_16x16 as font
21# import vga2_16x16 as font
22import vga2_bold_16x16 as font3
23
24# import vga1_16x32 as font
25# import vga1_bold_16x32 as font
26# import vga2_16x32 as font
27import vga2_bold_16x32 as font4
28
29
30def main():
31    tft = thmi.THMI(0)
32    tft.vscrdef(0, 320, 0)
33
34    while True:
35        for font in (font1, font2, font3, font4):
36            tft.clear()
37            line = 0
38            col = 0
39
40            for char in range(font.FIRST, font.LAST):
41                tft.text(font, chr(char), col, line, thmi.WHITE)
42                col += font.WIDTH
43                if col > tft.width - font.WIDTH:
44                    col = 0
45                    line += font.HEIGHT
46
47                    if line > tft.height - font.HEIGHT:
48                        utime.sleep(3)
49                        tft.clear()
50                        line = 0
51                        col = 0
52
53            utime.sleep(3)
54
55
56freq(240_000_000)
57main()

hello.py

 1"""
 2hello.py
 3
 4    Writes "Hello!" in random colors at random locations on the display.
 5
 6"""
 7from machine import freq
 8import random
 9from time import sleep
10import thmi
11
12# Choose a font
13
14# import vga1_8x8 as font
15# import vga2_8x8 as font
16# import vga1_8x16 as font
17# import vga2_8x16 as font
18# import vga1_16x16 as font
19# import vga1_bold_16x16 as font
20# import vga2_16x16 as font
21# import vga2_bold_16x16 as font
22# import vga1_16x32 as font
23# import vga1_bold_16x32 as font
24# import vga2_16x32 as font
25import vga2_bold_16x32 as font
26
27
28def main():
29    tft = thmi.THMI(0)
30
31    while True:
32        for rotation in range(4):
33            tft.rotation(rotation)
34            tft.clear(random.getrandbits(8))
35
36            col_max = tft.width - font.WIDTH * 6
37            row_max = tft.height - font.HEIGHT
38
39            for _ in range(50):
40                tft.text(
41                    font,
42                    "Hello!",
43                    random.randint(0, col_max),
44                    random.randint(0, row_max),
45                    thmi.color565(
46                        random.getrandbits(8),
47                        random.getrandbits(8),
48                        random.getrandbits(8),
49                    ),
50                    thmi.color565(
51                        random.getrandbits(8),
52                        random.getrandbits(8),
53                        random.getrandbits(8),
54                    ),
55                )
56
57
58freq(240_000_000)
59main()

lines.py

 1"""
 2lines.py
 3
 4    Draws lines and rectangles in random colors at random locations on the
 5    display.
 6
 7"""
 8
 9from machine import freq
10import random
11import thmi
12
13
14def main():
15    tft = thmi.THMI(0)
16    tft.clear()
17
18    while True:
19        tft.line(
20            random.randint(0, tft.width),
21            random.randint(0, tft.height),
22            random.randint(0, tft.width),
23            random.randint(0, tft.height),
24            thmi.color565(
25                random.getrandbits(8), random.getrandbits(8), random.getrandbits(8)
26            ),
27        )
28
29        width = random.randint(0, tft.width // 2)
30        height = random.randint(0, tft.height // 2)
31        col = random.randint(0, tft.width - width)
32        row = random.randint(0, tft.height - height)
33        tft.fill_rect(
34            col,
35            row,
36            width,
37            height,
38            thmi.color565(
39                random.getrandbits(8), random.getrandbits(8), random.getrandbits(8)
40            ),
41        )
42
43
44freq(240_000_000)
45main()

noto_fonts.py

Test for font2bitmap converter for the driver. See the font2bitmap program in the utils directory.

 1"""
 2noto_fonts Writes the names of three Noto fonts centered on the display
 3    using the font. The fonts were converted from True Type fonts using
 4    the font2bitmap utility.
 5"""
 6
 7from machine import freq
 8import thmi
 9import NotoSans_32 as noto_sans
10import NotoSerif_32 as noto_serif
11import NotoSansMono_32 as noto_mono
12
13
14def main():
15    def center(font, string, row, color=thmi.WHITE):
16        screen = tft.width  # get screen width
17        width = tft.write_width(font, string)  # get the width of the string
18        col = tft.width // 2 - width // 2 if width and width < screen else 0
19        tft.write(font, string, col, row, color)  # and write the string
20
21    # enable display and clear screen
22    tft = thmi.THMI(0)
23    tft.clear()
24
25    row = 16
26
27    # center the name of the first font, using the font
28    center(noto_sans, "NotoSans", row, thmi.RED)
29    row += noto_sans.HEIGHT
30
31    # center the name of the second font, using the font
32    center(noto_serif, "NotoSerif", row, thmi.GREEN)
33    row += noto_serif.HEIGHT
34
35    # center the name of the third font, using the font
36    center(noto_mono, "NotoSansMono", row, thmi.BLUE)
37    row += noto_mono.HEIGHT
38
39
40freq(240_000_000)
41main()

proverbs.py

Displays what I hope are chinese proverbs in simplified chinese to test UTF-8 font support.

 1"""
 2proverbs.py - Displays what I hope are chinese proverbs in simplified chinese to test UTF-8
 3    font support.
 4"""
 5
 6import utime
 7from machine import freq
 8import thmi
 9import proverbs_font as font
10
11
12tft = thmi.THMI(1)
13tft.clear()
14
15
16def color_wheel(WheelPos):
17    """returns a 565 color from the given position of the color wheel"""
18    WheelPos = (255 - WheelPos) % 255
19
20    if WheelPos < 85:
21        return thmi.color565(255 - WheelPos * 3, 0, WheelPos * 3)
22
23    if WheelPos < 170:
24        WheelPos -= 85
25        return thmi.color565(0, WheelPos * 3, 255 - WheelPos * 3)
26
27    WheelPos -= 170
28    return thmi.color565(WheelPos * 3, 255 - WheelPos * 3, 0)
29
30
31def main():
32
33    proverbs = [
34        "万事起头难",
35        "熟能生巧",
36        "冰冻三尺,非一日之寒",
37        "三个臭皮匠,胜过诸葛亮",
38        "今日事,今日毕",
39        "师父领进门,修行在个人",
40        "一口吃不成胖子",
41        "欲速则不达",
42        "百闻不如一见",
43        "不入虎穴,焉得虎子",
44    ]
45
46    # initialize display
47    line_height = font.HEIGHT + 8
48    half_height = tft.height // 2
49    half_width = tft.width // 2
50    wheel = 0
51
52    tft.clear()
53
54    while True:
55        for proverb in proverbs:
56            proverb_lines = proverb.split(",")
57            half_lines_height = len(proverb_lines) * line_height // 2
58
59            tft.clear()
60
61            for count, proverb_line in enumerate(proverb_lines):
62                half_length = tft.write_width(font, proverb_line) // 2
63
64                tft.write(
65                    font,
66                    proverb_line,
67                    half_width - half_length,
68                    half_height - half_lines_height + count * line_height,
69                    color_wheel(wheel),
70                )
71
72            wheel = (wheel + 5) % 256
73
74            # pause to slow down scrolling
75            utime.sleep(5)
76
77
78main()

scroll.py

 1"""
 2scroll.py
 3
 4    Smoothly scrolls all font characters up the screen on the display.
 5
 6"""
 7from machine import freq
 8import utime
 9import random
10import thmi
11
12# choose a font
13
14# import vga1_8x8 as font
15# import vga2_8x8 as font
16# import vga1_8x16 as font
17# import vga2_8x16 as font
18# import vga1_16x16 as font
19# import vga1_bold_16x16 as font
20# import vga2_16x16 as font
21import vga2_bold_16x16 as font
22
23
24def main():
25    tft = thmi.THMI(0)
26    tft.clear()
27
28    last_line = tft.height - font.HEIGHT
29    tft.vscrdef(0, 320, 0)
30
31    scroll = 0
32    character = 0
33    while True:
34        tft.fill_rect(0, scroll, tft.width, 1, thmi.BLACK)
35
36        if scroll % font.HEIGHT == 0:
37            tft.text(
38                font,
39                "0x{:02x}= {:s} ".format(character, chr(character)),
40                96,
41                (scroll + last_line) % tft.height,
42                thmi.WHITE,
43                thmi.BLACK,
44            )
45
46            character = character + 1 if character < 256 else 0
47
48        tft.vscsad(scroll)
49        scroll += 1
50
51        if scroll == tft.height:
52            scroll = 0
53
54        utime.sleep(0.01)
55
56
57main()

toasters.py

Flying toasters sprite demo using bitmaps created from spritesheet using the sprites2bitmap.py utility. See the maketoast shell script for the command line used to create the toast_bitmaps.py from the toasters.bmp image.

 1"""
 2toasters.py
 3
 4    An example using bitmap to draw sprites on the display.
 5
 6    Spritesheet from CircuitPython_Flying_Toasters
 7    https://learn.adafruit.com/circuitpython-sprite-animation-pendant-mario-clouds-flying-toasters
 8
 9"""
10
11import random
12from machine import freq
13import thmi
14import t1, t2, t3, t4, t5
15
16TOASTERS = [t1, t2, t3, t4]
17TOAST = [t5]
18WIDTH = 320
19
20
21class toast:
22    """
23    toast class to keep track of a sprites locaton and step
24    """
25
26    def __init__(self, sprites, x, y):
27        self.sprites = sprites
28        self.steps = len(sprites)
29        self.x = x
30        self.y = y
31        self.step = random.randint(0, self.steps - 1)
32        self.speed = random.randint(2, 5)
33
34    def move(self):
35        if self.x <= 0:
36            self.speed = random.randint(2, 5)
37            self.x = WIDTH - 64
38
39        self.step += 1
40        self.step %= self.steps
41        self.x -= self.speed
42
43
44def main():
45    """
46    Initialize the display and draw flying toasters and toast
47    """
48    tft = thmi.THMI(1)
49    tft.clear()
50
51    # create toast spites in random positions
52    sprites = [
53        toast(TOASTERS, WIDTH - 64, 32),
54        toast(TOAST, WIDTH - 64 * 2, 128),
55        toast(TOASTERS, WIDTH - 64 * 4, 224),
56    ]
57
58    # move and draw sprites
59    while True:
60        for man in sprites:
61            bitmap = man.sprites[man.step]
62            tft.fill_rect(
63                man.x + bitmap.WIDTH - man.speed,
64                man.y,
65                man.speed,
66                bitmap.HEIGHT,
67                thmi.BLACK,
68            )
69
70            man.move()
71
72            if man.x > 0:
73                tft.bitmap(bitmap, man.x, man.y)
74            else:
75                tft.fill_rect(0, man.y, bitmap.WIDTH, bitmap.HEIGHT, thmi.BLACK)
76
77
78freq(240_000_000)
79main()