TTGO T-Display RP2040 Examples
These examples run on the LilyGo TTGO-T-Display available from the usual locations. See https://github.com/Xinyuan-LilyGO/LILYGO-T-display-RP2040 for more information.
ttgo_tdisplay_rp2040/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 Pin(22, Pin.OUT, value=1)
15
16 spi = SoftSPI(
17 baudrate=20000000,
18 polarity=1,
19 phase=0,
20 sck=Pin(2),
21 mosi=Pin(3),
22 miso=Pin(13))
23
24 tft = st7789.ST7789(
25 spi,
26 135,
27 240,
28 reset=Pin(0, Pin.OUT),
29 cs=Pin(5, Pin.OUT),
30 dc=Pin(1, Pin.OUT),
31 backlight=Pin(4, Pin.OUT),
32 rotation=0)
33
34 tft.fill(st7789.BLACK)
35
36 while True:
37 tft.line(
38 random.randint(0, tft.width),
39 random.randint(0, tft.height),
40 random.randint(0, tft.width),
41 random.randint(0, tft.height),
42 st7789.color565(
43 random.getrandbits(8),
44 random.getrandbits(8),
45 random.getrandbits(8)
46 )
47 )
48
49 width = random.randint(0, tft.width // 2)
50 height = random.randint(0, tft.height // 2)
51 col = random.randint(0, tft.width - width)
52 row = random.randint(0, tft.height - height)
53 tft.fill_rect(
54 col,
55 row,
56 width,
57 height,
58 st7789.color565(
59 random.getrandbits(8),
60 random.getrandbits(8),
61 random.getrandbits(8)
62 )
63 )
64
65
66main()
ttgo_tdisplay_rp2040/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 Pin(22, Pin.OUT, value=1)
32
33 spi = SoftSPI(
34 baudrate=20000000,
35 polarity=1,
36 phase=0,
37 sck=Pin(2),
38 mosi=Pin(3),
39 miso=Pin(13))
40
41 tft = st7789.ST7789(
42 spi,
43 135,
44 240,
45 reset=Pin(0, Pin.OUT),
46 cs=Pin(5, Pin.OUT),
47 dc=Pin(1, Pin.OUT),
48 backlight=Pin(4, Pin.OUT),
49 rotation=1)
50
51 while True:
52 for rotation in range(4):
53 tft.rotation(rotation)
54 tft.fill(0)
55 col_max = tft.width - font.WIDTH*6
56 row_max = tft.height - font.HEIGHT
57
58 for _ in range(100):
59 tft.text(
60 font,
61 "Hello!",
62 random.randint(0, col_max),
63 random.randint(0, row_max),
64 st7789.color565(
65 random.getrandbits(8),
66 random.getrandbits(8),
67 random.getrandbits(8)),
68 st7789.color565(
69 random.getrandbits(8),
70 random.getrandbits(8),
71 random.getrandbits(8))
72 )
73
74
75main()
ttgo_tdisplay_rp2040/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 Pin(22, Pin.OUT, value=1)
43
44 spi = SoftSPI(
45 baudrate=20000000,
46 polarity=1,
47 phase=0,
48 sck=Pin(2),
49 mosi=Pin(3),
50 miso=Pin(13))
51
52 tft = st7789.ST7789(
53 spi,
54 135,
55 240,
56 reset=Pin(0, Pin.OUT),
57 cs=Pin(5, Pin.OUT),
58 dc=Pin(1, Pin.OUT),
59 backlight=Pin(4, Pin.OUT),
60 rotation=1)
61
62 tft.fill(st7789.BLACK) # clear screen
63
64 height = tft.height # height of display in pixels
65 width = tft.width # width if display in pixels
66
67 tfa = 40 # top free area when scrolling
68 bfa = 40 # bottom free area when scrolling
69
70 scroll = 0 # scroll position
71 wheel = 0 # color wheel position
72
73 tft.vscrdef(tfa, width, bfa) # set scroll area
74 tft.vscsad(scroll + tfa) # set scroll position
75 tft.fill(st7789.BLACK) # clear screen
76
77 half = (height >> 1) - 1 # half the height of the dislay
78 interval = 0 # steps between new points
79 increment = 0 # increment per step
80 counter = 1 # step counter, overflow to start
81 current_y = 0 # current_y value (right point)
82 last_y = 0 # last_y value (left point)
83
84 # segment offsets
85 x_offsets = [x * (width // 8) - 1 for x in range(2, 9)]
86
87 while True:
88 # when the counter exceeds the interval, save current_y to last_y,
89 # choose a new random value for current_y between 0 and 1/2 the
90 # height of the display, choose a new random interval then reset
91 # the counter to 0
92
93 if counter > interval:
94 last_y = current_y
95 current_y = random.randint(0, half)
96 counter = 0
97 interval = random.randint(10, 100)
98 increment = 1/interval # increment per step
99
100 # clear the first column of the display and scroll it
101 tft.vline(scroll, 0, height, st7789.BLACK)
102 tft.vscsad(scroll + tfa)
103
104 # get the next point between last_y and current_y
105 tween = int(between(last_y, current_y, counter * increment))
106
107 # draw mirrored pixels across the display at the offsets using the color_wheel effect
108 for i, x_offset in enumerate(x_offsets):
109 tft.pixel((scroll + x_offset) % width, half +
110 tween, color_wheel(wheel+(i << 2)))
111 tft.pixel((scroll + x_offset) % width, half -
112 tween, color_wheel(wheel+(i << 2)))
113
114 # increment scroll, counter, and wheel
115 scroll = (scroll + 1) % width
116 wheel = (wheel + 1) % 256
117 counter += 1
118
119
120main()
ttgo_tdisplay_rp2040/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 Pin(22, Pin.OUT, value=1)
31
32 spi = SoftSPI(
33 baudrate=20000000,
34 polarity=1,
35 phase=0,
36 sck=Pin(2),
37 mosi=Pin(3),
38 miso=Pin(13))
39
40 tft = st7789.ST7789(
41 spi,
42 135,
43 240,
44 reset=Pin(0, Pin.OUT),
45 cs=Pin(5, Pin.OUT),
46 dc=Pin(1, Pin.OUT),
47 backlight=Pin(4, Pin.OUT),
48 rotation=0)
49
50 tft.vscrdef(40, 240, 40)
51
52 while True:
53 for font in (font1, font2, font3, font4):
54 tft.fill(st7789.BLUE)
55 line = 0
56 col = 0
57
58 for char in range(font.FIRST, font.LAST):
59 tft.text(font, chr(char), col, line, st7789.WHITE, st7789.BLUE)
60 col += font.WIDTH
61 if col > tft.width - font.WIDTH:
62 col = 0
63 line += font.HEIGHT
64
65 if line > tft.height-font.HEIGHT:
66 utime.sleep(3)
67 tft.fill(st7789.BLUE)
68 line = 0
69 col = 0
70
71 utime.sleep(3)
72
73
74main()
ttgo_tdisplay_rp2040/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 Pin(22, Pin.OUT, value=1)
28
29 spi = SoftSPI(
30 baudrate=20000000,
31 polarity=1,
32 phase=0,
33 sck=Pin(2),
34 mosi=Pin(3),
35 miso=Pin(13))
36
37 tft = st7789.ST7789(
38 spi,
39 135,
40 240,
41 reset=Pin(0, Pin.OUT),
42 cs=Pin(5, Pin.OUT),
43 dc=Pin(1, Pin.OUT),
44 backlight=Pin(4, Pin.OUT),
45 rotation=0)
46
47 last_line = tft.height - font.HEIGHT
48 tfa = 40
49 tfb = 40
50 tft.vscrdef(tfa, 240, tfb)
51
52 tft.fill(st7789.BLUE)
53 scroll = 0
54 character = 0
55 while True:
56 tft.fill_rect(0, scroll, tft.width, 1, st7789.BLUE)
57
58 if scroll % font.HEIGHT == 0:
59 tft.text(
60 font,
61 '\\x{:02x}= {:s} '.format(character, chr(character)),
62 0,
63 (scroll + last_line) % tft.height,
64 st7789.WHITE,
65 st7789.BLUE)
66
67 character = character + 1 if character < 256 else 0
68
69 tft.vscsad(scroll + tfa)
70 scroll += 1
71
72 if scroll == tft.height:
73 scroll = 0
74
75 utime.sleep(0.01)
76
77
78main()
ttgo_tdisplay_rp2040/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_rp2040/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_rp2040/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()