TTGO T-Display Examples

These examples run on the LilyGo TTGO-T-Display available from the usual locations. See https://github.com/Xinyuan-LilyGO/TTGO-T-Display for more information.

ttgo_tdisplay/lines.py

 1"""
 2lines.py
 3
 4    Draws lines and rectangles in random colors at random locations on the
 5    display.
 6
 7"""
 8import random
 9from machine import Pin, SoftSPI
10import st7789py as st7789
11
12
13def main():
14    spi = SoftSPI(
15        baudrate=20000000,
16        polarity=1,
17        phase=0,
18        sck=Pin(18),
19        mosi=Pin(19),
20        miso=Pin(13))
21
22    tft = st7789.ST7789(
23        spi,
24        135,
25        240,
26        reset=Pin(23, Pin.OUT),
27        cs=Pin(5, Pin.OUT),
28        dc=Pin(16, Pin.OUT),
29        backlight=Pin(4, Pin.OUT),
30        rotation=0)
31
32    tft.fill(st7789.BLACK)
33
34    while True:
35        tft.line(
36            random.randint(0, tft.width),
37            random.randint(0, tft.height),
38            random.randint(0, tft.width),
39            random.randint(0, tft.height),
40            st7789.color565(
41                random.getrandbits(8),
42                random.getrandbits(8),
43                random.getrandbits(8)
44                )
45            )
46
47        width = random.randint(0, tft.width // 2)
48        height = random.randint(0, tft.height // 2)
49        col = random.randint(0, tft.width - width)
50        row = random.randint(0, tft.height - height)
51        tft.fill_rect(
52            col,
53            row,
54            width,
55            height,
56            st7789.color565(
57                random.getrandbits(8),
58                random.getrandbits(8),
59                random.getrandbits(8)
60            )
61        )
62
63
64main()

ttgo_tdisplay/hello.py

 1"""
 2hello.py
 3
 4    Writes "Hello!" in random colors at random locations on a
 5    LILYGO® TTGO T-Display.
 6
 7    https://www.youtube.com/watch?v=atBa0BYPAAc
 8
 9"""
10import random
11from machine import Pin, SoftSPI
12import st7789py as st7789
13
14# Choose a font
15
16# from romfonts import vga1_8x8 as font
17# from romfonts import vga2_8x8 as font
18# from romfonts import vga1_8x16 as font
19# from romfonts import vga2_8x16 as font
20# from romfonts import vga1_16x16 as font
21# from romfonts import vga1_bold_16x16 as font
22# from romfonts import vga2_16x16 as font
23# from romfonts import vga2_bold_16x16 as font
24# from romfonts import vga1_16x32 as font
25# from romfonts import vga1_bold_16x32 as font
26# from romfonts import vga2_16x32 as font
27from romfonts import vga2_bold_16x32 as font
28
29
30def main():
31    spi = SoftSPI(
32        baudrate=20000000,
33        polarity=1,
34        phase=0,
35        sck=Pin(18),
36        mosi=Pin(19),
37        miso=Pin(13))
38
39    tft = st7789.ST7789(
40        spi,
41        135,
42        240,
43        reset=Pin(23, Pin.OUT),
44        cs=Pin(5, Pin.OUT),
45        dc=Pin(16, Pin.OUT),
46        backlight=Pin(4, Pin.OUT),
47        rotation=0)
48
49    while True:
50        for rotation in range(4):
51            tft.rotation(rotation)
52            tft.fill(0)
53            col_max = tft.width - font.WIDTH*6
54            row_max = tft.height - font.HEIGHT
55
56            for _ in range(100):
57                tft.text(
58                    font,
59                    "Hello!",
60                    random.randint(0, col_max),
61                    random.randint(0, row_max),
62                    st7789.color565(
63                        random.getrandbits(8),
64                        random.getrandbits(8),
65                        random.getrandbits(8)),
66                    st7789.color565(
67                        random.getrandbits(8),
68                        random.getrandbits(8),
69                        random.getrandbits(8))
70                )
71
72
73main()

ttgo_tdisplay/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 Pin, SoftSPI
 12import st7789py as st7789
 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 st7789.color565(255 - position * 3, 0, position * 3)
 27
 28    if position < 170:
 29        position -= 85
 30        return st7789.color565(0, position * 3, 255 - position * 3)
 31
 32    position -= 170
 33    return st7789.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    spi = SoftSPI(
 43        baudrate=20000000,
 44        polarity=1,
 45        phase=0,
 46        sck=Pin(18),
 47        mosi=Pin(19),
 48        miso=Pin(13))
 49
 50    tft = st7789.ST7789(
 51        spi,
 52        135,
 53        240,
 54        reset=Pin(23, Pin.OUT),
 55        cs=Pin(5, Pin.OUT),
 56        dc=Pin(16, Pin.OUT),
 57        backlight=Pin(4, Pin.OUT),
 58        rotation=1)
 59
 60    tft.fill(st7789.BLACK)      # clear screen
 61
 62    height = tft.height         # height of display in pixels
 63    width = tft.width           # width if display in pixels
 64
 65    tfa = 40                    # top free area when scrolling
 66    bfa = 40        	        # bottom free area when scrolling
 67
 68    scroll = 0                  # scroll position
 69    wheel = 0                   # color wheel position
 70
 71    tft.vscrdef(tfa, width, bfa)    # set scroll area
 72    tft.vscsad(scroll + tfa)        # set scroll position
 73    tft.fill(st7789.BLACK)          # clear screen
 74
 75    half = (height >> 1) - 1    # half the height of the dislay
 76    interval = 0                # steps between new points
 77    increment = 0               # increment per step
 78    counter = 1                 # step counter, overflow to start
 79    current_y = 0               # current_y value (right point)
 80    last_y = 0                  # last_y value (left point)
 81
 82    # segment offsets
 83    x_offsets = [x * (width // 8) -1 for x in range(2,9)]
 84
 85    while True:
 86        # when the counter exceeds the interval, save current_y to last_y,
 87        # choose a new random value for current_y between 0 and 1/2 the
 88        # height of the display, choose a new random interval then reset
 89        # the counter to 0
 90
 91        if counter > interval:
 92            last_y = current_y
 93            current_y = random.randint(0, half)
 94            counter = 0
 95            interval = random.randint(10, 100)
 96            increment = 1/interval      # increment per step
 97
 98        # clear the first column of the display and scroll it
 99        tft.vline(scroll, 0, height, st7789.BLACK)
100        tft.vscsad(scroll + tfa)
101
102        # get the next point between last_y and current_y
103        tween = int(between(last_y, current_y, counter * increment))
104
105        # draw mirrored pixels across the display at the offsets using the color_wheel effect
106        for i, x_offset in enumerate(x_offsets):
107            tft.pixel((scroll + x_offset) % width, half + tween, color_wheel(wheel+(i<<2)))
108            tft.pixel((scroll + x_offset) % width, half - tween, color_wheel(wheel+(i<<2)))
109
110        # increment scroll, counter, and wheel
111        scroll = (scroll + 1) % width
112        wheel = (wheel + 1) % 256
113        counter += 1
114
115
116main()

ttgo_tdisplay/fonts.py

 1"""
 2fonts.py
 3
 4    Pages through all characters of four fonts on the LILYGO® TTGO T-Display.
 5
 6    https://www.youtube.com/watch?v=2cnAhEucPD4
 7
 8"""
 9import utime
10from machine import Pin, SoftSPI
11import st7789py as st7789
12
13# Choose fonts
14
15# from romfonts import vga1_8x8 as font
16from romfonts import vga2_8x8 as font1
17# from romfonts import vga1_8x16 as font
18from romfonts import vga2_8x16 as font2
19# from romfonts import vga1_16x16 as font
20# from romfonts import vga1_bold_16x16 as font
21# from romfonts import vga2_16x16 as font
22from romfonts import vga2_bold_16x16 as font3
23# from romfonts import vga1_16x32 as font
24# from romfonts import vga1_bold_16x32 as font
25# from romfonts import vga2_16x32 as font
26from romfonts import vga2_bold_16x32 as font4
27
28
29def main():
30    spi = SoftSPI(
31        baudrate=20000000,
32        polarity=1,
33        phase=0,
34        sck=Pin(18),
35        mosi=Pin(19),
36        miso=Pin(13))
37
38    tft = st7789.ST7789(
39        spi,
40        135,
41        240,
42        reset=Pin(23, Pin.OUT),
43        cs=Pin(5, Pin.OUT),
44        dc=Pin(16, Pin.OUT),
45        backlight=Pin(4, Pin.OUT),
46        rotation=0)
47
48    tft.vscrdef(40, 240, 40)
49
50    while True:
51        for font in (font1, font2, font3, font4):
52            tft.fill(st7789.BLUE)
53            line = 0
54            col = 0
55
56            for char in range(font.FIRST, font.LAST):
57                tft.text(font, chr(char), col, line, st7789.WHITE, st7789.BLUE)
58                col += font.WIDTH
59                if col > tft.width - font.WIDTH:
60                    col = 0
61                    line += font.HEIGHT
62
63                    if line > tft.height-font.HEIGHT:
64                        utime.sleep(3)
65                        tft.fill(st7789.BLUE)
66                        line = 0
67                        col = 0
68
69            utime.sleep(3)
70
71
72main()

ttgo_tdisplay/scroll.py

 1"""
 2scroll.py
 3
 4    Smoothly scrolls all font characters up the screen on the LILYGO® TTGO
 5    T-Display. Only works with fonts with heights that are even multiples of
 6    the screen height, (i.e. 8 or 16 pixels high)
 7
 8"""
 9import utime
10import random
11from machine import Pin, SoftSPI
12import st7789py as st7789
13
14# choose a font
15
16# from romfonts import vga1_8x8 as font
17# from romfonts import vga2_8x8 as font
18# from romfonts import vga1_8x16 as font
19# from romfonts import vga2_8x16 as font
20# from romfonts import vga1_16x16 as font
21# from romfonts import vga1_bold_16x16 as font
22# from romfonts import vga2_16x16 as font
23from romfonts import vga2_bold_16x16 as font
24
25
26def main():
27    spi = SoftSPI(
28        baudrate=20000000,
29        polarity=1,
30        phase=0,
31        sck=Pin(18),
32        mosi=Pin(19),
33        miso=Pin(13))
34
35    tft = st7789.ST7789(
36        spi,
37        135,
38        240,
39        reset=Pin(23, Pin.OUT),
40        cs=Pin(5, Pin.OUT),
41        dc=Pin(16, Pin.OUT),
42        backlight=Pin(4, Pin.OUT),
43        rotation=0)
44
45    last_line = tft.height - font.HEIGHT
46    tfa = 40
47    tfb = 40
48    tft.vscrdef(tfa, 240, tfb)
49
50    tft.fill(st7789.BLUE)
51    scroll = 0
52    character = 0
53    while True:
54        tft.fill_rect(0, scroll, tft.width, 1, st7789.BLUE)
55
56        if scroll % font.HEIGHT == 0:
57            tft.text(
58                font,
59                '\\x{:02x}= {:s} '.format(character, chr(character)),
60                0,
61                (scroll + last_line) % tft.height,
62                st7789.WHITE,
63                st7789.BLUE)
64
65            character = character + 1 if character < 256 else 0
66
67        tft.vscsad(scroll + tfa)
68        scroll += 1
69
70        if scroll == tft.height:
71            scroll = 0
72
73        utime.sleep(0.01)
74
75
76main()

ttgo_tdisplay/toasters.py

Flying toasters sprite demo using bitmaps created from spritesheet using the imgtobitmap.py utility. See the maketoast script in the utils directory for details. See the 320x240 toasters example for a more advanced example that uses the sprites2bitmap utility and indexed bitmaps.

 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 Pin, SoftSPI
13import st7789py as st7789
14import t1, t2, t3, t4, t5
15
16TOASTERS = [t1, t2, t3, t4]
17TOAST = [t5]
18
19
20class toast():
21    '''
22    toast class to keep track of a sprites locaton and step
23    '''
24    def __init__(self, sprites, x, y):
25        self.sprites = sprites
26        self.steps = len(sprites)
27        self.x = x
28        self.y = y
29        self.step = random.randint(0, self.steps-1)
30        self.speed = random.randint(2, 5)
31
32    def move(self):
33        if self.x <= 0:
34            self.speed = random.randint(2, 5)
35            self.x = 135 - 64
36
37        self.step += 1
38        self.step %= self.steps
39        self.x -= self.speed
40
41
42def main():
43    """
44    Initialize the display and draw flying toasters and toast
45    """
46    spi = SoftSPI(
47        baudrate=20000000,
48        polarity=1,
49        phase=0,
50        sck=Pin(18),
51        mosi=Pin(19),
52        miso=Pin(13))
53
54    tft = st7789.ST7789(
55        spi,
56        135,
57        240,
58        reset=Pin(23, Pin.OUT),
59        cs=Pin(5, Pin.OUT),
60        dc=Pin(16, Pin.OUT),
61        backlight=Pin(4, Pin.OUT),
62        rotation=0)
63
64    tft.fill(st7789.BLACK)
65    # create toast spites in random positions
66    sprites = [
67        toast(TOASTERS, 135-64, 0),
68        toast(TOAST, 135-64*2, 80),
69        toast(TOASTERS, 135-64*4, 160)
70    ]
71
72    # move and draw sprites
73    while True:
74        for man in sprites:
75            bitmap = man.sprites[man.step]
76            tft.fill_rect(
77                man.x+bitmap.WIDTH-man.speed,
78                man.y,
79                man.speed,
80                bitmap.HEIGHT,
81                st7789.BLACK)
82
83            man.move()
84
85            if man.x > 0:
86                tft.bitmap(bitmap, man.x, man.y)
87            else:
88                tft.fill_rect(
89                    0,
90                    man.y,
91                    bitmap.WIDTH,
92                    bitmap.HEIGHT,
93                    st7789.BLACK)
94
95
96main()

ttgo_tdisplay/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 Pin, SoftSPI
 9import st7789py as st7789
10import gc
11from truetype import chango_16 as font_16
12from truetype import chango_32 as font_32
13from truetype import chango_64 as font_64
14
15gc.collect()
16
17
18def main():
19    # enable display and clear screen
20    spi = SoftSPI(
21        baudrate=20000000,
22        polarity=1,
23        phase=0,
24        sck=Pin(18),
25        mosi=Pin(19),
26        miso=Pin(13))
27
28    tft = st7789.ST7789(
29        spi,
30        135,
31        240,
32        reset=Pin(23, Pin.OUT),
33        cs=Pin(5, Pin.OUT),
34        dc=Pin(16, Pin.OUT),
35        backlight=Pin(4, Pin.OUT),
36        rotation=1)
37
38    tft.fill(st7789.BLACK)
39
40    row = 0
41    tft.write(font_16, "abcdefghijklmnopqrst", 0, row, st7789.RED)
42    row += font_16.HEIGHT
43
44    tft.write(font_32, "abcdefghij", 0, row, st7789.GREEN)
45    row += font_32.HEIGHT
46
47    tft.write(font_64, "abcd", 0, row, st7789.BLUE)
48    row += font_64.HEIGHT
49
50
51main()

ttgo_tdisplay/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 SoftSPI, Pin
 8import st7789py as st7789
 9
10from truetype import NotoSans_32 as noto_sans
11from truetype import NotoSerif_32 as noto_serif
12from truetype import NotoSansMono_32 as noto_mono
13
14
15def main():
16
17    def center(font, string, row, color=st7789.WHITE):
18        screen = tft.width                        # get screen width
19        width = tft.write_width(font, string)     # get the width of the string
20        if width and width < screen:              # if the string < display
21            col = tft.width // 2 - width // 2     # find the column to center
22        else:                                     # otherwise
23            col = 0                               # left justify
24
25        tft.write(font, string, col, row, color)  # and write the string
26
27    try:
28        spi = SoftSPI(
29            baudrate=20000000,
30            polarity=1,
31            phase=0,
32            sck=Pin(18),
33            mosi=Pin(19),
34            miso=Pin(13))
35
36        tft = st7789.ST7789(
37            spi,
38            135,
39            240,
40            reset=Pin(23, Pin.OUT),
41            cs=Pin(5, Pin.OUT),
42            dc=Pin(16, Pin.OUT),
43            backlight=Pin(4, Pin.OUT),
44            rotation=1)
45
46        # enable display and clear screen
47        tft.fill(st7789.BLACK)
48
49        row = 16
50
51        # center the name of the first font, using the font
52        center(noto_sans, "NotoSans", row, st7789.RED)
53        row += noto_sans.HEIGHT
54
55        # center the name of the second font, using the font
56        center(noto_serif, "NotoSerif", row, st7789.GREEN)
57        row += noto_serif.HEIGHT
58
59        # center the name of the third font, using the font
60        center(noto_mono, "NotoSansMono", row, st7789.BLUE)
61        row += noto_mono.HEIGHT
62
63    finally:
64        # shutdown spi
65        if 'spi' in locals():
66            spi.deinit()
67
68
69main()