# 檔名:main.py
# 說明:示範如何載入多個 XGLCD 字型 (.c) 並顯示文字到 ILI9341
from machine import Pin, SPI
import time
import ili9341
from xglcd_font import XglcdFont
# ------------------------------------------------------------------------------
# 1. 初始化 SPI 介面與 ILI9341 螢幕 (以 ESP32 為例)
# 若使用其他板子或在 Wokwi 模擬器,請對應硬體/虛擬腳位調整
# ------------------------------------------------------------------------------
spi = SPI(1, baudrate=40000000, sck=Pin(15), mosi=Pin(7))
cs = Pin(4, Pin.OUT) # Chip Select 腳位
dc = Pin(6, Pin.OUT) # Data/Command 腳位
rst = Pin(5, Pin.OUT) # Reset 腳位
display = ili9341.Display(
spi=spi,
cs=cs,
dc=dc,
rst=rst,
width=240,
height=320,
rotation=180 # 0, 90, 180, 270 均可
)
# ------------------------------------------------------------------------------
# 2. 定義字型資訊,並載入字型
# ------------------------------------------------------------------------------
fonts_info = [
("Wingdings26x21.c", 26, 21, 32, 50)
]
loaded_fonts = {}
for font_file, w, h, start_char, letter_cnt in fonts_info:
try:
font_obj = XglcdFont(path=font_file, width=w, height=h,
start_letter=start_char,
letter_count=letter_cnt)
loaded_fonts[font_file] = font_obj
print(f"✅ 成功載入字型: {font_file}")
except Exception as e:
print(f"❌ 載入字型失敗: {font_file},錯誤: {e}")
# ------------------------------------------------------------------------------
# 3. 清除螢幕,設定背景顏色
# ------------------------------------------------------------------------------
display.clear(color=0) # 黑色背景
# ------------------------------------------------------------------------------
# 4. 顯示不同字型的文字
# ------------------------------------------------------------------------------
start_x, start_y = 10, 10 # 起始繪製座標
line_spacing = 40 # 行距
for font_name, font_obj in loaded_fonts.items():
print(f"顯示字型: {font_name}")
fc = ili9341.color565(255, 255, 0) # 黃色字體
bc = ili9341.color565(0, 0, 0) # 黑色背景
display.draw_text(x=start_x,
y=start_y,
text="0123456789",
font=font_obj,
color=fc,
background=bc,
landscape=False, # 直立顯示
spacing=1)
start_y += line_spacing
print("✅ 所有字型顯示完成!")
# ------------------------------------------------------------------------------
# 5. 保持顯示
# ------------------------------------------------------------------------------
while True:
time.sleep(1)