加入动画显示

This commit is contained in:
2026-01-24 21:29:00 +08:00
parent 3aee06cace
commit 2dddc60a3b
3 changed files with 71 additions and 16 deletions

View File

@@ -7,15 +7,10 @@ import sys
import time import time
import machine import machine
import uasyncio
from config import config from config import config
from wifi_manager import wifi_manager from wifi_manager import wifi_manager
def print_sysinfo():
gc.collect()
print(f"Memory Free: {gc.mem_free()}")
# 全局变量存储最新的天气数据 # 全局变量存储最新的天气数据
latest_weather = None latest_weather = None
@@ -107,9 +102,6 @@ def get_weather_data(city=None, force=False):
async def weather_update_task(): async def weather_update_task():
"""定时更新天气数据的后台任务""" """定时更新天气数据的后台任务"""
# 导入uasyncio
import uasyncio
# 获取更新间隔默认10分钟 # 获取更新间隔默认10分钟
interval_minutes = config.get("weather_interval", 10) interval_minutes = config.get("weather_interval", 10)
interval_ms = interval_minutes * 60 * 1000 # 转换为毫秒 interval_ms = interval_minutes * 60 * 1000 # 转换为毫秒
@@ -132,7 +124,68 @@ async def weather_update_task():
await uasyncio.sleep_ms(60 * 1000) await uasyncio.sleep_ms(60 * 1000)
# 精简的动画显示任务
async def animation_task():
"""显示JPG动画的后台任务"""
try:
import st7789
from machine import SPI, Pin
# 初始化显示屏
tft = st7789.ST7789(
SPI(1, 40_000_000, polarity=1),
240,
240,
dc=Pin(0, Pin.OUT),
reset=Pin(2, Pin.OUT),
backlight=Pin(5, Pin.OUT),
buffer_size=0,
)
# 初始化并清屏
tft.init()
gc.collect()
tft.fill(st7789.BLACK)
tft.off()
# 动画参数
frame_count = 20
frame_delay = 10 # 帧延迟(毫秒)
print(f"开始JPG动画帧延迟: {frame_delay}ms")
frame = 0
while True:
try:
# 计算当前帧号(1-20)
current_frame = (frame % frame_count) + 1
filename = f"/rom/www/images/T{current_frame}.jpg"
# 显示当前帧
tft.jpg(filename, 160, 160, st7789.FAST)
# 控制帧率
await uasyncio.sleep_ms(frame_delay)
# 每轮清理一次内存
gc.collect()
if current_frame == frame_count:
# gc.collect()
print(f"Memory: {gc.mem_free()}")
frame += 1
except Exception as e:
print(f"动画帧错误: {e}")
# 出错后等待1秒再继续
await uasyncio.sleep_ms(1000)
except Exception as e:
print(f"动画任务初始化失败: {e}")
def start(): def start():
# init lcd screen
if not wifi_manager.connect(): if not wifi_manager.connect():
print("Failed to connect to WiFi, starting CaptivePortal for configuration") print("Failed to connect to WiFi, starting CaptivePortal for configuration")
from captive_portal import CaptivePortal from captive_portal import CaptivePortal
@@ -220,14 +273,16 @@ def start():
await request.write(json.dumps(ack)) await request.write(json.dumps(ack))
# create task # create task
import uasyncio
loop = uasyncio.get_event_loop() loop = uasyncio.get_event_loop()
loop.create_task(naw.run()) loop.create_task(naw.run())
# 启动定时天气更新任务 # 启动定时天气更新任务
loop.create_task(weather_update_task()) loop.create_task(weather_update_task())
# 启动动画显示任务
loop.create_task(animation_task())
# run! # run!
print_sysinfo() gc.collect()
print(f"App Memory Free: {gc.mem_free()}")
loop.run_forever() loop.run_forever()

View File

@@ -30,7 +30,7 @@ class Config:
self.config_data.update(kwargs) self.config_data.update(kwargs)
self._initialized = True self._initialized = True
# 自动加载配置文件 # 自动加载配置文件
self.load() self._load()
def write(self): def write(self):
"""将配置写入JSON格式的配置文件""" """将配置写入JSON格式的配置文件"""
@@ -42,10 +42,10 @@ class Config:
ujson.dump(save_data, f) ujson.dump(save_data, f)
print(f"Writing configuration to {self.CONFIG_FILE}") print(f"Writing configuration to {self.CONFIG_FILE}")
# 写入后重新加载配置 # 写入后重新加载配置
return self.load() return self._load()
return False return False
def load(self): def _load(self):
"""从配置文件加载配置""" """从配置文件加载配置"""
try: try:
with open(self.CONFIG_FILE, "r") as f: with open(self.CONFIG_FILE, "r") as f:

View File

@@ -51,7 +51,7 @@ class WiFiManager:
def connect(self): def connect(self):
"""尝试连接到WiFi""" """尝试连接到WiFi"""
# 加载配置 # 加载配置
if not self.config.load().is_valid(): if not self.config.is_valid():
print("No valid WiFi configuration") print("No valid WiFi configuration")
return False return False