|
|

楼主 |
发表于 2025-9-30 19:27:54
|
显示全部楼层
我把代码发出来 ,
请大家帮我出出主意?
谢谢 !
from machine import Pin, PWM
import time
import os
import _thread
# 硬件配置(精简到最小必要)
DATA_PINS = [Pin(i, Pin.OUT, value=0) for i in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,45,46]]
CS = Pin(15, Pin.OUT, value=1)
RS = Pin(16, Pin.OUT, value=0)
WR = Pin(17, Pin.OUT, value=1)
RST = Pin(21, Pin.OUT, value=1)
BACKLIGHT = PWM(Pin(38), freq=1000, duty=0)
# 引脚操作函数(直接引用,不做额外缓存)
def set_data(data):
for i in range(16):
DATA_PINS[i].value((data >> i) & 1)
# 屏幕基础操作(极简版)
def send_cmd(cmd):
CS.value(0)
RS.value(0)
set_data(cmd)
WR.value(0)
time.sleep_us(2)
WR.value(1)
CS.value(1)
time.sleep_us(2)
def send_dat(data):
CS.value(0)
RS.value(1)
set_data(data)
WR.value(0)
time.sleep_us(2)
WR.value(1)
CS.value(1)
def set_window(x0, y0, x1, y1):
send_cmd(0x2A)
send_dat(x0>>8); send_dat(x0&0xFF); send_dat(x1>>8); send_dat(x1&0xFF)
send_cmd(0x2B)
send_dat(y0>>8); send_dat(y0&0xFF); send_dat(y1>>8); send_dat(y1&0xFF)
send_cmd(0x2C)
# 清屏函数
def clear_screen():
set_window(0, 0, 479, 319) # 480x320屏幕
CS.value(0)
RS.value(1)
set_data(0x0000) # 黑色
for _ in range(480*320):
WR.value(0)
WR.value(1)
CS.value(1)
# BMP加载函数(仅保留核心功能)
def load_bmp(path):
try:
with open(path, 'rb') as f:
f.seek(10)
offset = int.from_bytes(f.read(4), 'little')
f.seek(18)
w = int.from_bytes(f.read(4), 'little')
h = int.from_bytes(f.read(4), 'little')
f.seek(28)
if int.from_bytes(f.read(2), 'little') != 24:
return (0,0,[])
h_abs = abs(h)
data = []
row_size = (w*3 +3) & ~3
for y in range(h_abs):
f.seek(offset + ((h_abs-1-y) if h>0 else y)*row_size)
row = f.read(row_size)
for x in range(w-1, -1, -1): # 水平翻转
b, g, r = row[x*3], row[x*3+1], row[x*3+2]
rgb565 = ((b&0xF8)<<8)|((g&0xFC)<<3)|((r&0xF8)>>3)
data.append(rgb565)
return (w, h_abs, data)
except:
return (0,0,[])
# 绘制函数
def draw_bmp(bmp):
w, h, data = bmp
if w ==0 or h ==0 or not data:
return False
x = (480 - w)//2
y = (320 - h)//2
set_window(x, y, x+w-1, y+h-1)
CS.value(0)
RS.value(1)
for pixel in data:
set_data(pixel)
WR.value(0)
WR.value(1)
CS.value(1)
return True
# 主函数(完全重写,逻辑极简)
def main():
# 初始化所有变量(强制赋值)
files = []
current_idx = 0
next_data = (0,0,[])
load_ready = False
lock = _thread.allocate_lock()
running = True
# 屏幕初始化
RST.value(0)
time.sleep_ms(50)
RST.value(1)
time.sleep_ms(100)
send_cmd(0x01); time.sleep_ms(50)
send_cmd(0x11); time.sleep_ms(60)
send_cmd(0x36); send_dat(0x60)
send_cmd(0x3A); send_dat(0x05)
send_cmd(0x29); time.sleep_ms(20)
clear_screen()
print("屏幕初始化完成")
# 开启背光
BACKLIGHT.duty(444)
print("系统启动成功")
# 获取BMP文件
if "bmp" in os.listdir():
files = [f"bmp/{f}" for f in os.listdir("bmp") if f.lower().endswith(".bmp")]
files.sort()
if not files:
print("无BMP文件,退出")
return
print(f"找到{len(files)}个BMP文件")
# 预加载线程函数(内部定义,避免全局变量混乱)
def preload():
nonlocal next_data, load_ready, current_idx, running
while running:
target = (current_idx + 1) % len(files)
bmp = load_bmp(files[target])
with lock:
next_data = bmp
load_ready = True
# 等待当前索引推进
while running and current_idx == (target -1) % len(files):
time.sleep_ms(100)
# 启动预加载线程
_thread.start_new_thread(preload, ())
# 显示第一张图
first_bmp = load_bmp(files[0])
if first_bmp[0] ==0:
print("第一张图加载失败")
return
print(f"[1/{len(files)}] 显示: {files[0].split('/')[-1]}")
draw_bmp(first_bmp)
# 主循环(极简逻辑)
try:
while running:
time.sleep(3) # 显示3秒
# 准备下一张
current_bmp = (0,0,[]) # 强制初始化
with lock:
if load_ready:
current_bmp = next_data
load_ready = False
current_idx = (current_idx +1) % len(files)
else:
print("下一张未准备好,跳过")
current_idx = (current_idx +1) % len(files)
continue
# 清屏并显示
clear_screen()
fname = files[current_idx].split('/')[-1]
print(f"[{current_idx+1}/{len(files)}] 显示: {fname}")
draw_bmp(current_bmp)
except KeyboardInterrupt:
print("\n用户中断")
except Exception as e:
print(f"错误: {e}")
finally:
running = False
time.sleep_ms(200)
BACKLIGHT.duty(0)
print("程序已退出")
if __name__ == "__main__":
main()
|
|