Skip to content

Commit

Permalink
Add periodic testpulse to confirm board connection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SeongGino authored May 20, 2024
1 parent 1a49077 commit 89dac07
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
36 changes: 31 additions & 5 deletions guiwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QThread>
#include <QColorDialog>
#include <QInputDialog>
#include <QTimer>

// Currently loaded board object
boardInfo_s board;
Expand Down Expand Up @@ -92,6 +93,7 @@ QPushButton *renameBtn[PROFILES_COUNT];

QSvgWidget *centerPic;
QGraphicsScene *testScene;
#define ALIVE_TIMER 5000

//
// ^^^-------GLOBAL VARS UP THERE----------^^^
Expand Down Expand Up @@ -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]");
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -656,6 +660,7 @@ void guiWindow::DiffUpdate()
ui->confirmButton->setText("[Nothing To Save]");
ui->confirmButton->setEnabled(false);
}
qDebug() << settingsDiff;
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -1257,6 +1282,7 @@ void guiWindow::on_comPortSelector_currentIndexChanged(int index)
serialActive = false;
}
qDebug() << "COM port disabled!";
aliveTimer->stop();
ui->tabWidget->setEnabled(false);
}
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1866,6 +1891,7 @@ void guiWindow::on_testBtn_clicked()
ui->dangerZoneBox->setEnabled(true);
DiffUpdate();
serialActive = false;
aliveTimer->start(ALIVE_TIMER);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions guiwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QSerialPort>
#include <QGraphicsItem>
#include <QPen>
#include <QTimer>

QT_BEGIN_NAMESPACE
namespace Ui {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 89dac07

Please sign in to comment.