增加通讯协议

This commit is contained in:
xu
2026-10-14 09:59:42 +08:00
parent 32878a9a80
commit 950e9e6c9e
102 changed files with 5538 additions and 2679 deletions
+35 -55
View File
@@ -1,14 +1,10 @@
#include "titlebar.h"
#include "ledindicator.h"
#include "appcontext.h"
#include "udpcommunication.h"
#include "confirmdialog.h"
#include "car.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTime>
#include "global.h"
#include <QStyle>
TitleBar::TitleBar(AppContext* ctx, QWidget* parent)
: QWidget(parent), m_ctx(ctx)
@@ -18,34 +14,38 @@ TitleBar::TitleBar(AppContext* ctx, QWidget* parent)
setStyleSheet(QString("QWidget#TitleBar{background-color:%1;border-bottom:1px solid %2;}")
.arg(C::PANEL, C::BORDER));
// 左侧区(通信状态)
// 左侧区(车载端通信状态)
QWidget* leftBox = new QWidget(this);
QHBoxLayout* leftLay = new QHBoxLayout(leftBox);
leftLay->setContentsMargins(0, 0, 0, 0);
leftLay->setContentsMargins(260, 0, 0, 0);
leftLay->setSpacing(20);
m_comm = new LedIndicator("通信正常", leftBox);
m_comm->setFixedHeight(80); // 容纳放大后的文字,避免上下裁切
m_comm->setTextPixelSize(45);
m_comm->setState(LedIndicator::Green);
leftLay->addWidget(m_comm);
leftLay->addStretch(1);
// 右侧区(时间 + 软急停)
// 右侧区(遥控器状态:链路 + 电量)
QWidget* rightBox = new QWidget(this);
QHBoxLayout* rightLay = new QHBoxLayout(rightBox);
rightLay->setContentsMargins(0, 0, 0, 0);
rightLay->setSpacing(20);
m_time = new QLabel("", rightBox);
m_time->setStyleSheet(QString("color:%1;font-size:60px;").arg(C::TEXT_SUB));
m_time->setAlignment(Qt::AlignCenter);
m_estop = new QPushButton("急停", rightBox);
m_estop->setProperty("variant", "estop");
m_estop->setStyleSheet("font-size:60px;");
m_estop->setFixedSize(180, 80);
rightLay->setContentsMargins(0, 0, 20, 0);
rightLay->setSpacing(30);
rightLay->addStretch(1);
rightLay->addWidget(m_time);
rightLay->addWidget(m_estop);
m_rc = new LedIndicator("遥控器断开", rightBox);
m_rc->setFixedHeight(80);
m_rc->setTextPixelSize(45);
m_rc->setState(LedIndicator::RedBlink);
rightLay->addWidget(m_rc);
m_rcBat = new QLabel("--%", rightBox);
m_rcBat->setStyleSheet(QString("color:%1;font-size:60px;").arg(C::TEXT_SUB));
m_rcBat->setAlignment(Qt::AlignCenter);
rightLay->addWidget(m_rcBat);
// 左右两侧等宽 → 中间标题恰好居中于屏幕
const int sideW = 380; // 需 >= 较宽一侧所需宽度(右侧:时间 + 急停)
// 左区需 260(左边距)+ 240(圆点区与留白 60 + 文字 4×45)= 500
// 右区需 285(圆点区与留白 60 + 文字 5×45)+ 30 + 130(电量)+ 20 = 465
const int sideW = 520;
leftBox->setFixedWidth(sideW);
rightBox->setFixedWidth(sideW);
@@ -61,30 +61,16 @@ TitleBar::TitleBar(AppContext* ctx, QWidget* parent)
lay->addWidget(m_title, 1);
lay->addWidget(rightBox);
connect(m_estop, &QPushButton::clicked, this, [this]() {
if (m_ctx->estopTriggered) {
if (ConfirmDialog::confirm(window(), "急停复位", "确定复位急停并恢复系统?"))
m_ctx->resetEstop();
} else {
m_ctx->triggerEstop();
}
});
// 通信状态刷新
connect(m_ctx->comm(), &UdpCommunication::commOkChanged, this, [this](bool ok) {
// 车载端通信状态刷新
connect(m_ctx->comm(), &CarCommunication::commOkChanged, this, [this](bool ok) {
if (ok) { m_comm->setText("通信正常"); m_comm->setState(LedIndicator::Green); }
else { m_comm->setText("通信异常"); m_comm->setState(LedIndicator::RedBlink); }
});
// 急停状态刷新
connect(m_ctx, &AppContext::estopChanged, this, &TitleBar::updateEstop);
updateEstop();
// 时钟
m_clock = new QTimer(this);
connect(m_clock, &QTimer::timeout, this, &TitleBar::updateTime);
m_clock->start(1000);
updateTime();
// 遥控器链路与电量刷新
connect(m_ctx->rc(), &RcCommunication::linkChanged, this, &TitleBar::onRcLink);
connect(m_ctx->rc(), &RcCommunication::frameReceived, this, &TitleBar::onRcFrame);
onRcLink(m_ctx->rc()->linkOk());
}
void TitleBar::setTitle(const QString& title)
@@ -92,25 +78,19 @@ void TitleBar::setTitle(const QString& title)
m_title->setText(title);
}
void TitleBar::setEstopVisible(bool visible)
void TitleBar::onRcLink(bool ok)
{
m_estop->setVisible(visible);
}
void TitleBar::updateEstop()
{
if (m_ctx->estopTriggered) {
m_estop->setText("复位");
m_estop->setProperty("variant", "danger");
if (ok) {
m_rc->setText("遥控器正常");
m_rc->setState(LedIndicator::Green);
} else {
m_estop->setText("急停");
m_estop->setProperty("variant", "estop");
m_rc->setText("遥控器断开");
m_rc->setState(LedIndicator::RedBlink);
m_rcBat->setText("--%");
}
m_estop->style()->unpolish(m_estop);
m_estop->style()->polish(m_estop);
}
void TitleBar::updateTime()
void TitleBar::onRcFrame(const RcFrame& f)
{
m_time->setText(QTime::currentTime().toString("HH:mm"));
m_rcBat->setText(QString("%1%").arg(f.battery));
}