From 53fff71526f5125a059d9701ab069b355db7cbc0 Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 17 Apr 2026 16:05:29 -0700 Subject: [PATCH] use concurrent to background rig calls clean up exit and polling to stop freezing GUI --- src/mainwindow.cpp | 30 ++++++++++++++++++++++++++---- src/mainwindow.h | 5 +++++ src/qsstv.pro | 2 +- src/rig/rigcontrol.cpp | 33 +++++++++++++++++++++++---------- src/rig/rigcontrol.h | 3 +++ 5 files changed, 58 insertions(+), 15 deletions(-) mode change 100644 => 100755 src/mainwindow.cpp mode change 100644 => 100755 src/mainwindow.h mode change 100644 => 100755 src/qsstv.pro mode change 100644 => 100755 src/rig/rigcontrol.cpp mode change 100644 => 100755 src/rig/rigcontrol.h diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp old mode 100644 new mode 100755 index 241ce99..283ad48 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include "filewatcher.h" #include "ftpfunctions.h" @@ -59,6 +60,9 @@ mainWindow::mainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { inStartup=true; + exitingApp=false; + rigPollInProgress=false; + rigFreqWatcher=nullptr; QApplication::instance()->thread()->setObjectName("qsstv_main"); wfTextPushButton=new QPushButton("WF Text",this); bsrPushButton=new QPushButton("BSR",this); @@ -103,6 +107,8 @@ mainWindow::mainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainW ui->statusBar->addPermanentWidget(pttIcon); statusBarPtr=statusBar(); // must be after setup UI spectrumFramePtr=ui->spectrumFrame; + rigFreqWatcher=new QFutureWatcher(this); + connect(rigFreqWatcher,SIGNAL(finished()),this,SLOT(slotRigFrequencyPollFinished())); // setting up pointers @@ -355,6 +361,7 @@ void mainWindow::slotExit() if(exit==QMessageBox::Ok) { + exitingApp=true; statusBarPtr->showMessage("Cleaning up..."); dispatcherPtr->idleAll(); rxWidgetPtr->setOnlineStatus(false); @@ -515,11 +522,26 @@ void mainWindow::slotSetFrequency(int freqIndex) void mainWindow::timerEvent(QTimerEvent *) { - double fr; - if(rigControllerPtr->getFrequency(fr)) + if(exitingApp) return; + if(rigPollInProgress) return; + + rigPollInProgress=true; + rigFreqWatcher->setFuture(QtConcurrent::run([this]() -> double { + double fr=0; + if(rigControllerPtr->getFrequency(fr)) return fr; + return -1; + })); +} + +void mainWindow::slotRigFrequencyPollFinished() +{ + rigPollInProgress=false; + if(exitingApp) return; + + const double fr=rigFreqWatcher->result(); + if(fr>1000000.) { - fr/=1000000.; - if(fr>1) freqDisplay->setText(QString::number(fr,'f',6)); + freqDisplay->setText(QString::number(fr/1000000.,'f',6)); } else { diff --git a/src/mainwindow.h b/src/mainwindow.h old mode 100644 new mode 100755 index 7dfeef3..5856881 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -5,6 +5,7 @@ #include #include #include +#include class configDialog; class spectrumWidget; @@ -45,6 +46,7 @@ private slots: void slotSendBSR(); void slotSendWfText(); void slotSetFrequency(int freqIndex); + void slotRigFrequencyPollFinished(); @@ -79,6 +81,9 @@ private slots: void timerEvent(QTimerEvent *); QStringList modModeList; QStringList modPassBandList; + bool exitingApp; + bool rigPollInProgress; + QFutureWatcher *rigFreqWatcher; }; #endif // MAINWINDOW_H diff --git a/src/qsstv.pro b/src/qsstv.pro old mode 100644 new mode 100755 index c2a47be..2cdc4f1 --- a/src/qsstv.pro +++ b/src/qsstv.pro @@ -1,6 +1,6 @@ QT += core gui -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets network xml +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets network xml concurrent CONFIG += c++11 diff --git a/src/rig/rigcontrol.cpp b/src/rig/rigcontrol.cpp old mode 100644 new mode 100755 index c006eec..587d101 --- a/src/rig/rigcontrol.cpp +++ b/src/rig/rigcontrol.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -61,11 +62,13 @@ rigControl::rigControl(int radioIndex) getRadioList(); serialP=0; lastFrequency=0.0; + getFreqErrorCount=0; xmlModes<<"USB"<<"LSB"<<"FM"<<"AM"; } rigControl::~rigControl() { + QMutexLocker locker(&rigAccessMutex); rig_close(my_rig); /* close port */ rig_cleanup(my_rig); /* if you care about memory */ } @@ -145,23 +148,27 @@ bool rigControl::getFrequency(double &frequency) if(catParams.enableXMLRPC) { frequency=xmlIntfPtr->getFrequency(); + lastFrequency=frequency; + getFreqErrorCount=0; return true; } if(!rigControlEnabled || !canGetFreq) return false; + QMutexLocker locker(&rigAccessMutex); retcode = rig_get_freq(my_rig, RIG_VFO_CURR, &frequency); - for(int i=0;i=RIGCMDTRIES) + { + // Stop polling after repeated failures to avoid blocking the UI thread. + canGetFreq=false; } - // errorMessage(retcode,"getFrequency"); - canGetFreq=false; // too many errors; frequency=lastFrequency; return false; @@ -176,6 +183,7 @@ bool rigControl::setFrequency(double frequency) return true; } if(!rigControlEnabled || !canSetFreq) return false; + QMutexLocker locker(&rigAccessMutex); // retcode = rig_set_vfo(my_rig, RIG_VFO_CURR); // if (retcode != RIG_OK ) {errorMessage(retcode,"setVFO"); return false; } @@ -194,6 +202,7 @@ bool rigControl::setFrequency(double frequency) void rigControl::disable() { + QMutexLocker locker(&rigAccessMutex); if(rigControlEnabled) { rig_close(my_rig); /* close port */ @@ -214,6 +223,7 @@ bool rigControl::getMode(QString &mode) pbwidth_t width; int retcode; if(!rigControlEnabled || !canGetMode) return false; + QMutexLocker locker(&rigAccessMutex); for(int i=0;istate; // check if backend via rigctld diff --git a/src/rig/rigcontrol.h b/src/rig/rigcontrol.h old mode 100644 new mode 100755 index f2ef67a..403d87f --- a/src/rig/rigcontrol.h +++ b/src/rig/rigcontrol.h @@ -6,6 +6,7 @@ #include #include +#include extern "C" int write_block(hamlib_port_t *p, const char *txbuffer, size_t count); extern "C" int read_block(hamlib_port_t *p, char *rxbuffer, size_t count); @@ -88,6 +89,8 @@ class rigControl: public QObject bool canGetMode; bool canSetPTT; bool canGetPTT; + int getFreqErrorCount; + QMutex rigAccessMutex; };