temp
This commit is contained in:
parent
2a3bcd312b
commit
9dc2f8886b
41
src/main.cpp
41
src/main.cpp
@ -3,13 +3,24 @@
|
||||
#include "toml.hpp"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <csignal>
|
||||
#include <atomic>
|
||||
#include "SerialPort.h"
|
||||
#include "LedCommand.h"
|
||||
#include "LedMqttSubscriber.h"
|
||||
|
||||
using namespace toml;
|
||||
|
||||
// 声明全局变量
|
||||
int led_num = 0;
|
||||
std::atomic<bool> running(true);
|
||||
|
||||
// 信号处理器
|
||||
void signalHandler(int signum)
|
||||
{
|
||||
std::cout << "\n收到中断信号 (" << signum << "),准备退出..." << std::endl;
|
||||
running = false;
|
||||
}
|
||||
|
||||
class BreathingLight
|
||||
{
|
||||
@ -74,11 +85,28 @@ void alwaysRed(SerialPort &serial)
|
||||
void alwaysGreen(SerialPort &serial)
|
||||
{
|
||||
LedCommand cmd;
|
||||
cmd.setColor(0, 255, 0); // 红色
|
||||
cmd.setColor(0, 255, 0);
|
||||
cmd.setExtCount(led_num);
|
||||
auto packet = cmd.serialize();
|
||||
|
||||
std::cout << "发送常红数据包:";
|
||||
std::cout << "发送常绿数据包:";
|
||||
for (auto b : packet)
|
||||
printf(" %02X", b);
|
||||
std::cout << std::endl;
|
||||
|
||||
if (!serial.writeData(packet.data(), packet.size()))
|
||||
std::cerr << "发送数据失败" << std::endl;
|
||||
}
|
||||
|
||||
// 关灯
|
||||
void CloseLed(SerialPort &serial)
|
||||
{
|
||||
LedCommand cmd;
|
||||
cmd.setColor(0, 0, 0);
|
||||
cmd.setExtCount(led_num);
|
||||
auto packet = cmd.serialize();
|
||||
|
||||
std::cout << "发送关闭数据包:";
|
||||
for (auto b : packet)
|
||||
printf(" %02X", b);
|
||||
std::cout << std::endl;
|
||||
@ -89,6 +117,10 @@ void alwaysGreen(SerialPort &serial)
|
||||
|
||||
int main()
|
||||
{
|
||||
// 注册信号处理函数
|
||||
std::signal(SIGINT, signalHandler);
|
||||
std::signal(SIGTERM, signalHandler);
|
||||
|
||||
std::string port = "/dev/ttyS0";
|
||||
int baudrate = 115200;
|
||||
|
||||
@ -136,7 +168,7 @@ int main()
|
||||
|
||||
BreathingLight breathingLight;
|
||||
|
||||
while (true)
|
||||
while (running)
|
||||
{
|
||||
mqtt.checkTimeout(std::chrono::seconds(3)); // 没消息则重置数据
|
||||
|
||||
@ -164,8 +196,9 @@ int main()
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
CloseLed();
|
||||
serial.closePort();
|
||||
mqtt.disconnect();
|
||||
|
||||
std::cout << "程序已退出。" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user