scroll.py

Smoothly scroll all characters of a font up the display. Fonts heights must be even multiples of the screen height (i.e. 8 or 16 pixels high).

 1"""
 2scroll.py
 3=========
 4
 5    Smoothly scroll all characters of a font up the display.
 6    Fonts heights must be even multiples of the screen height
 7    (i.e. 8 or 16 pixels high).
 8"""
 9
10import time
11import s3lcd
12import tft_config
13import vga1_bold_16x32 as big
14import vga1_8x8 as small
15
16
17print(0)
18tft = tft_config.config(tft_config.WIDE)
19
20
21def cycle(p):
22    try:
23        len(p)
24    except TypeError:
25        cache = []
26        for i in p:
27            yield i
28            cache.append(i)
29        p = cache
30    while p:
31        yield from p
32
33
34def main():
35
36    try:
37        tft.init()
38
39        color = cycle(
40            (
41                s3lcd.RED,
42                s3lcd.GREEN,
43                s3lcd.BLUE,
44                s3lcd.CYAN,
45                s3lcd.MAGENTA,
46                s3lcd.YELLOW,
47                s3lcd.WHITE,
48            )
49        )
50
51        foreground = next(color)
52        background = s3lcd.BLACK
53
54        tft.fill(background)
55
56        height = tft.height()
57        width = tft.width()
58
59        font = small if tft.width() < 96 else big
60        line = height - font.HEIGHT
61
62        while True:
63            for character in range(font.FIRST, font.LAST + 1):
64                # write character hex value as a string
65                tft.text(font, f"x{character:02x}", 16, line, foreground, background)
66
67                # write character using a integer (could be > 0x7f)
68                tft.text(
69                    font,
70                    character,
71                    width - font.WIDTH * 2,
72                    line,
73                    foreground,
74                    background,
75                )
76
77                # change color for next line
78                foreground = next(color)
79
80                # next character with rollover at 256
81                character = (character +1) % height
82
83                # scroll the screen up by one character height
84                for _ in range(font.HEIGHT // 2):
85                    tft.scroll(0, -2)
86                    tft.show()
87
88            time.sleep(1)
89
90    finally:
91        tft.deinit()
92
93
94main()