【Digi-Key Follow me Issue 2】Task 1: Control the screen to display Chinese
[Copy link]
This post was last edited by StreakingJerry on 2023-9-19 19:30
Hello everyone, I am an amateur electronic DIY enthusiast. In the first lesson, we will teach you how to use CircuitPython to display Chinese characters.
In traditional embedded development, displaying Chinese characters has always been a troublesome task, because you have to count the Chinese characters that need to be displayed first, and then take the modulus. If there are many Chinese characters to be displayed, there may be a problem of excessive memory usage. This time we conducted experiments on the Adafruit ESP32-S3 TFT Feather. This board has 4M flash, 2M PSRAM, and a 1.14-inch IPS display, which is more than enough for displaying Chinese. Not only that, because the memory is large enough, we can even import a complete Chinese font library, so there is no need to count the Chinese characters that need to be displayed in advance. This is very useful in some applications, such as web browsers, or e-books.
Let's first look at the code implementation method. The CircuitPython firmware already comes with a screen driver, so it is very convenient to use. The initialization code only requires three lines:
import board
import displayio
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import label
display = board.DISPLAY
group = displayio.Group()
display.show(group)
Here comes the point, because we need to display Chinese, we need to download the PCF font file ourselves and load it into the code.
font = bitmap_font.load_font("/wenquanyi_13px.pcf")
Next, we create a text element and add it to the screen for display, and all the settings are complete. Here we predefine a common color dictionary for easy use.
color = {
"black" : 0x000000,
"white" : 0xFFFFFF,
"red" : 0xFF0000,
"green" : 0x00FF00,
"blue" : 0x0000FF,
"cyan" : 0x00FFFF,
"magenta" : 0xFF00FF,
"yellow" : 0xFFFF00,
}
text_tg = label.Label(font, color=color["white"])
text_tg.y = 60
text_tg.x = 0
text_tg.background_color = None
text_tg.line_spacing = 1.0
text_tg.scale = 3
group.append(text_tg)
Finally, you only need to set the text content of the element in the code. When the text content changes, the new text will appear on the screen synchronously.
text_tg.text = "你好,世界!"
|