add font support

This commit is contained in:
2026-01-29 15:32:26 +08:00
parent 816a54a112
commit 36b4044a59
12 changed files with 1187 additions and 1089 deletions

View File

@@ -177,7 +177,7 @@ async def animation_task():
try:
# 计算当前帧号(1-20)
current_frame = (frame % frame_count) + 1
filename = f"/rom/www/images/T{current_frame}.jpg"
filename = f"/rom/images/T{current_frame}.jpg"
# 显示当前帧,右下角
display.show_jpg(filename, 160, 160)
@@ -203,15 +203,17 @@ def start():
# 初始化液晶屏
display.init_display(config.get("bl_mode") != "gpio")
display.brightness(int(config.get("brightness", 10)))
display.show_jpg("/rom/www/images/T1.jpg", 80, 80)
display.show_jpg("/rom/images/T1.jpg", 80, 80)
gc.collect()
display.message("WiFi connect ...")
if not wifi_manager.connect():
print("Failed to connect to WiFi, starting CaptivePortal for configuration")
gc.collect()
from captive_portal import CaptivePortal
portal = CaptivePortal()
display.portal_win(portal.essid.decode('ascii'))
return portal.start()
gc.collect()
# init web server
@@ -368,7 +370,8 @@ def start():
# 启动动画显示任务
loop.create_task(animation_task())
# run!
gc.collect()
print(f"App Memory Free: {gc.mem_free()}")
display.message(f"success: {gc.mem_free()}...")
# run!
loop.run_forever()

View File

@@ -29,9 +29,14 @@ class Display:
self._backlight = None
self._brightness = 80 # 默认亮度80%
self._initialized = True
self.en_font = '/rom/fonts/en-8x16.rfont'
self.cn_font = '/rom/fonts/cn-22x24.bfont'
self.vector_font = '/rom/fonts/en-32x32.hfont'
# 前景色、背景色、提示框背景色
self._COLORS = (0xFE19, 0x0000, 0x7800)
def init_display(self, bl_pwm=True):
"""初始化液晶屏"""
def init_display(self, bl_pwm=True, buffer_size=2048):
"""初始化液晶屏默认2048够用且不易有内存碎片"""
try:
from machine import PWM, SPI, Pin
@@ -42,7 +47,7 @@ class Display:
240,
dc=Pin(0, Pin.OUT),
reset=Pin(2, Pin.OUT),
buffer_size=0,
buffer_size=buffer_size,
)
# 初始化PWM背光控制
@@ -69,15 +74,11 @@ class Display:
"""检查液晶屏是否已初始化"""
return self.tft is not None
def driver(self):
"""获取液晶屏对象"""
return self.tft
def clear(self, color=st7789.BLACK):
"""清屏"""
self.tft.fill(color)
def show_jpg(self, filename, x=0, y=0, mode=st7789.FAST):
def show_jpg(self, filename, x=0, y=0, mode=st7789.SLOW):
"""显示JPG图片"""
self.tft.jpg(filename, x, y, mode)
@@ -97,6 +98,34 @@ class Display:
self._brightness = _brightness
return self._brightness
def message(self, msg, x=10, y=10, fg=st7789.WHITE, bg=st7789.BLACK):
self.tft.text(self.en_font, msg, x, y, fg, bg)
def window(self, title=None, content=None, info=None):
C_FG,C_BG,C_BT = self._COLORS
self.tft.fill(C_BG)
self.tft.rect(8, 8, 224, 224, C_FG)
self.tft.fill_rect(9, 9, 222, 40, C_BT)
self.tft.hline(9, 48, 222, C_FG)
self.tft.fill_rect(9, 192, 222, 39, C_BT)
self.tft.hline(9, 192, 222, C_FG)
if title:
self.tft.write(self.cn_font, title, 19, 17, C_FG, C_BT)
if info:
self.tft.text(self.en_font, info, 19, 204, C_FG, C_BT)
for i, line in enumerate(content):
if line:
self.tft.write(self.cn_font, line, 19, 68+i*28, C_FG, C_BG)
def portal_win(self, ssid):
tips = [
"> 连接热点:",
f"> {ssid}",
"",
"> 自动进入配置页面",
]
self.window("配置设备网络连接", tips, "portal ip: 192.168.4.1")
# 全局液晶屏实例
display = Display()