feat: configure RGB colors and flash timing

This commit is contained in:
way
2026-09-27 10:04:15 +08:00
parent adfa5524b8
commit 6ca19b362b
5 changed files with 103 additions and 38 deletions
+31 -23
View File
@@ -249,23 +249,31 @@ ROW_Y=[cf("display","row1_y",0,int),cf("display","row2_y",8,int),cf("display","r
DARK_PWR=cf("dark_mode","pi_pwr_led",True,lambda x: str(x).strip().lower() in ("off","0","false","no"))
DARK_ACT=cf("dark_mode","pi_act_led",True,lambda x: str(x).strip().lower() in ("off","0","false","no"))
RGB_WRITE_DELAY_MS=max(1,cf("led","write_delay_ms",10,int))
WARNING_FLASH_MS=max(50,cf("led","warning_flash_ms",1500,int))
CRITICAL_FLASH_MS=max(50,cf("led","critical_flash_ms",750,int))
EMERGENCY_FLASH_MS=max(50,cf("led","emergency_flash_ms",300,int))
RGB_STATIC_VALUES={
"RED":(0xff,0x00,0x00),"GREEN":(0x00,0xff,0x00),"BLUE":(0x00,0x00,0xff),
"YELLOW":(0xff,0xff,0x00),"PURPLE":(0xff,0x00,0xff),"CYAN":(0x00,0xff,0x60),
"WHITE":(0xff,0xff,0xff),"ORANGE":(0xff,0x28,0x00),
WARNING_FLASH=(max(50,cf("led","warning_flash_on_ms",1500,int)),max(50,cf("led","warning_flash_off_ms",1500,int)))
CRITICAL_FLASH=(max(50,cf("led","critical_flash_on_ms",750,int)),max(50,cf("led","critical_flash_off_ms",750,int)))
EMERGENCY_FLASH=(max(50,cf("led","emergency_flash_on_ms",300,int)),max(50,cf("led","emergency_flash_off_ms",300,int)))
def rgb_cfg(name,default):
return tuple(max(0,min(255,cf("led",f"{name}_{channel}",value,int)))
for channel,value in zip(("r","g","b"),default))
RGB_GROUP_VALUES={
"CPU":rgb_cfg("cpu",(255,0,0)),
"POWER":rgb_cfg("power",(255,40,0)),
"MEMORY":rgb_cfg("memory",(255,0,255)),
"STORAGE":rgb_cfg("storage",(255,255,255)),
"NETWORK":rgb_cfg("network",(0,0,255)),
"SERVICE":rgb_cfg("service",(0,255,96)),
}
RGB_OBJECT_COLORS={
"ROOT_READONLY":"WHITE",
"TEMP_CRITICAL":"RED","TEMP_HIGH":"RED","SOFT_TEMP_LIMIT":"RED",
"THROTTLED":"RED","FREQ_CAPPED":"RED","CPU_HIGH":"RED",
"POWER_UNDERVOLT":"YELLOW",
"MEMORY_HIGH":"PURPLE",
"DISK_CRITICAL":"WHITE","DISK_HIGH":"WHITE",
"NETWORK_DOWN":"BLUE","INTERNET_DOWN":"BLUE","WIFI_WEAK":"BLUE",
"SERVICE_DOWN":"CYAN","PROCESS_DOWN":"CYAN",
RGB_OBJECT_GROUPS={
"ROOT_READONLY":"STORAGE",
"TEMP_CRITICAL":"CPU","TEMP_HIGH":"CPU","SOFT_TEMP_LIMIT":"CPU",
"THROTTLED":"CPU","FREQ_CAPPED":"CPU","CPU_HIGH":"CPU",
"POWER_UNDERVOLT":"POWER",
"MEMORY_HIGH":"MEMORY",
"DISK_CRITICAL":"STORAGE","DISK_HIGH":"STORAGE",
"NETWORK_DOWN":"NETWORK","INTERNET_DOWN":"NETWORK","WIFI_WEAK":"NETWORK",
"SERVICE_DOWN":"SERVICE","PROCESS_DOWN":"SERVICE",
}
def check_watch(w):
@@ -413,14 +421,14 @@ def update_active_alerts(cpu,temp,mem,disk,sample_elapsed=0.0):
def rgb_mode_for(request_id,severity):
if severity<=0: return "OFF"
alert_type=request_id.split("|",1)[0]
color="ORANGE" if alert_type=="POWER_UNDERVOLT" else RGB_OBJECT_COLORS[alert_type]
color=RGB_OBJECT_GROUPS[alert_type]
level="EMERGENCY" if severity>=3 else "CRITICAL" if severity>=2 else "WARNING"
return f"{color}_{level}_FLASH"
def rgb_flash(mode,on):
global _last_rgb
color=mode.split("_",1)[0]
channels=RGB_STATIC_VALUES[color]
channels=RGB_GROUP_VALUES[color]
state=(mode,bool(on))
if _last_rgb==state: return
if not on:
@@ -623,11 +631,11 @@ class DisplayController:
applied_mode=wanted_mode; animation_started=time.monotonic()
applied_flash=applied_mode.endswith("_FLASH")
if applied_flash:
elapsed=time.monotonic()-animation_started
if "_EMERGENCY_" in applied_mode: interval=EMERGENCY_FLASH_MS
elif "_CRITICAL_" in applied_mode: interval=CRITICAL_FLASH_MS
else: interval=WARNING_FLASH_MS
on=int(elapsed*1000/max(50,interval))%2==0
elapsed_ms=(time.monotonic()-animation_started)*1000.0
if "_EMERGENCY_" in applied_mode: on_ms,off_ms=EMERGENCY_FLASH
elif "_CRITICAL_" in applied_mode: on_ms,off_ms=CRITICAL_FLASH
else: on_ms,off_ms=WARNING_FLASH
on=elapsed_ms%(on_ms+off_ms)<on_ms
with bus_lock: rgb_flash(applied_mode,on)
if previous_error is not None: log("INFO","RGB_RECOVERED")
previous_error=None