背光亮度可配置

This commit is contained in:
2026-01-25 10:17:02 +08:00
parent ab078c6371
commit 581515df44
2 changed files with 16 additions and 9 deletions

View File

@@ -187,9 +187,6 @@ async def animation_task():
# 每轮清理一次内存
gc.collect()
if current_frame == frame_count:
# gc.collect()
print(f"Memory: {gc.mem_free()}")
frame += 1
@@ -204,7 +201,7 @@ async def animation_task():
def start():
# 初始化液晶屏
display.init_display()
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)
gc.collect()

View File

@@ -30,7 +30,7 @@ class Display:
self._brightness = 80 # 默认亮度80%
self._initialized = True
def init_display(self):
def init_display(self, bl_pwm=True):
"""初始化液晶屏"""
try:
from machine import PWM, SPI, Pin
@@ -46,7 +46,10 @@ class Display:
)
# 初始化PWM背光控制
self._backlight = PWM(Pin(5), freq=1000)
if bl_pwm:
self._backlight = PWM(Pin(5), freq=1000)
else:
self._backlight = Pin(5, Pin.OUT)
self.brightness(self._brightness)
# 初始化并清屏
@@ -81,9 +84,16 @@ class Display:
def brightness(self, _brightness=-1):
"""设置背光亮度 (0-100)"""
if _brightness >= 0 and _brightness <= 100:
# 将0-100范围映射到0-1023 (PWM占空比)
duty = int(1023 * (100 - _brightness) / 100)
self._backlight.duty(duty)
from machine import PWM
if type(self._backlight) == PWM: # pwm设备
# 将0-100范围映射到0-1023 (PWM占空比)
duty = int(1023 * (100 - _brightness) / 100)
self._backlight.duty(duty)
elif _brightness == 0: # 关闭背光
self._backlight.on()
else: # 打开背光
self._backlight.off()
_brightness = 100
self._brightness = _brightness
return self._brightness