sdrangel/sdrbase/dsp/scopevis.cpp

384 wiersze
9.6 KiB
C++
Czysty Zwykły widok Historia

#include "dsp/scopevis.h"
#include "gui/glscope.h"
#include "dsp/dspcommands.h"
#include "util/messagequeue.h"
2015-07-20 20:51:49 +00:00
#include <algorithm>
#include <QDebug>
2015-07-06 07:17:51 +00:00
2015-07-13 08:46:51 +00:00
MESSAGE_CLASS_DEFINITION(ScopeVis::MsgConfigureScopeVis, Message)
2015-07-21 19:38:36 +00:00
const uint ScopeVis::m_traceChunkSize = 4800;
ScopeVis::ScopeVis(GLScope* glScope) :
m_glScope(glScope),
2015-07-20 20:51:49 +00:00
m_tracebackCount(0),
m_fill(0),
m_triggerState(Untriggered),
m_triggerIndex(0),
m_prevTrigger(false),
m_triggerPre(0),
2015-07-23 02:58:27 +00:00
m_triggerDelayCount(0),
2015-07-14 00:18:55 +00:00
m_triggerOneShot(false),
2015-07-13 22:04:34 +00:00
m_armed(false),
m_sampleRate(0)
{
setObjectName("ScopeVis");
2015-07-21 19:38:36 +00:00
m_trace.reserve(100*m_traceChunkSize);
m_trace.resize(20*m_traceChunkSize);
m_traceback.resize(20*m_traceChunkSize);
for (int i = 0; i < m_nbTriggers; i++)
{
m_triggerChannel[i] = TriggerFreeRun;
m_triggerLevel[i] = 0.0;
m_triggerPositiveEdge[i] = true;
m_triggerBothEdges[i] = false;
m_triggerDelay[i] = 0;
}
}
2015-08-14 03:00:28 +00:00
ScopeVis::~ScopeVis()
{
}
void ScopeVis::configure(MessageQueue* msgQueue,
uint triggerIndex,
2015-07-23 02:58:27 +00:00
TriggerChannel triggerChannel,
Real triggerLevel,
bool triggerPositiveEdge,
2015-07-23 04:52:12 +00:00
bool triggerBothEdges,
2015-07-23 02:58:27 +00:00
uint triggerPre,
uint triggerDelay,
uint traceSize)
{
Message* cmd = MsgConfigureScopeVis::create(triggerIndex,
triggerChannel,
2015-07-23 04:52:12 +00:00
triggerLevel,
triggerPositiveEdge,
triggerBothEdges,
triggerPre,
triggerDelay,
traceSize);
2015-08-14 03:00:28 +00:00
msgQueue->push(cmd);
}
void ScopeVis::feed(const SampleVector::const_iterator& cbegin, const SampleVector::const_iterator& end, bool positiveOnly)
{
SampleVector::const_iterator begin(cbegin);
if (m_triggerChannel[m_triggerIndex] == TriggerFreeRun) {
m_triggerPoint = begin;
}
else if (m_triggerState == Triggered) {
m_triggerPoint = begin;
}
else if (m_triggerState == Untriggered) {
m_triggerPoint = end;
}
else if (m_triggerState == WaitForReset) {
m_triggerPoint = end;
}
else {
m_triggerPoint = begin;
}
2015-07-13 08:46:51 +00:00
while(begin < end)
{
if (m_triggerChannel[m_triggerIndex] == TriggerFreeRun)
2015-07-13 08:46:51 +00:00
{
int count = end - begin;
2015-07-13 08:46:51 +00:00
if(count > (int)(m_trace.size() - m_fill))
{
2015-07-13 08:46:51 +00:00
count = m_trace.size() - m_fill;
}
2015-07-13 08:46:51 +00:00
std::vector<Complex>::iterator it = m_trace.begin() + m_fill;
for(int i = 0; i < count; ++i)
{
*it++ = Complex(begin->real() / 32768.0f, begin->imag() / 32768.0f);
2015-07-13 08:46:51 +00:00
++begin;
}
2015-07-13 08:46:51 +00:00
m_fill += count;
if(m_fill >= m_trace.size())
{
2015-07-13 08:46:51 +00:00
m_glScope->newTrace(m_trace, m_sampleRate);
m_fill = 0;
}
}
2015-07-13 21:38:10 +00:00
else
2015-07-13 08:46:51 +00:00
{
2015-07-14 00:18:55 +00:00
if(m_triggerState == WaitForReset)
{
2015-07-14 00:56:54 +00:00
break;
2015-07-14 00:18:55 +00:00
}
2015-07-23 02:58:27 +00:00
if(m_triggerState == Config)
{
m_glScope->newTrace(m_trace, m_sampleRate); // send a dummy trace
m_triggerState = Untriggered;
m_triggerIndex = 0;
2015-07-23 02:58:27 +00:00
}
if(m_triggerState == Delay)
{
int count = end - begin;
if (count > (int)(m_trace.size() - m_fill))
{
count = m_trace.size() - m_fill;
}
begin += count;
m_fill += count;
if(m_fill >= m_trace.size())
{
m_fill = 0;
m_triggerDelayCount--;
if (m_triggerDelayCount == 0)
{
if (nextTrigger())
{
m_triggerState = Untriggered;
}
else
{
m_triggerState = Triggered;
}
2015-07-23 02:58:27 +00:00
}
}
}
2015-07-13 22:04:34 +00:00
if(m_triggerState == Untriggered)
{
while(begin < end)
{
2015-07-23 04:52:12 +00:00
bool triggerCdt = triggerCondition(begin);
if (m_tracebackCount > m_triggerPre)
2015-07-20 20:51:49 +00:00
{
2015-07-23 04:52:12 +00:00
bool trigger;
if (m_triggerBothEdges[m_triggerIndex]) {
2015-07-23 04:52:12 +00:00
trigger = m_prevTrigger ^ triggerCdt;
} else {
trigger = triggerCdt ^ !m_triggerPositiveEdge[m_triggerIndex];
2015-07-23 04:52:12 +00:00
}
if (trigger)
{
if (m_armed)
{
m_armed = false;
if (m_triggerDelay[m_triggerIndex] > 0)
2015-07-23 02:58:27 +00:00
{
m_triggerDelayCount = m_triggerDelay[m_triggerIndex];
2015-07-23 02:58:27 +00:00
m_fill = 0;
m_triggerState = Delay;
}
else
{
if (nextTrigger())
{
m_triggerState = Untriggered;
}
else
{
m_triggerState = Triggered;
m_triggerPoint = begin;
// fill beginning of m_trace with delayed samples from the trace memory FIFO. Increment m_fill accordingly.
if (m_triggerPre) { // do this process only if there is a pre-trigger delay
std::copy(m_traceback.end() - m_triggerPre - 1, m_traceback.end() - 1, m_trace.begin());
m_fill = m_triggerPre; // Increment m_fill accordingly (from 0).
}
}
2015-07-23 02:58:27 +00:00
}
break;
}
2015-07-13 22:04:34 +00:00
}
else
{
m_armed = true;
}
}
2015-07-23 04:52:12 +00:00
m_prevTrigger = triggerCdt;
++begin;
}
}
2015-07-13 22:04:34 +00:00
if(m_triggerState == Triggered)
{
int count = end - begin;
if(count > (int)(m_trace.size() - m_fill))
{
count = m_trace.size() - m_fill;
}
std::vector<Complex>::iterator it = m_trace.begin() + m_fill;
for(int i = 0; i < count; ++i)
{
2015-10-09 06:45:29 +00:00
*it++ = Complex(begin->real() / 32768.0f, begin->imag() / 32768.0f);
++begin;
}
m_fill += count;
if(m_fill >= m_trace.size())
{
m_glScope->newTrace(m_trace, m_sampleRate);
m_fill = 0;
2015-07-14 00:18:55 +00:00
if (m_triggerOneShot) {
m_triggerState = WaitForReset;
} else {
2015-07-20 20:51:49 +00:00
m_tracebackCount = 0;
2015-07-14 00:18:55 +00:00
m_triggerState = Untriggered;
m_triggerIndex = 0;
2015-07-14 00:18:55 +00:00
}
}
}
}
}
}
void ScopeVis::start()
{
}
void ScopeVis::stop()
{
}
2015-08-14 03:00:28 +00:00
bool ScopeVis::handleMessage(const Message& message)
{
2015-08-17 06:29:34 +00:00
qDebug() << "ScopeVis::handleMessage";
if (DSPSignalNotification::match(message))
2015-08-14 03:00:28 +00:00
{
2015-08-17 06:29:34 +00:00
DSPSignalNotification& notif = (DSPSignalNotification&) message;
m_sampleRate = notif.getSampleRate();
qDebug() << " - DSPSignalNotification: m_sampleRate: " << m_sampleRate;
return true;
}
else if (MsgConfigureScopeVis::match(message))
{
MsgConfigureScopeVis& conf = (MsgConfigureScopeVis&) message;
m_tracebackCount = 0;
2015-07-23 02:58:27 +00:00
m_triggerState = Config;
uint index = conf.getTriggerIndex();
m_triggerChannel[index] = (TriggerChannel) conf.getTriggerChannel();
m_triggerLevel[index] = conf.getTriggerLevel();
m_triggerPositiveEdge[index] = conf.getTriggerPositiveEdge();
m_triggerBothEdges[index] = conf.getTriggerBothEdges();
2015-08-17 06:29:34 +00:00
m_triggerPre = conf.getTriggerPre();
if (m_triggerPre >= m_traceback.size())
{
m_triggerPre = m_traceback.size() - 1; // top sample in FIFO is always the triggering one (pre-trigger delay = 0)
2015-07-21 19:38:36 +00:00
}
2015-08-17 06:29:34 +00:00
m_triggerDelay[index] = conf.getTriggerDelay();
2015-08-17 06:29:34 +00:00
uint newSize = conf.getTraceSize();
if (newSize != m_trace.size())
{
2015-07-21 19:38:36 +00:00
m_trace.resize(newSize);
}
2015-08-17 06:29:34 +00:00
if (newSize > m_traceback.size()) // fitting the exact required space is not a requirement for the back trace
{
2015-07-21 19:38:36 +00:00
m_traceback.resize(newSize);
2015-07-20 20:51:49 +00:00
}
2015-08-17 06:29:34 +00:00
qDebug() << " - MsgConfigureScopeVis:"
<< " triggerIndex: " << index
<< " m_triggerChannel: " << m_triggerChannel[index]
<< " m_triggerLevel: " << m_triggerLevel[index]
<< " m_triggerPositiveEdge: " << (m_triggerPositiveEdge[index] ? "edge+" : "edge-")
<< " m_triggerBothEdges: " << (m_triggerBothEdges[index] ? "yes" : "no")
<< " m_preTrigger: " << m_triggerPre
<< " m_triggerDelay: " << m_triggerDelay[index]
<< " m_traceSize: " << m_trace.size();
2015-08-17 06:29:34 +00:00
2015-07-13 08:46:51 +00:00
return true;
}
2015-08-14 03:00:28 +00:00
else
{
2015-08-14 03:00:28 +00:00
return false;
}
}
2015-07-06 23:17:16 +00:00
void ScopeVis::setSampleRate(int sampleRate)
{
m_sampleRate = sampleRate;
}
2015-07-13 21:38:10 +00:00
bool ScopeVis::triggerCondition(SampleVector::const_iterator& it)
{
2015-10-09 06:45:29 +00:00
Complex c(it->real()/32768.0f, it->imag()/32768.0f);
2015-07-20 20:51:49 +00:00
m_traceback.push_back(c); // store into trace memory FIFO
if (m_tracebackCount < m_traceback.size()) { // increment count up to trace memory size
m_tracebackCount++;
}
if (m_triggerChannel[m_triggerIndex] == TriggerChannelI) {
return c.real() > m_triggerLevel[m_triggerIndex];
2015-07-13 21:38:10 +00:00
}
else if (m_triggerChannel[m_triggerIndex] == TriggerChannelQ) {
return c.imag() > m_triggerLevel[m_triggerIndex];
2015-07-13 21:38:10 +00:00
}
else if (m_triggerChannel[m_triggerIndex] == TriggerMagLin) {
return abs(c) > m_triggerLevel[m_triggerIndex];
2015-07-13 21:38:10 +00:00
}
else if (m_triggerChannel[m_triggerIndex] == TriggerMagDb) {
2015-07-13 21:38:10 +00:00
Real mult = (10.0f / log2f(10.0f));
Real v = c.real() * c.real() + c.imag() * c.imag();
return mult * log2f(v) > m_triggerLevel[m_triggerIndex];
2015-07-13 21:38:10 +00:00
}
else if (m_triggerChannel[m_triggerIndex] == TriggerPhase) {
return arg(c) / M_PI > m_triggerLevel[m_triggerIndex];
2015-07-13 21:38:10 +00:00
}
else {
return false;
}
}
2015-07-14 00:18:55 +00:00
void ScopeVis::setOneShot(bool oneShot)
{
m_triggerOneShot = oneShot;
2015-07-14 00:56:54 +00:00
if ((m_triggerState == WaitForReset) && !oneShot) {
2015-07-20 20:51:49 +00:00
m_tracebackCount = 0;
2015-07-14 00:56:54 +00:00
m_triggerState = Untriggered;
m_triggerIndex = 0;
}
}
bool ScopeVis::nextTrigger()
{
m_triggerIndex++;
m_prevTrigger = false;
m_triggerDelayCount = 0;
m_armed = false;
if (m_triggerIndex == m_nbTriggers)
{
m_triggerIndex = 0;
return false;
}
else if (m_triggerChannel[m_triggerIndex] == TriggerFreeRun)
{
m_triggerIndex = 0;
return false;
}
else
{
return true;
2015-07-14 00:56:54 +00:00
}
2015-07-14 00:18:55 +00:00
}