STTLed 封装 LVGL LED,用于布尔状态、报警状态和运行指示。它继承 STTWidget,可直接使用通用位置、显隐、事件和变量绑定接口。
#include "widgets/STTLed.h"
using namespace stt;
STTLed* led = new STTLed(getRuntime(), parent, "ledRun");
led->setMainArg(20, 20, 40, 40, false, 0x00c060, 9999);
setMainArg 参数为 x、y、w、h、初始是否点亮、颜色和圆角。实现会移除边框,并在 ison=true 时把亮度设为 LV_LED_BRIGHT_MAX。
led->on();
led->off();
led->toggle();
三个方法都返回 this,可链式调用,但连续 on()->off() 最终只保留最后状态。
led->setColor(0xff0000);
led->setBrightness(200);
uint8_t brightness = led->getBrightness();
头文件给出的有效亮度是 80..255;当前 STTLed 层不做范围检查,直接交给 LVGL。低于 80 的值及溢出值由底层类型转换和 LVGL 行为决定,调用方应主动限制。
示例:
int value = 160;
if (value < 80) value = 80;
if (value > 255) value = 255;
led->setBrightness(value);
Ivariable run = getRuntime()->getIVarMgr()->get("MotorRunning");
led->bindData(run);
onDataChanged 会用变量管理器读取整数,并按 C++ bool 规则处理:0 熄灭,任何非 0 值点亮。来源对象是当前 LED 时会跳过。
如业务变量可能使用字符串 "false",应确认变量管理器能正确转为整数;STTLed 自身不解析字符串布尔值。
void Frm01::createRunLed(STTWidget* panel) {
STTLed* led = new STTLed(getRuntime(), panel, "ledRun");
led->setMainArg(16, 16, 32, 32, false, 0x18b566, 9999);
led->setBrightness(220);
Ivariable value = getRuntime()->getIVarMgr()->get("MotorRunning");
if (value != nullptr) {
led->bindData(value);
}
}