Digi-Key Electronics Follow me Phase 2 Task Submission - Control the screen to display Chinese
[Copy link]
Digi-Key Electronics Follow me Phase 2 Task Submission - Control the screen to display Chinese
Because Chinese characters need to be displayed, font files are essential. Currently, the firmware can only recognize fonts in bdf and pcf formats. It is necessary to convert the fonts in ttf format to pcf format. Here, a font conversion tool otf2bdf is needed. According to the teacher's introduction in the live broadcast and the experience shared by other forum friends, this dictionary file size is just right for this development and will be uploaded at the end of the article.
The following code can display all Chinese characters in the font library on the screen
First, import the package. Here we use the board displayio adafruit_display_text adafruit_bitmap_font package.
You can add it directly through the shortcut keys of vscode. The specific steps are as follows
Press ctrl+shift+p on the keyboard and enter the following in the pop-up dialog box
You can get the automatic installation package service provided by CircuitPython plug-in
The import code is as follows
import board
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
Then define a Chinese string information and load the font information to display the Chinese font color, etc. The code is as follows
dis_str = "我是一条中文信息"
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
color = 0x00FFFF
Then determine the number of characters that can be displayed on each line according to the screen rotation angle
def screen_dispstr(str):
if board.DISPLAY.rotation % 180 == 0:
char_num = 23 # 横屏
else:
char_num = 13 # 竖屏
strbuf = ""
for i in range(len(str) / char_num):
strbuf = strbuf + str[i * char_num:(i + 1) * char_num] + "\n"
return strbuf
Initialize the display
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0
Display Chinese characters
text_group = displayio.Group()
text_area = label.Label(font, text=screen_dispstr(dis_str), color=color)
text_area.x = 2
text_area.y = 6
text_area.line_spacing = 0.8
text_area.scale = 1
text_group.append(text_area)
display.show(text_group)
Keep the program looping and outputting to the screen
while True:
pass
Finally, let’s take a look at the finished effect.
After downloading the code provided by the teacher, I learned how to display a picture.
image, palette = adafruit_imageload.load(
"images/ysqd.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
)
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
group = displayio.Group()
group.append(tile_grid)
board.DISPLAY.show(group)
By calling the palette method in image, you can display a picture on the screen. This study also discovered how the teacher's hidden Easter egg is the original god startup!
|