From 89dac076a9c78b0f9436f315773353b5cb25299b Mon Sep 17 00:00:00 2001 From: That One Seong <7321839+SeongGino@users.noreply.github.com> Date: Mon, 20 May 2024 01:37:39 +0000 Subject: [PATCH] Add periodic testpulse to confirm board connection Pings the board every 5 seconds with a throwaway command; if write succeeds, all's good, but failure assumes that the board must have disconnected or is otherwise unavailable, so set the port off to avoid user confusion. --- guiwindow.cpp | 36 +++++++++++++++++++++++++++++++----- guiwindow.h | 8 ++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/guiwindow.cpp b/guiwindow.cpp index d8ce56b..475e94c 100644 --- a/guiwindow.cpp +++ b/guiwindow.cpp @@ -32,6 +32,7 @@ #include #include #include +#include // Currently loaded board object boardInfo_s board; @@ -92,6 +93,7 @@ QPushButton *renameBtn[PROFILES_COUNT]; QSvgWidget *centerPic; QGraphicsScene *testScene; +#define ALIVE_TIMER 5000 // // ^^^-------GLOBAL VARS UP THERE----------^^^ @@ -261,6 +263,8 @@ guiWindow::guiWindow(QWidget *parent) ui->testView->scale(0.5, 0.5); // Finally get to the thing! + aliveTimer = new QTimer(); + connect(aliveTimer, &QTimer::timeout, this, &guiWindow::aliveTimer_timeout); statusBar()->showMessage("Welcome to the OpenFIRE app!", 3000); PortsSearch(); usbName.prepend("[No device]"); @@ -347,7 +351,7 @@ void guiWindow::SerialLoad() serialPort.write("Xls"); serialPort.waitForBytesWritten(2000); serialPort.waitForReadyRead(2000); - for(uint8_t i = 0; i < sizeof(settingsTable) / 2 + 4; i++) { + for(uint8_t i = 0; i < sizeof(settingsTable) / 2; i++) { buffer = serialPort.readLine().trimmed(); settingsTable[i] = buffer.toInt(); settingsTable_orig[i] = settingsTable[i]; @@ -656,6 +660,7 @@ void guiWindow::DiffUpdate() ui->confirmButton->setText("[Nothing To Save]"); ui->confirmButton->setEnabled(false); } + qDebug() << settingsDiff; } @@ -734,6 +739,7 @@ void guiWindow::on_confirmButton_clicked() if(value == QMessageBox::Yes) { if(serialPort.isOpen()) { serialActive = true; + aliveTimer->stop(); // send a signal so the gun pauses its test outputs for the save op. serialPort.write("Xm"); serialPort.waitForBytesWritten(1000); @@ -817,6 +823,7 @@ void guiWindow::on_confirmButton_clicked() ui->boardLabel->setText(PrettifyName()); } serialActive = false; + aliveTimer->start(ALIVE_TIMER); serialQueue.clear(); if(!serialPort.atEnd()) { serialPort.readAll(); @@ -830,6 +837,19 @@ void guiWindow::on_confirmButton_clicked() } +void guiWindow::aliveTimer_timeout() +{ + if(serialPort.isOpen()) { + serialPort.write("."); + if(!serialPort.waitForBytesWritten(1)) { + statusBar()->showMessage("Board hasn't responded to pulse; assuming it's been disconnected."); + serialPort.close(); + ui->comPortSelector->setCurrentIndex(0); + } + } +} + + void guiWindow::on_comPortSelector_currentIndexChanged(int index) { // Indiscriminately clears the board layout views. @@ -893,9 +913,14 @@ void guiWindow::on_comPortSelector_currentIndexChanged(int index) serialPort.close(); serialActive = false; } + // try to init serial port + // if returns false, it failed, so just turn the index back to initial. if(!SerialInit(index - 1)) { ui->comPortSelector->setCurrentIndex(0); + aliveTimer->stop(); + // else, serial port is online! What do we got? } else { + aliveTimer->start(ALIVE_TIMER); ui->versionLabel->setText(QString("v%1 - \"%2\"").arg(board.versionNumber).arg(board.versionCodename)); BoxesFill(); @@ -1257,6 +1282,7 @@ void guiWindow::on_comPortSelector_currentIndexChanged(int index) serialActive = false; } qDebug() << "COM port disabled!"; + aliveTimer->stop(); ui->tabWidget->setEnabled(false); } } @@ -1655,8 +1681,7 @@ void guiWindow::serialPort_readyRead() while(!serialPort.atEnd()) { QString idleBuffer = serialPort.readLine(); if(idleBuffer.contains("Pressed:")) { - idleBuffer = idleBuffer.right(4); - idleBuffer = idleBuffer.trimmed(); + idleBuffer = idleBuffer.right(2).trimmed(); uint8_t button = idleBuffer.toInt(); switch(button) { case btnTrigger: @@ -1703,8 +1728,7 @@ void guiWindow::serialPort_readyRead() break; } } else if(idleBuffer.contains("Released:")) { - idleBuffer = idleBuffer.right(4); - idleBuffer = idleBuffer.trimmed(); + idleBuffer = idleBuffer.right(2).trimmed(); uint8_t button = idleBuffer.toInt(); switch(button) { case btnTrigger: @@ -1839,6 +1863,7 @@ void guiWindow::on_testBtn_clicked() if(serialPort.isOpen()) { // Pre-emptively put a sock in the readyRead signal serialActive = true; + aliveTimer->stop(); serialPort.write("XT"); serialPort.waitForBytesWritten(1000); serialPort.waitForReadyRead(1000); @@ -1866,6 +1891,7 @@ void guiWindow::on_testBtn_clicked() ui->dangerZoneBox->setEnabled(true); DiffUpdate(); serialActive = false; + aliveTimer->start(ALIVE_TIMER); } } } diff --git a/guiwindow.h b/guiwindow.h index cbb4099..a0a924a 100644 --- a/guiwindow.h +++ b/guiwindow.h @@ -22,6 +22,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE namespace Ui { @@ -42,6 +43,8 @@ class guiWindow : public QMainWindow bool serialActive = false; private slots: + void aliveTimer_timeout(); + void on_comPortSelector_currentIndexChanged(int index); void on_confirmButton_clicked(); @@ -192,6 +195,11 @@ private slots: bool testMode = false; + // for timer + bool boardIsAlive = false; + + QTimer *aliveTimer; + // Test Mode screen points & colors QGraphicsEllipseItem testPointTL; QGraphicsEllipseItem testPointTR;