Skip to content

Commit 2a0ccba

Browse files
committed
Add example for terminalio
1 parent 0ce4758 commit 2a0ccba

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Randall Bohn (dexter)
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Demonstrate usage of the terminalio module.
6+
7+
The module provides a VT100 emulation within a displayo.TileGrid.
8+
A good reference for VT100 "escape" codes is found at
9+
https://www.csie.ntu.edu.tw/~r92094/c++/VT100.html
10+
"""
11+
import random
12+
import time
13+
import board
14+
import displayio
15+
import terminalio
16+
17+
display = board.DISPLAY
18+
group = displayio.Group()
19+
display.show(group)
20+
21+
palette = displayio.Palette(2)
22+
palette[0] = 0x220000
23+
palette[1] = 0x00FFFF
24+
25+
ROWS = 12
26+
COLS = 40
27+
28+
w, h = terminalio.FONT.get_bounding_box()
29+
30+
termgrid = displayio.TileGrid(
31+
terminalio.FONT.bitmap,
32+
pixel_shader=palette,
33+
y=20,
34+
width=COLS,
35+
height=ROWS,
36+
tile_width=w,
37+
tile_height=h,
38+
)
39+
group.append(termgrid)
40+
term = terminalio.Terminal(termgrid, terminalio.FONT)
41+
42+
43+
def world_seed(n):
44+
"""For entertainment purposes only."""
45+
letters = "AEIOUMRFCV"
46+
word = [random.choice(letters) for _ in range(n)]
47+
return "".join(word)
48+
49+
50+
term.write("Terminal %dx%d:\r\n" % (COLS, ROWS))
51+
term.write(" %dx%d pixels.\r\n" % (COLS * w, ROWS * h))
52+
term.write("VT100 protocol.\r\n")
53+
term.write("Both carriage return and line feed are required.\r\n")
54+
while True:
55+
term.write("Your world seed is:\r\n")
56+
for _ in range(10):
57+
term.write(world_seed(12) + " ")
58+
term.write("\r\n")
59+
time.sleep(10)

0 commit comments

Comments
 (0)