From 9dbf2fb763c714086ca4be1b1b062efcf8f7fbf0 Mon Sep 17 00:00:00 2001 From: ezratl Date: Sat, 21 Sep 2019 14:25:29 -0400 Subject: [PATCH] Scanning works now, like actually --- src/core/ScannerSM.cpp | 21 ++++++++++---- src/core/ScannerSM.h | 4 +++ src/cubic/audio/AudioThread.cpp | 7 ++--- src/cubic/demod/DemodulatorPreThread.cpp | 8 +++--- src/cubic/sdr/SoapySDRThread.cpp | 5 ++-- src/scan/SentinelFile.cpp | 2 +- src/sigproc/Demodulator.cpp | 36 ++++++++++++++++-------- 7 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/core/ScannerSM.cpp b/src/core/ScannerSM.cpp index e6ef0d6..5032aac 100644 --- a/src/core/ScannerSM.cpp +++ b/src/core/ScannerSM.cpp @@ -102,12 +102,15 @@ void ScannerSM::ST_Scan(EventData* data){ _currentContext.state = ScannerContext::SCAN; _manualMode = false; - // incremental scan pattern - _entryCounter = (_entryCounter + 1) % _currentSystem->size(); - if(currentState != lastState) _broadcastContextUpdate(); + // incremental scan pattern + if(!_squelchHits || (currentState != lastState)){ + _entryCounter = (_entryCounter + 1) % _currentSystem->size(); + + + if(_entryCounter == 0){ _sysCounter = (_sysCounter + 1) % _systems.size(); @@ -121,12 +124,20 @@ void ScannerSM::ST_Scan(EventData* data){ _currentEntry = _currentSystem->operator[](_entryCounter); CHECK_F(_currentEntry != NULL); + } if(_currentEntry->hasSignal()){ - LOG_F(2, "Signal found: %s", _currentEntry->tag().c_str()); - InternalEvent(ST_RECEIVE); + _squelchHits++; + if(_squelchHits >= SQUELCH_TRIGGER_HITS){ + LOG_F(2, "Signal found: %s", _currentEntry->tag().c_str()); + InternalEvent(ST_RECEIVE); + } + else{ + InternalEvent(ST_SCAN); + } } else{ + _squelchHits = 0; InternalEvent(ST_SCAN); } diff --git a/src/core/ScannerSM.h b/src/core/ScannerSM.h index 26cc149..4a1f8e9 100644 --- a/src/core/ScannerSM.h +++ b/src/core/ScannerSM.h @@ -16,6 +16,8 @@ #include "messages.h" #include "clientmessage.h" +#define SQUELCH_TRIGGER_HITS 10 + namespace piscan { class ScannerSM: public MessageReceiver, public StateMachine { @@ -74,6 +76,8 @@ private: bool _manualMode = false; std::time_t timeoutStart = 0; + int _squelchHits = 0; + void _broadcastContextUpdate(); void _enableAudioOut(bool en); void _handleRequest(ClientRequest& request); diff --git a/src/cubic/audio/AudioThread.cpp b/src/cubic/audio/AudioThread.cpp index 5024d54..da97578 100644 --- a/src/cubic/audio/AudioThread.cpp +++ b/src/cubic/audio/AudioThread.cpp @@ -129,16 +129,13 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned //lock every single boundThread srcmix in succession the time we process //its audio samples. //std::lock_guard lock(srcmix->getMutex()); - std::unique_lock lock2(srcmix->getMutex(), std::defer_lock); - - //this is hacky AF but whatever - while(!lock2.try_lock()) - std::this_thread::yield(); if (srcmix->isTerminated() || !srcmix->inputQueue || srcmix->inputQueue->empty() || !srcmix->isActive()) { continue; } + std::lock_guard lock2(srcmix->getMutex()); + if (!srcmix->currentInput) { srcmix->audioQueuePtr = 0; diff --git a/src/cubic/demod/DemodulatorPreThread.cpp b/src/cubic/demod/DemodulatorPreThread.cpp index e1fed16..5dee272 100644 --- a/src/cubic/demod/DemodulatorPreThread.cpp +++ b/src/cubic/demod/DemodulatorPreThread.cpp @@ -88,12 +88,12 @@ void DemodulatorPreThread::run() { currentFrequency.store(newFrequency); frequencyChanged.store(false); - /*std::unique_lock lock(_msgMutex, std::defer_lock); + std::unique_lock lock(_msgMutex, std::defer_lock); if(lock.try_lock()){ _msgAvailable = true; lock.unlock(); } - _cv.notify_one();*/ + _cv.notify_one(); } if (inp->sampleRate != currentSampleRate) { @@ -312,10 +312,10 @@ void DemodulatorPreThread::setFrequency(long long freq) { frequencyChanged.store(true); newFrequency = freq; - /*if(!stopping.load()){ + if(!stopping.load()){ std::unique_lock lock(_msgMutex); _cv.wait(lock, [this]{return this->_msgAvailable;}); - }*/ + } } long long DemodulatorPreThread::getFrequency() { diff --git a/src/cubic/sdr/SoapySDRThread.cpp b/src/cubic/sdr/SoapySDRThread.cpp index e150404..4bb40af 100644 --- a/src/cubic/sdr/SoapySDRThread.cpp +++ b/src/cubic/sdr/SoapySDRThread.cpp @@ -383,8 +383,9 @@ int SDRThread::readStream(SDRThreadIQDataQueuePtr iqDataOutQueue) { readStreamCode = -31; //std::cout << "SDRThread::readStream(): 3.1 iqDataOutQueue output queue is full, discard processing of the batch..." << std::endl; if(!stopping.load()){ - if(++blocked_attempts >= 50) - throw new std::runtime_error("SDRPostThread is blocked - deadlock"); + if(++blocked_attempts >= 50){ + ABORT_F("SDRPostThread is blocked - deadlock"); + } LOG_F(WARNING, "SDRThread::readStream(): 3.1 iqDataOutQueue output queue is full, discard processing of the batch..."); } //saturation, let a chance to the other threads to consume the existing samples diff --git a/src/scan/SentinelFile.cpp b/src/scan/SentinelFile.cpp index 0ad3ee2..caf413b 100644 --- a/src/scan/SentinelFile.cpp +++ b/src/scan/SentinelFile.cpp @@ -103,7 +103,7 @@ void SentinelFile::_newAnalogEntry(std::vector& tokens){ if(!mode.compare(SENTINEL_NFM) || !mode.compare(SENTINEL_FM) || !mode.compare(SENTINEL_AUTO)) { Entry* entry = new FMChannel(std::stoul(freq), tag, - (!lockout.compare(SENTINEL_TRUE)), (!delay.compare(SENTINEL_TRUE))); + (!lockout.compare(SENTINEL_TRUE)), (delay.compare("0"))); _system->addEntry(entry); } } diff --git a/src/sigproc/Demodulator.cpp b/src/sigproc/Demodulator.cpp index d309fde..b637994 100644 --- a/src/sigproc/Demodulator.cpp +++ b/src/sigproc/Demodulator.cpp @@ -104,8 +104,8 @@ void Demodulator::start(){ newDemod->setSquelchLevel(-100); newDemod->setMuted(true); newDemod->setOutputDevice(audiodev); - newDemod->setFrequency(INIT_FREQUENCY); newDemod->run(); + newDemod->setFrequency(INIT_FREQUENCY); _demods[NFM] = newDemod; LOG_F(INFO, "Added modem NFM"); @@ -153,20 +153,27 @@ void Demodulator::stop(){ } bool Demodulator::setFrequency(uint32_t freq) { - if(freq == _demodMgr.getActiveContextModem()->getFrequency()){ + /*if(freq == _demodMgr.getCurrentModem()->getFrequency()){ DLOG_F(9, "Frequency already set"); return true; + }*/ + + _demodMgr.getCurrentModem()->setFrequency(freq); + + //TODO account for bandwidth + if(std::abs(_cubic->getFrequency() - freq) >= (_cubic->getSampleRate() / 2)){ + _cubic->setFrequency(freq); + //also arbitrary + usleep(200000); } - _demodMgr.getActiveContextModem()->setFrequency(freq); - //TODO account for bandwidth - if(std::abs(_cubic->getFrequency() - _demodMgr.getActiveContextModem()->getFrequency()) >= (_cubic->getSampleRate() / 2)) - _cubic->setFrequency(freq); + _demodMgr.getCurrentModem()->setFrequency(freq); + //this is totally arbitrary + usleep(1000); _currentFreq = freq; - //this is totally arbitrary - usleep(1000); + return true; } @@ -236,11 +243,17 @@ void Demodulator::_handleRequest(ClientRequest& request){ break; case DEMOD_SET_GAIN: DCHECK_F(data != nullptr); - //_tuner.setGain(*data); - if (*data == AUTO_GAIN) + + if (*data == AUTO_GAIN){ + _cubic->setAGCMode(true); + LOG_F(1, "Gain set to auto"); - else + } + else{ + _cubic->setAGCMode(false); + _cubic->setGain("TUNER", *data); LOG_F(1, "Gain set to %i", *data); + } _gain = *data; delete data; break; @@ -251,7 +264,6 @@ void Demodulator::_handleRequest(ClientRequest& request){ _contextUpdate(); } else if(request.rqInfo.type == GET_CONTEXT){ - _gain = _cubic->getGain("TUNER"); DemodContext* context = new DemodContext(_gain, _squelchLevel); request.connection->demodContextRequestCallback(request.rqHandle, context); }