This commit is contained in:
cxh 2025-06-05 10:39:59 +08:00
parent 3a71b608f0
commit 27a1c72446

View File

@ -9,6 +9,48 @@
using namespace toml; using namespace toml;
class BreathingLight
{
public:
BreathingLight() : brightness(0), step(15), increasing(true) {}
void update(SerialPort &serial)
{
LedCommand cmd;
cmd.setColor(brightness, brightness, 0); // 黄色
cmd.setExtCount(1);
auto packet = cmd.serialize();
if (!serial.writeData(packet.data(), packet.size()))
{
std::cerr << "发送数据失败" << std::endl;
}
if (increasing)
{
brightness += step;
if (brightness >= 255)
{
brightness = 255;
increasing = false;
}
}
else
{
brightness -= step;
if (brightness <= 0)
{
brightness = 0;
increasing = true;
}
}
}
private:
int brightness;
int step;
bool increasing;
};
// 常红灯 // 常红灯
void alwaysRed(SerialPort &serial) void alwaysRed(SerialPort &serial)
{ {
@ -43,35 +85,6 @@ void alwaysGreen(SerialPort &serial)
std::cerr << "发送数据失败" << std::endl; std::cerr << "发送数据失败" << std::endl;
} }
// 黄色呼吸灯效果
void yellowBreathing(SerialPort &serial)
{
LedCommand cmd;
std::cout << "开始黄色呼吸灯效果..." << std::endl;
// 呼吸灯亮度从0升到255
for (int i = 0; i <= 255; i += 15)
{
cmd.setColor(i, i, 0);
cmd.setExtCount(1);
auto packet = cmd.serialize();
if (!serial.writeData(packet.data(), packet.size()))
std::cerr << "发送数据失败" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// 呼吸灯亮度从255降到0
for (int i = 255; i >= 0; i -= 15)
{
cmd.setColor(i, i, 0);
cmd.setExtCount(1);
auto packet = cmd.serialize();
if (!serial.writeData(packet.data(), packet.size()))
std::cerr << "发送数据失败" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main() int main()
{ {
std::string port = "/dev/ttyS0"; std::string port = "/dev/ttyS0";
@ -118,6 +131,8 @@ int main()
return 1; return 1;
} }
BreathingLight breathingLight;
while (true) while (true)
{ {
mqtt.checkTimeout(std::chrono::seconds(3)); // 没消息则重置数据 mqtt.checkTimeout(std::chrono::seconds(3)); // 没消息则重置数据
@ -135,7 +150,7 @@ int main()
{ {
if (speed > 0) if (speed > 0)
{ {
yellowBreathing(serial); breathingLight.update(serial);
} }
else else
{ {
@ -143,7 +158,7 @@ int main()
} }
} }
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
serial.closePort(); serial.closePort();