2017-10-21 06:11:49 +01:00
|
|
|
#include "isobuffer.h"
|
2019-01-28 00:14:04 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2017-10-21 06:11:49 +01:00
|
|
|
#include "isodriver.h"
|
2017-11-28 00:17:11 +00:00
|
|
|
#include "uartstyledecoder.h"
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-01-28 00:14:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
constexpr char const* fileHeaderFormat =
|
|
|
|
"EspoTek Labrador DAQ V1.0 Output File\n"
|
|
|
|
"Averaging = %d\n"
|
|
|
|
"Mode = %d\n";
|
|
|
|
|
|
|
|
constexpr auto kSamplesSeekingCap = 20;
|
|
|
|
|
|
|
|
#ifdef INVERT_MM
|
|
|
|
constexpr auto fX0Comp = std::greater<int> {};
|
|
|
|
constexpr auto fX1X2Comp = std::less<int> {};
|
|
|
|
#else
|
|
|
|
constexpr auto fX0Comp = std::less<int> {};
|
|
|
|
constexpr auto fX1X2Comp = std::greater<int> {};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
constexpr auto kTopMultimeter = 2048;
|
|
|
|
constexpr double kTriggerSensitivityMultiplier = 4;
|
2019-01-23 05:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isoBuffer::isoBuffer(QWidget* parent, int bufferLen, isoDriver* caller, unsigned char channel_value)
|
2019-02-12 07:32:16 +00:00
|
|
|
: QWidget(parent)
|
|
|
|
, m_channel(channel_value)
|
2019-02-15 10:44:02 +00:00
|
|
|
, m_bufferPtr(std::make_unique<short[]>(bufferLen*2))
|
2019-02-12 07:32:16 +00:00
|
|
|
, m_bufferLen(bufferLen)
|
|
|
|
, m_samplesPerSecond(bufferLen/21.0/375*VALID_DATA_PER_375)
|
|
|
|
, m_sampleRate_bit(bufferLen/21.0/375*VALID_DATA_PER_375*8)
|
|
|
|
, m_virtualParent(caller)
|
2019-01-23 05:13:40 +00:00
|
|
|
{
|
2019-02-15 10:44:02 +00:00
|
|
|
m_buffer = m_bufferPtr.get();
|
2019-01-23 05:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void isoBuffer::insertIntoBuffer(short item)
|
2017-10-21 06:11:49 +01:00
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
m_buffer[m_back] = item;
|
|
|
|
m_buffer[m_back+m_bufferLen] = item;
|
2019-01-28 02:39:48 +00:00
|
|
|
m_back++;
|
|
|
|
m_insertedCount++;
|
2019-01-23 05:13:40 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
if (m_insertedCount > m_bufferLen)
|
|
|
|
{
|
|
|
|
m_insertedCount = m_bufferLen;
|
|
|
|
}
|
2019-01-23 05:13:40 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
if (m_back == m_bufferLen)
|
|
|
|
{
|
|
|
|
m_back = 0;
|
|
|
|
}
|
2019-01-28 02:39:48 +00:00
|
|
|
|
|
|
|
checkTriggered();
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
2019-01-23 05:13:40 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
short isoBuffer::bufferAt(uint32_t idx) const
|
2017-10-21 06:11:49 +01:00
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
if (idx > m_insertedCount)
|
2019-02-17 06:14:20 +00:00
|
|
|
qFatal("isoBuffer::bufferAt: invalid query, idx = %lu, m_insertedCount = %lu", idx, m_insertedCount);
|
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
return m_buffer[(m_back-1) + m_bufferLen - idx];
|
2019-01-23 05:13:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
template<typename T, typename Function>
|
|
|
|
void isoBuffer::writeBuffer(T* data, int len, int TOP, Function transform)
|
2019-01-23 05:13:40 +00:00
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
insertIntoBuffer(transform(data[i]));
|
|
|
|
}
|
2019-01-28 00:14:04 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// Output to CSV
|
|
|
|
if (m_fileIOEnabled)
|
|
|
|
{
|
|
|
|
bool isUsingAC = m_channel == 1
|
|
|
|
? m_virtualParent->AC_CH1
|
|
|
|
: m_virtualParent->AC_CH2;
|
2019-01-28 00:14:04 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
for (int i = 0; i < len && m_fileIOEnabled; i++)
|
|
|
|
{
|
|
|
|
double convertedSample = sampleConvert(data[i], TOP, isUsingAC);
|
2019-01-23 05:13:40 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
maybeOutputSampleToFile(convertedSample);
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isoBuffer::writeBuffer_char(char* data, int len)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
writeBuffer(data, len, 128, [](char item) -> short {return item;});
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isoBuffer::writeBuffer_short(short* data, int len)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
writeBuffer(data, len, 2048, [](short item) -> short {return item >> 4;});
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-01-28 00:14:04 +00:00
|
|
|
std::unique_ptr<short[]> isoBuffer::readBuffer(double sampleWindow, int numSamples, bool singleBit, double delayOffset)
|
2017-10-21 06:11:49 +01:00
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
/*
|
|
|
|
* The expected behavior is to run backwards over the buffer with a stride
|
|
|
|
* of timeBetweenSamples steps, and push the touched elements into readData.
|
|
|
|
* If more elements are requested than how many are stored (1), the buffer
|
|
|
|
* will be populated only partially. Modifying this function to return null
|
|
|
|
* or a zero-filled buffer instead should be simple enough.
|
|
|
|
*
|
|
|
|
* (1) m_insertedCount < (delayOffset + sampleWindow) * m_samplesPerSecond
|
|
|
|
*/
|
|
|
|
const double timeBetweenSamples = sampleWindow * m_samplesPerSecond / numSamples;
|
|
|
|
const int delaySamples = delayOffset * m_samplesPerSecond;
|
|
|
|
|
|
|
|
std::unique_ptr<short[]> readData = std::make_unique<short[]>(numSamples);
|
|
|
|
|
|
|
|
std::fill (readData.get(), readData.get() + numSamples, short(0));
|
|
|
|
|
|
|
|
double itr = delaySamples;
|
|
|
|
for (int i = 0; i < numSamples && itr < m_insertedCount; i++)
|
|
|
|
{
|
|
|
|
readData[i] = bufferAt(int(itr));
|
|
|
|
|
|
|
|
if (singleBit)
|
|
|
|
{
|
|
|
|
int subIdx = 8*(-itr-floor(-itr));
|
|
|
|
readData[i] &= (1 << subIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
itr += timeBetweenSamples;
|
|
|
|
}
|
|
|
|
|
|
|
|
return readData;
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isoBuffer::clearBuffer()
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
for (uint32_t i = 0; i < m_bufferLen; i++)
|
|
|
|
{
|
|
|
|
m_buffer[i] = 0;
|
|
|
|
m_buffer[i + m_bufferLen] = 0;
|
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
m_back = 0;
|
|
|
|
m_insertedCount = 0;
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isoBuffer::gainBuffer(int gain_log)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
qDebug() << "Buffer shifted by" << gain_log;
|
|
|
|
for (uint32_t i = 0; i < m_bufferLen; i++)
|
|
|
|
{
|
|
|
|
if (gain_log < 0)
|
|
|
|
{
|
|
|
|
m_buffer[i] <<= -gain_log;
|
|
|
|
m_buffer[i+m_bufferLen] <<= -gain_log;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_buffer[i] >>= gain_log;
|
|
|
|
m_buffer[i+m_bufferLen] >>= gain_log;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void isoBuffer::outputSampleToFile(double averageSample)
|
|
|
|
{
|
|
|
|
char numStr[32];
|
|
|
|
sprintf(numStr,"%7.5f, ", averageSample);
|
|
|
|
|
|
|
|
m_currentFile->write(numStr);
|
|
|
|
m_currentColumn++;
|
|
|
|
|
|
|
|
if (m_currentColumn == COLUMN_BREAK)
|
|
|
|
{
|
|
|
|
m_currentFile->write("\n");
|
|
|
|
m_currentColumn = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void isoBuffer::maybeOutputSampleToFile(double convertedSample)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This function adds a sample to an accumulator and bumps a sample count.
|
|
|
|
* After the sample count hits some threshold the samples are averaged
|
|
|
|
* and the average is written to a file.
|
|
|
|
* If this makes us hit the max. file size, then fileIO is disabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
m_fileIO_sampleAccumulator += convertedSample;
|
|
|
|
m_fileIO_sampleCount++;
|
|
|
|
|
|
|
|
if (m_fileIO_sampleCount == m_fileIO_sampleCountPerWrite)
|
|
|
|
{
|
|
|
|
double averageSample = m_fileIO_sampleAccumulator / m_fileIO_sampleCount;
|
|
|
|
outputSampleToFile(averageSample);
|
|
|
|
|
|
|
|
// Reset the accumulator and sample count for next data point.
|
|
|
|
m_fileIO_sampleAccumulator = 0;
|
|
|
|
m_fileIO_sampleCount = 0;
|
|
|
|
|
|
|
|
// value of 0 means "no limit", meaning we must skip the check by returning.
|
|
|
|
if (m_fileIO_maxFileSize == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 7 chars(number) + 1 char(comma) + 1 char(space) = 9 bytes/sample.
|
|
|
|
m_fileIO_numBytesWritten += 9;
|
|
|
|
if (m_fileIO_numBytesWritten >= m_fileIO_maxFileSize)
|
|
|
|
{
|
|
|
|
m_fileIOEnabled = false; // Just in case signalling fails.
|
|
|
|
fileIOinternalDisable();
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
void isoBuffer::enableFileIO(QFile* file, int samplesToAverage, qulonglong max_file_size)
|
2017-10-21 06:11:49 +01:00
|
|
|
{
|
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// Open the file
|
|
|
|
file->open(QIODevice::WriteOnly);
|
|
|
|
m_currentFile = file;
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// Add the header
|
|
|
|
char headerLine[256];
|
|
|
|
sprintf(headerLine, fileHeaderFormat, samplesToAverage, m_virtualParent->driver->deviceMode);
|
|
|
|
m_currentFile->write(headerLine);
|
2017-12-27 01:44:49 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// Set up the isoBuffer for DAQ
|
|
|
|
m_fileIO_maxFileSize = max_file_size;
|
|
|
|
m_fileIO_sampleCountPerWrite = samplesToAverage;
|
|
|
|
m_fileIO_sampleCount = 0;
|
|
|
|
m_fileIO_sampleAccumulator = 0;
|
|
|
|
m_fileIO_numBytesWritten = 0;
|
2017-12-24 03:08:27 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// Enable DAQ
|
|
|
|
m_fileIOEnabled = true;
|
2017-12-27 01:44:49 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
qDebug("File IO enabled, averaging %d samples, max file size %lluMB", samplesToAverage, max_file_size/1000000);
|
|
|
|
qDebug() << max_file_size;
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
void isoBuffer::disableFileIO()
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
m_fileIOEnabled = false;
|
|
|
|
m_currentColumn = 0;
|
|
|
|
m_currentFile->close();
|
|
|
|
return;
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
double isoBuffer::sampleConvert(short sample, int TOP, bool AC) const
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
double scope_gain = (double)(m_virtualParent->driver->scopeGain);
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
double voltageLevel = (sample * (vcc/2)) / (m_frontendGain*scope_gain*TOP);
|
|
|
|
if (m_virtualParent->driver->deviceMode != 7) voltageLevel += m_voltage_ref;
|
2019-01-23 05:13:40 +00:00
|
|
|
#ifdef INVERT_MM
|
2019-02-12 07:32:16 +00:00
|
|
|
if (m_virtualParent->driver->deviceMode == 7) voltageLevel *= -1;
|
2019-01-23 05:13:40 +00:00
|
|
|
#endif
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
if (AC)
|
|
|
|
{
|
|
|
|
// This is old (1 frame in past) value and might not be good for signals with
|
|
|
|
// large variations in DC level (although the cap should filter that anyway)??
|
|
|
|
voltageLevel -= m_virtualParent->currentVmean;
|
|
|
|
}
|
|
|
|
return voltageLevel;
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
short isoBuffer::inverseSampleConvert(double voltageLevel, int TOP, bool AC) const
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
double scope_gain = m_virtualParent->driver->scopeGain;
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
if (AC)
|
|
|
|
{
|
|
|
|
// This is old (1 frame in past) value and might not be good for signals with
|
|
|
|
// large variations in DC level (although the cap should filter that anyway)??
|
|
|
|
voltageLevel += m_virtualParent->currentVmean;
|
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
|
|
|
|
#ifdef INVERT_MM
|
2019-02-12 07:32:16 +00:00
|
|
|
if (m_virtualParent->driver->deviceMode == 7) voltageLevel *= -1;
|
2017-10-21 06:11:49 +01:00
|
|
|
#endif
|
2019-02-12 07:32:16 +00:00
|
|
|
if (m_virtualParent->driver->deviceMode != 7) voltageLevel -= m_voltage_ref;
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// voltageLevel = (sample * (vcc/2)) / (frontendGain*scope_gain*TOP);
|
|
|
|
short sample = (voltageLevel * (m_frontendGain*scope_gain*TOP))/(vcc/2);
|
|
|
|
return sample;
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// For capacitance measurement.
|
|
|
|
// x0, x1 and x2 are all various time points used to find the RC coefficient.
|
2019-01-23 05:13:40 +00:00
|
|
|
template<typename Function>
|
|
|
|
int isoBuffer::capSample(int offset, int target, double seconds, double value, Function comp)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
int samples = seconds * m_samplesPerSecond;
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-17 08:20:03 +00:00
|
|
|
if (static_cast<int32_t>(m_back) < (samples + offset))
|
|
|
|
return -1;
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
short sample = inverseSampleConvert(value, 2048, 0);
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
int found = 0;
|
|
|
|
for (int i = samples + offset; i--;)
|
|
|
|
{
|
|
|
|
short currentSample = bufferAt(i);
|
|
|
|
if (comp(currentSample, sample))
|
|
|
|
found = found + 1;
|
|
|
|
else
|
|
|
|
found = std::max(0, found-1);
|
2019-01-23 05:13:40 +00:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
if (found > target)
|
|
|
|
return samples - i;
|
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
return -1;
|
2017-10-21 06:11:49 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
int isoBuffer::cap_x0fromLast(double seconds, double vbot)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
return capSample(0, kSamplesSeekingCap, seconds, vbot, fX0Comp);
|
2017-11-28 00:17:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
int isoBuffer::cap_x1fromLast(double seconds, int x0, double vbot)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
return capSample(-x0, kSamplesSeekingCap, seconds, vbot, fX1X2Comp);
|
2019-01-23 05:13:40 +00:00
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
int isoBuffer::cap_x2fromLast(double seconds, int x1, double vtop)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
return capSample(-x1, kSamplesSeekingCap, seconds, vtop, fX1X2Comp);
|
2019-01-23 05:13:40 +00:00
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-01-23 05:13:40 +00:00
|
|
|
void isoBuffer::serialManage(double baudRate, UartParity parity)
|
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
if (m_decoder == NULL)
|
|
|
|
{
|
2019-03-18 07:14:49 +00:00
|
|
|
m_decoder = new uartStyleDecoder(baudRate, this);
|
2019-02-12 07:32:16 +00:00
|
|
|
|
|
|
|
connect(m_decoder, &uartStyleDecoder::wireDisconnected,
|
|
|
|
m_virtualParent, &isoDriver::serialNeedsDisabling);
|
|
|
|
}
|
|
|
|
if (!m_isDecoding)
|
|
|
|
{
|
2019-03-18 07:14:49 +00:00
|
|
|
m_decoder->m_updateTimer.start(CONSOLE_UPDATE_TIMER_PERIOD);
|
2019-02-12 07:32:16 +00:00
|
|
|
m_isDecoding = true;
|
|
|
|
}
|
2019-03-18 07:14:49 +00:00
|
|
|
|
|
|
|
m_decoder->m_baudRate = baudRate;
|
2019-02-12 07:32:16 +00:00
|
|
|
m_decoder->setParityMode(parity);
|
2019-03-18 07:14:49 +00:00
|
|
|
m_decoder->serialDecode();
|
2019-01-23 05:13:40 +00:00
|
|
|
}
|
2017-10-21 06:11:49 +01:00
|
|
|
|
2019-01-28 02:39:48 +00:00
|
|
|
void isoBuffer::setTriggerType(TriggerType newType)
|
|
|
|
{
|
|
|
|
qDebug() << "Trigger Type: " << (uint8_t)newType;
|
|
|
|
m_triggerType = newType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void isoBuffer::setTriggerLevel(double voltageLevel, uint16_t top, bool acCoupled)
|
|
|
|
{
|
|
|
|
m_triggerLevel = inverseSampleConvert(voltageLevel, top, acCoupled);
|
|
|
|
m_triggerSensitivity = static_cast<short>(1 + abs(voltageLevel * kTriggerSensitivityMultiplier * static_cast<double>(top) / 128.));
|
|
|
|
qDebug() << "Trigger Level: " << m_triggerLevel;
|
|
|
|
qDebug() << "Trigger sensitivity:" << m_triggerSensitivity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Clear trigger
|
|
|
|
// FIXME: AC changes will not be reflected here
|
|
|
|
void isoBuffer::checkTriggered()
|
|
|
|
{
|
|
|
|
if (m_triggerType == TriggerType::Disabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((bufferAt(0) >= (m_triggerLevel + m_triggerSensitivity)) && (m_triggerSeekState == TriggerSeekState::BelowTriggerLevel))
|
|
|
|
{
|
|
|
|
// Rising Edge
|
|
|
|
m_triggerSeekState = TriggerSeekState::AboveTriggerLevel;
|
2019-01-28 09:10:29 +00:00
|
|
|
if (m_triggerType == TriggerType::Rising)
|
|
|
|
m_triggerPositionList.push_back(m_back - 1);
|
2019-01-28 02:39:48 +00:00
|
|
|
}
|
|
|
|
else if ((bufferAt(0) < (m_triggerLevel - m_triggerSensitivity)) && (m_triggerSeekState == TriggerSeekState::AboveTriggerLevel))
|
|
|
|
{
|
|
|
|
// Falling Edge
|
|
|
|
m_triggerSeekState = TriggerSeekState::BelowTriggerLevel;
|
2019-01-28 09:10:29 +00:00
|
|
|
if (m_triggerType == TriggerType::Falling)
|
|
|
|
m_triggerPositionList.push_back(m_back - 1);
|
2019-01-28 02:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-28 09:10:29 +00:00
|
|
|
|
|
|
|
double isoBuffer::getDelayedTriggerPoint(double delay)
|
|
|
|
{
|
2019-01-28 23:08:09 +00:00
|
|
|
if (m_triggerPositionList.size() == 0)
|
2019-01-28 22:45:45 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-01-28 09:10:29 +00:00
|
|
|
const uint32_t delaySamples = delay * m_samplesPerSecond;
|
|
|
|
|
|
|
|
auto isValid = [=](uint32_t index)->bool
|
|
|
|
{
|
|
|
|
if (m_back > delaySamples)
|
2019-02-12 07:32:16 +00:00
|
|
|
return (index < m_back - delaySamples) || (index >= m_back);
|
2019-01-28 09:10:29 +00:00
|
|
|
else
|
2019-02-12 07:32:16 +00:00
|
|
|
return (index < m_bufferLen + m_back - delaySamples) && (index >= m_back);
|
2019-01-28 09:10:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto getDelay = [=](uint32_t index)->double
|
|
|
|
{
|
|
|
|
if (m_back > index)
|
|
|
|
return (m_back - index) / static_cast<double>(m_samplesPerSecond);
|
|
|
|
else
|
2019-02-12 07:32:16 +00:00
|
|
|
return (m_bufferLen + (m_back-1) - index) / static_cast<double>(m_samplesPerSecond);
|
2019-01-28 09:10:29 +00:00
|
|
|
};
|
|
|
|
|
2019-02-12 07:32:16 +00:00
|
|
|
// Like std::find_if but returns the last element matching the predicate instead of the first one
|
|
|
|
// TODO: Move this elsewhere (maybe a utils / algorithms file??)
|
|
|
|
// requires first and last to be Bidirectional iters, and form a valid range
|
|
|
|
// requires p to be a valid unaryPredicate
|
|
|
|
// Full signature would be:
|
|
|
|
// template<typename It, typename Predicate>
|
|
|
|
// It find_last_if(It begin, It end, Predicate p)
|
|
|
|
auto find_last_if = [](auto begin, auto end, auto p)
|
2019-01-28 09:10:29 +00:00
|
|
|
{
|
2019-02-12 07:32:16 +00:00
|
|
|
using It = decltype(begin); // TODO: remove this line once this is a proper function
|
|
|
|
std::reverse_iterator<It> rlast(begin), rfirst(end);
|
|
|
|
auto found = std::find_if(rfirst, rlast, p);
|
|
|
|
return found == rlast
|
|
|
|
? end
|
|
|
|
: std::prev(found.base());
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it = find_last_if(m_triggerPositionList.begin(), m_triggerPositionList.end(), isValid);
|
|
|
|
if (it != m_triggerPositionList.end())
|
|
|
|
{
|
|
|
|
// NOTE: vector::erase does not remove the element pointed to by the second iterator.
|
|
|
|
m_triggerPositionList.erase(m_triggerPositionList.begin(), it);
|
|
|
|
return getDelay(m_triggerPositionList[0]);
|
2019-01-28 09:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|