diff --git a/qtgui/plotter.cpp b/qtgui/plotter.cpp index 3751b3f3337ebd58690f24d5f54027ea1247928a..5af70c294bd519f84f6845e6932475b0b1e2cb57 100644 --- a/qtgui/plotter.cpp +++ b/qtgui/plotter.cpp @@ -113,7 +113,7 @@ static inline quint64 time_ms(void) CPlotter::CPlotter(QWidget *parent) : QFrame(parent) - , m_maxSize(1024,768) + , m_maxSize(1920,1080) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); setFocusPolicy(Qt::StrongFocus); diff --git a/specwidget.cpp b/specwidget.cpp index 8090b0220fa70c9ccc54d3047f9e5fe83c90ef71..f2f15ac3cd9eb23596a3619359bccc61742db09c 100644 --- a/specwidget.cpp +++ b/specwidget.cpp @@ -2,16 +2,21 @@ #include "ui_specwidget.h" #include "qtgui/bookmarks.h" #include +#include +#include +#include +#include specWidget::specWidget(QWidget *parent) : QWidget(parent), - ui(new Ui::specWidget), + ui(new Ui::specWidget), + m_attenMod(new QStandardItemModel(this)), m_runth(new uhd_io_thread(this)), - m_attenMod(new QStandardItemModel(this)) + m_saveth(new uhd_io_thread(this)), + m_bSaveToFile(false) { ui->setupUi(this); Bookmarks::create(); - loadSettings(); ui->plotter->setFftFill(false); ui->plotter->setPeakHold(true); ui->plotter->setFftPlotColor(QColor(255,255,255)); @@ -43,7 +48,8 @@ specWidget::specWidget(QWidget *parent) : connect (ui->plotter,&CPlotter::newZoomLevel,this, &specWidget::evt_newZoomLevel); connect (ui->freqCtrl,&CFreqCtrl::newFrequency,this, &specWidget::setCenterFreq); - //resetFFT(); + loadSettings(); + } specWidget::~specWidget() @@ -114,7 +120,10 @@ void specWidget::setCenterFreq(quint64 f){ tune_request_t tune_request_rx = tune_request_t((double)f, 0); usrp->set_rx_freq(tune_request_rx,m_channel); ui->plotter->setCenterFreq(usrp->get_rx_freq(m_channel)); - + } + else + { + ui->plotter->setCenterFreq(f); } if (ui->doubleSpinBox_center->value() * 1e6 !=ui->freqCtrl->getFrequency()) ui->doubleSpinBox_center->setValue(ui->freqCtrl->getFrequency()/1e6); @@ -172,6 +181,10 @@ void specWidget::setSampleRate(double rate){ usrp->set_rx_bandwidth(bw,m_channel); ui->plotter->setSampleRate(usrp->get_rx_rate(m_channel)); } + else + { + ui->plotter->setSampleRate(rate); + } } double specWidget::getSampleRate(void){ return ui->plotter->getSampleRate(); @@ -263,6 +276,27 @@ void specWidget::loadSettings() if (m_nFFTSize<128) m_nFFTSize = 128; + const int comboBox_atn = settings.value("spectrum/comboBox_atn",0).toInt(); + ui->comboBox_atn->setCurrentIndex(comboBox_atn); + + const int spinBox_chan = settings.value("spectrum/spinBox_chan",0).toInt(); + ui->spinBox_chan->setValue(spinBox_chan); + + const int spinBox_gain = settings.value("spectrum/spinBox_gain",0).toInt(); + ui->spinBox_gain->setValue(spinBox_gain); + + const double doubleSpinBox_range_max = settings.value("spectrum/doubleSpinBox_range_max",0).toInt(); + ui->doubleSpinBox_range_max->setValue(doubleSpinBox_range_max); + + const double doubleSpinBox_range_min = settings.value("spectrum/doubleSpinBox_range_min",-150).toInt(); + ui->doubleSpinBox_range_min->setValue(doubleSpinBox_range_min); + + const QString lineEdit_dev = settings.value("spectrum/lineEdit_dev","").toString(); + ui->lineEdit_dev->setText(lineEdit_dev); + + const QString lineEdit_folder = settings.value("spectrum/lineEdit_folder","./").toString(); + ui->lineEdit_folder->setText(lineEdit_folder); + } void specWidget::saveSettings() { @@ -275,6 +309,22 @@ void specWidget::saveSettings() settings.setValue("spectrum/spinBox_fftUpdate",spinBox_fftUpdate); const int comboBox_fftSize = ui->comboBox_fftSize->currentIndex(); settings.setValue("spectrum/comboBox_fftSize",comboBox_fftSize); + const int comboBox_atn = ui->comboBox_atn->currentIndex(); + settings.setValue("spectrum/comboBox_atn",comboBox_atn); + const int spinBox_gain = ui->spinBox_gain->value(); + settings.setValue("spectrum/spinBox_gain",spinBox_gain); + const int spinBox_chan = ui->spinBox_chan->value(); + settings.setValue("spectrum/spinBox_chan",spinBox_chan); + const double doubleSpinBox_range_max = ui->doubleSpinBox_range_max->value(); + settings.setValue("spectrum/doubleSpinBox_range_max",doubleSpinBox_range_max); + const double doubleSpinBox_range_min = ui->doubleSpinBox_range_min->value(); + settings.setValue("spectrum/doubleSpinBox_range_min",doubleSpinBox_range_min); + + const QString lineEdit_dev = ui->lineEdit_dev->text(); + settings.setValue("spectrum/lineEdit_dev",lineEdit_dev); + const QString lineEdit_folder = ui->lineEdit_folder->text(); + settings.setValue("spectrum/lineEdit_folder",lineEdit_folder); + } void specWidget::freeFFT() { @@ -283,6 +333,13 @@ void specWidget::freeFFT() killTimer(m_nTimerID); m_nTimerID = -1; } + if (m_runth->isRunning()) + { + stop_signal_called = true; + m_runth->wait(); + m_saveth->wait(); + } + if (m_pFFTPlan) { fftw_destroy_plan(m_pFFTPlan); @@ -300,12 +357,19 @@ void specWidget::freeFFT() } m_dHammingWnd.clear(); m_dFFTAmp.clear(); + } void specWidget::resetFFT() { ui->pushButton_reset->setText(tr("Start")); - m_channel = ui->spinBox_chan->value(); + if (m_nTimerID>=0) + killTimer(m_nTimerID); freeFFT(); + if (usrp) + { + close_device(); + } + m_pFFTIn = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * m_nFFTSize); m_pFFTOut = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * m_nFFTSize); m_pFFTPlan = fftw_plan_dft_1d(m_nFFTSize,m_pFFTIn,m_pFFTOut,FFTW_FORWARD,FFTW_ESTIMATE); @@ -315,35 +379,78 @@ void specWidget::resetFFT() m_dFFTAmp.push_back(0); } int updateIntelv = 1000.0 /ui->spinBox_fftUpdate->value() + .5; - if (m_nTimerID>=0) - killTimer(m_nTimerID); - - if (usrp) - { - close_device(); - } + m_channel = ui->spinBox_chan->value(); if (!usrp) open_device(); if (!usrp) return; + //开始 setSampleRate(getSampleRate()); setCenterFreq(ui->plotter->getCenterFreq()); QString atn = ui->comboBox_atn->currentText(); if (atn.size()) usrp->set_rx_antenna(atn.toStdString(),m_channel); - if (m_runth->isRunning()) - { - stop_signal_called = true; - m_runth->wait(); - } + + on_spinBox_gain_valueChanged(ui->spinBox_gain->value()); m_runth->setRunner([&](void){ run_IO(); }); + + //保存文件 + m_strFolder = ui->lineEdit_folder->text(); + + save_count = 0; + m_bSaveToFile = ui->checkBox_save->isChecked(); + double dfreq = ui->doubleSpinBox_center->value(); + double spr = ui->doubleSpinBox_spr->value(); + m_saveth->setRunner([&,dfreq,spr](void){ + QFile fOut; + while (!stop_signal_called) + { + if (save_count + 100 < rx_count) + { + std::shared_ptr< short > buf = vec_buffer[save_count % bufsz]; + size_t len = vec_buffersz[save_count % bufsz]; + ++save_count; + if (!m_bSaveToFile) + { + QThread::msleep(10); + continue; + } + if (fOut.isOpen()) + { + if (fOut.size()>=1024*1024*1024) + fOut.close(); + } + if (!fOut.isOpen()) + { + QDir dir; + dir.mkpath(m_strFolder); + QDateTime dtm = QDateTime::currentDateTime(); + QString sdtm = dtm.toString("yyyy_MM_dd_HH_mm_ss.zzz"); + QString fm = m_strFolder + "/"; + fm += QString("%1MHz_%2Sps_%3.iq.sc16Intel.pcm").arg(dfreq,0,'f',6) + .arg(spr,0,'f',6).arg(sdtm); + fOut.setFileName(fm); + if (!fOut.open(QIODevice::WriteOnly)) + m_bSaveToFile = false; + } + if (!m_bSaveToFile) + continue; + fOut.write((char *)buf.get(),len * sizeof(short)*2); + } + else + QThread::msleep(10); + } + if (fOut.isOpen()) + fOut.close(); + }); m_runth->start(); + m_saveth->start(); m_nTimerID = startTimer(updateIntelv); ui->pushButton_reset->setText(tr("Stop")); } @@ -380,37 +487,50 @@ void specWidget::timerEvent(QTimerEvent * e) const int fft_bytes_needed = m_nFFTSize *2 * 2; //int fft - if (e->timerId()==m_nTimerID) + if (e->timerId()==m_nTimerID ) { - const int sppoints = fft_bytes_needed/(sizeof(short))/(2); - Q_ASSERT(sppoints == m_nFFTSize); - double ref1v = 16384; - int curr_tx = (rx_count + bufsz - 100) % bufsz ; - int total_cp = 0; - memset(pBufIQ,0,sizeof(double)*2*sppoints); - while (total_cp < sppoints) + if (!m_bSaveToFile) { - const size_t sz = vec_buffersz[curr_tx]; - const short (*datap)[2] = (const short (*)[2])vec_buffer[curr_tx].get(); - for (size_t i=0;i=0 && m_nCurrentCenter < m_nFFTSize) + { + ui->sMeter->setLevel(m_dFFTAmp[m_nCurrentCenter]); + //ui->sMeter->setSqlLevel(m_dFFTAmp[m_nCurrentCenter]); } - ++curr_tx; - curr_tx %= bufsz; - if (!sz) - break; } - appendWavComplex(pBufIQ,sppoints,ref1v); - - if (m_nCurrentCenter>=0 && m_nCurrentCenter < m_nFFTSize) + else { - ui->sMeter->setLevel(m_dFFTAmp[m_nCurrentCenter]); - //ui->sMeter->setSqlLevel(m_dFFTAmp[m_nCurrentCenter]); + int he = rx_count - save_count; + + ui->lcdNumber_saveBehind->display(he); + + if (he >= bufsz) + m_bSaveToFile = false; } + static int ccct = 0; if (++ccct % 10==0) { @@ -438,6 +558,7 @@ void specWidget::on_comboBox_fftSize_activated(int index) m_nFFTSize = 65536; if (m_nFFTSize<128) m_nFFTSize = 128; + resetFFT(); saveSettings(); } @@ -480,3 +601,15 @@ void specWidget::on_spinBox_chan_valueChanged(int arg1) { m_channel = arg1; } + +void specWidget::on_toolButton_br_clicked() +{ + QString str = QFileDialog::getExistingDirectory(this,tr("Output to"),ui->lineEdit_folder->text()); + if (str.length()) + ui->lineEdit_folder->setText(str); +} + +void specWidget::on_checkBox_save_stateChanged(int arg1) +{ + m_bSaveToFile = arg1==0?false:true; +} diff --git a/specwidget.h b/specwidget.h index 51237832ce58ca81e99d8be0b4d8c60d08605328..f5a5a6619408839e603869923720fb18a2ba4629 100644 --- a/specwidget.h +++ b/specwidget.h @@ -107,6 +107,10 @@ private slots: void on_spinBox_chan_valueChanged(int arg1); + void on_toolButton_br_clicked(); + + void on_checkBox_save_stateChanged(int arg1); + private: void loadSettings(); void saveSettings(); @@ -127,7 +131,10 @@ private: double m_dManuAdd = 0; //Thread uhd_io_thread * m_runth = nullptr; - + uhd_io_thread * m_saveth = nullptr; + std::atomic save_count; + std::atomic m_bSaveToFile; + QString m_strFolder; }; #endif // SPECWIDGET_H diff --git a/specwidget.ui b/specwidget.ui index 54f956ba234c3d0226d01a6f8f7bcd03738ef20b..41eb6e63d06d6fa97225b15527f115c79964a203 100644 --- a/specwidget.ui +++ b/specwidget.ui @@ -11,7 +11,7 @@ - Form + USRP Spectrum @@ -46,6 +46,9 @@ Qt::Horizontal + + QSizePolicy::Preferred + 40 @@ -55,18 +58,12 @@ - - - - 155456 - 56844 - - - - ... + + + 8 - - true + + QLCDNumber::Flat @@ -362,7 +359,7 @@ 0 0 - 310 + 166 164 @@ -533,6 +530,45 @@ Save + + + + + Recording to Files + + + + + + + Folder + + + + + + + ... + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + diff --git a/uhd_device.cpp b/uhd_device.cpp index 8a53ba99c34ff4d0136ba136b1dc6057aafec92a..b126343227fd33fb1bbea2e43f438d5271023c48 100644 --- a/uhd_device.cpp +++ b/uhd_device.cpp @@ -62,8 +62,9 @@ uhd_device::~uhd_device() stop_signal_called = true; while (streaming) { - QThread::msleep(1000); + QThread::msleep(100); } + } void uhd_device::setDevArgs(const std::string &args)