2014-05-18 11:52:39 -04:00
|
|
|
#include "dsp/scopevis.h"
|
|
|
|
#include "gui/glscope.h"
|
|
|
|
#include "dsp/dspcommands.h"
|
|
|
|
#include "util/messagequeue.h"
|
2015-07-20 16:51:49 -04:00
|
|
|
#include <algorithm>
|
2014-05-18 11:52:39 -04:00
|
|
|
|
2015-07-06 03:17:51 -04:00
|
|
|
#include <cstdio>
|
2015-07-13 17:38:10 -04:00
|
|
|
#include <iostream>
|
2015-07-06 03:17:51 -04:00
|
|
|
|
2015-07-13 04:46:51 -04:00
|
|
|
MESSAGE_CLASS_DEFINITION(ScopeVis::MsgConfigureScopeVis, Message)
|
|
|
|
|
2015-07-21 15:38:36 -04:00
|
|
|
const uint ScopeVis::m_traceChunkSize = 4800;
|
|
|
|
|
2014-05-18 11:52:39 -04:00
|
|
|
ScopeVis::ScopeVis(GLScope* glScope) :
|
|
|
|
m_glScope(glScope),
|
2015-07-20 16:51:49 -04:00
|
|
|
m_tracebackCount(0),
|
2014-05-18 11:52:39 -04:00
|
|
|
m_fill(0),
|
|
|
|
m_triggerState(Untriggered),
|
|
|
|
m_triggerChannel(TriggerFreeRun),
|
2015-07-13 17:38:10 -04:00
|
|
|
m_triggerLevel(0.0),
|
2015-07-13 04:46:51 -04:00
|
|
|
m_triggerPositiveEdge(true),
|
2015-07-21 16:18:17 -04:00
|
|
|
m_triggerPre(0),
|
2015-07-13 20:18:55 -04:00
|
|
|
m_triggerOneShot(false),
|
2015-07-13 18:04:34 -04:00
|
|
|
m_armed(false),
|
2014-05-18 11:52:39 -04:00
|
|
|
m_sampleRate(0)
|
|
|
|
{
|
2015-07-21 15:38:36 -04:00
|
|
|
m_trace.reserve(100*m_traceChunkSize);
|
|
|
|
m_trace.resize(20*m_traceChunkSize);
|
|
|
|
m_traceback.resize(20*m_traceChunkSize);
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 16:18:17 -04:00
|
|
|
void ScopeVis::configure(MessageQueue* msgQueue, TriggerChannel triggerChannel, Real triggerLevel, bool triggerPositiveEdge, uint triggerPre, uint traceSize)
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2015-07-21 16:18:17 -04:00
|
|
|
Message* cmd = MsgConfigureScopeVis::create(triggerChannel, triggerLevel, triggerPositiveEdge, triggerPre, traceSize);
|
2014-05-18 11:52:39 -04:00
|
|
|
cmd->submit(msgQueue, this);
|
|
|
|
}
|
|
|
|
|
2014-06-15 04:32:25 -04:00
|
|
|
void ScopeVis::feed(SampleVector::const_iterator begin, SampleVector::const_iterator end, bool positiveOnly)
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2015-07-14 19:19:39 -04:00
|
|
|
if (m_triggerChannel == 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 04:46:51 -04:00
|
|
|
while(begin < end)
|
|
|
|
{
|
|
|
|
if (m_triggerChannel == TriggerFreeRun)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
*it++ = Complex(begin->real() / 32768.0, begin->imag() / 32768.0);
|
|
|
|
++begin;
|
|
|
|
}
|
|
|
|
m_fill += count;
|
|
|
|
if(m_fill >= m_trace.size()) {
|
|
|
|
m_glScope->newTrace(m_trace, m_sampleRate);
|
|
|
|
m_fill = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 17:38:10 -04:00
|
|
|
else
|
2015-07-13 04:46:51 -04:00
|
|
|
{
|
2015-07-13 20:18:55 -04:00
|
|
|
if(m_triggerState == WaitForReset)
|
|
|
|
{
|
2015-07-13 20:56:54 -04:00
|
|
|
break;
|
2015-07-13 20:18:55 -04:00
|
|
|
}
|
2015-07-13 18:04:34 -04:00
|
|
|
if(m_triggerState == Untriggered)
|
|
|
|
{
|
|
|
|
while(begin < end)
|
|
|
|
{
|
2015-07-20 16:51:49 -04:00
|
|
|
bool trigger = triggerCondition(begin);
|
2015-07-21 16:18:17 -04:00
|
|
|
if ((trigger ^ !m_triggerPositiveEdge) && (m_tracebackCount > m_triggerPre))
|
2015-07-20 16:51:49 -04:00
|
|
|
{
|
|
|
|
if (m_armed)
|
|
|
|
{
|
2015-07-13 18:04:34 -04:00
|
|
|
m_triggerState = Triggered;
|
|
|
|
m_armed = false;
|
2015-07-14 19:19:39 -04:00
|
|
|
m_triggerPoint = begin;
|
2015-07-20 16:51:49 -04:00
|
|
|
// fill beginning of m_trace with delayed samples from the trace memory FIFO. Increment m_fill accordingly.
|
2015-07-21 16:18:17 -04:00
|
|
|
if (m_triggerPre) { // do this process only if there is a pre-trigger delay
|
|
|
|
std::copy(m_traceback.end() - m_triggerPre, m_traceback.end() - 1, m_trace.begin());
|
|
|
|
m_fill = m_triggerPre; // Increment m_fill accordingly (from 0).
|
2015-07-20 16:51:49 -04:00
|
|
|
}
|
2015-07-13 18:04:34 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-20 16:51:49 -04:00
|
|
|
else
|
|
|
|
{
|
2015-07-13 18:04:34 -04:00
|
|
|
m_armed = true;
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
++begin;
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 18:04:34 -04:00
|
|
|
if(m_triggerState == Triggered)
|
|
|
|
{
|
2014-05-18 11:52:39 -04:00
|
|
|
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) {
|
|
|
|
*it++ = Complex(begin->real() / 32768.0, begin->imag() / 32768.0);
|
|
|
|
++begin;
|
|
|
|
}
|
|
|
|
m_fill += count;
|
|
|
|
if(m_fill >= m_trace.size()) {
|
|
|
|
m_glScope->newTrace(m_trace, m_sampleRate);
|
|
|
|
m_fill = 0;
|
2015-07-13 20:18:55 -04:00
|
|
|
if (m_triggerOneShot) {
|
|
|
|
m_triggerState = WaitForReset;
|
|
|
|
} else {
|
2015-07-20 16:51:49 -04:00
|
|
|
m_tracebackCount = 0;
|
2015-07-13 20:18:55 -04:00
|
|
|
m_triggerState = Untriggered;
|
|
|
|
}
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeVis::start()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeVis::stop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-23 14:05:28 -04:00
|
|
|
bool ScopeVis::handleMessageKeep(Message* message)
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
|
|
|
if(DSPSignalNotification::match(message)) {
|
|
|
|
DSPSignalNotification* signal = (DSPSignalNotification*)message;
|
|
|
|
m_sampleRate = signal->getSampleRate();
|
2015-07-16 16:31:12 -04:00
|
|
|
/*fprintf(stderr, "ScopeVis::handleMessage : %d samples/sec, %lld Hz offset, traceSize: \n",
|
|
|
|
m_sampleRate,
|
|
|
|
signal->getFrequencyOffset(),
|
|
|
|
m_trace.size());*/
|
2014-05-18 11:52:39 -04:00
|
|
|
return true;
|
2015-07-13 04:46:51 -04:00
|
|
|
} else if(MsgConfigureScopeVis::match(message)) {
|
|
|
|
MsgConfigureScopeVis* conf = (MsgConfigureScopeVis*)message;
|
2015-07-20 16:51:49 -04:00
|
|
|
m_tracebackCount = 0;
|
2015-07-13 04:46:51 -04:00
|
|
|
m_triggerState = Untriggered;
|
|
|
|
m_triggerChannel = (TriggerChannel) conf->getTriggerChannel();
|
2015-07-13 17:38:10 -04:00
|
|
|
m_triggerLevel = conf->getTriggerLevel();
|
2015-07-13 04:46:51 -04:00
|
|
|
m_triggerPositiveEdge = conf->getTriggerPositiveEdge();
|
2015-07-21 16:18:17 -04: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 15:38:36 -04:00
|
|
|
}
|
|
|
|
uint newSize = conf->getTraceSize();
|
|
|
|
if (newSize != m_trace.size()) {
|
|
|
|
m_trace.resize(newSize);
|
|
|
|
}
|
|
|
|
if (newSize > m_traceback.size()) { // fitting the exact required space is not a requirement for the back trace
|
|
|
|
m_traceback.resize(newSize);
|
2015-07-20 16:51:49 -04:00
|
|
|
}
|
2015-07-13 17:38:10 -04:00
|
|
|
std::cerr << "ScopeVis::handleMessageKeep:"
|
|
|
|
<< " m_triggerChannel: " << m_triggerChannel
|
|
|
|
<< " m_triggerLevel: " << m_triggerLevel
|
2015-07-20 16:51:49 -04:00
|
|
|
<< " m_triggerPositiveEdge: " << (m_triggerPositiveEdge ? "edge+" : "edge-")
|
2015-07-21 16:18:17 -04:00
|
|
|
<< " m_preTrigger: " << m_triggerPre
|
2015-07-21 15:38:36 -04:00
|
|
|
<< " m_traceSize: " << m_trace.size() << std::endl;
|
2015-07-13 04:46:51 -04:00
|
|
|
return true;
|
|
|
|
/*
|
2014-05-18 11:52:39 -04:00
|
|
|
} else if(DSPConfigureScopeVis::match(message)) {
|
|
|
|
DSPConfigureScopeVis* conf = (DSPConfigureScopeVis*)message;
|
|
|
|
m_triggerState = Untriggered;
|
|
|
|
m_triggerChannel = (TriggerChannel)conf->getTriggerChannel();
|
|
|
|
m_triggerLevelHigh = conf->getTriggerLevelHigh() * 32767;
|
|
|
|
m_triggerLevelLow = conf->getTriggerLevelLow() * 32767;
|
2015-07-13 04:46:51 -04:00
|
|
|
return true;*/
|
2014-05-18 11:52:39 -04:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 14:05:28 -04:00
|
|
|
|
|
|
|
bool ScopeVis::handleMessage(Message* message)
|
|
|
|
{
|
|
|
|
bool done = handleMessageKeep(message);
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
{
|
|
|
|
message->completed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
2015-07-06 19:17:16 -04:00
|
|
|
|
|
|
|
void ScopeVis::setSampleRate(int sampleRate)
|
|
|
|
{
|
|
|
|
m_sampleRate = sampleRate;
|
|
|
|
}
|
2015-07-13 17:38:10 -04:00
|
|
|
|
|
|
|
bool ScopeVis::triggerCondition(SampleVector::const_iterator& it)
|
|
|
|
{
|
|
|
|
Complex c(it->real()/32768.0, it->imag()/32768.0);
|
2015-07-20 16:51:49 -04: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++;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:38:10 -04:00
|
|
|
if (m_triggerChannel == TriggerChannelI) {
|
|
|
|
return c.real() > m_triggerLevel;
|
|
|
|
}
|
|
|
|
else if (m_triggerChannel == TriggerChannelQ) {
|
|
|
|
return c.imag() > m_triggerLevel;
|
|
|
|
}
|
|
|
|
else if (m_triggerChannel == TriggerMagLin) {
|
|
|
|
return abs(c) > m_triggerLevel;
|
|
|
|
}
|
|
|
|
else if (m_triggerChannel == TriggerMagDb) {
|
|
|
|
Real mult = (10.0f / log2f(10.0f));
|
|
|
|
Real v = c.real() * c.real() + c.imag() * c.imag();
|
|
|
|
return mult * log2f(v) > m_triggerLevel;
|
|
|
|
}
|
|
|
|
else if (m_triggerChannel == TriggerPhase) {
|
|
|
|
return arg(c) / M_PI > m_triggerLevel;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 20:18:55 -04:00
|
|
|
|
|
|
|
void ScopeVis::setOneShot(bool oneShot)
|
|
|
|
{
|
|
|
|
m_triggerOneShot = oneShot;
|
2015-07-13 20:56:54 -04:00
|
|
|
|
|
|
|
if ((m_triggerState == WaitForReset) && !oneShot) {
|
2015-07-20 16:51:49 -04:00
|
|
|
m_tracebackCount = 0;
|
2015-07-13 20:56:54 -04:00
|
|
|
m_triggerState = Untriggered;
|
|
|
|
}
|
2015-07-13 20:18:55 -04:00
|
|
|
}
|