Skip to content

Commit

Permalink
Clean up signess. Fix crashes in the software trigger code (glgenerat…
Browse files Browse the repository at this point in the history
…or). Remove headers where they are not used anymore (defines.h for instance)
  • Loading branch information
David Graeff committed Dec 25, 2017
1 parent db4f8ba commit 997a01e
Show file tree
Hide file tree
Showing 43 changed files with 171 additions and 176 deletions.
2 changes: 1 addition & 1 deletion openhantek/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ endif()

# include directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories(src/ src/hantekprotocol src/hantekdso src/widgets src/docks src/configdialog)
include_directories(src/ src/hantekdso src/widgets src/docks src/configdialog)

# collect sources and other files
file(GLOB_RECURSE SRC "src/*.cpp")
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/analyse/dataanalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <memory>

#include "dataanalyzerresult.h"
#include "definitions.h"
#include "dsosamples.h"
#include "utils/printutils.h"
#include "enums.h"
Expand Down
8 changes: 4 additions & 4 deletions openhantek/src/analyse/dataanalyzerresult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ DataAnalyzerResult::DataAnalyzerResult(unsigned int channelCount) { analyzedData
/// \brief Returns the analyzed data.
/// \param channel Channel, whose data should be returned.
/// \return Analyzed data as AnalyzedData struct.
const DataChannel *DataAnalyzerResult::data(int channel) const {
if (channel >= (int)this->analyzedData.size()) return 0;
const DataChannel *DataAnalyzerResult::data(unsigned channel) const {
if (channel >= this->analyzedData.size()) return 0;

return &this->analyzedData[(size_t)channel];
}

DataChannel *DataAnalyzerResult::modifyData(int channel) {
if (channel >= (int)this->analyzedData.size())
DataChannel *DataAnalyzerResult::modifyData(unsigned channel) {
if (channel >= this->analyzedData.size())
throw new std::runtime_error("If you modfiy the DataAnalyzerResult, you "
"need to set the channels first!");

Expand Down
4 changes: 2 additions & 2 deletions openhantek/src/analyse/dataanalyzerresult.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ struct DataChannel {
class DataAnalyzerResult {
public:
DataAnalyzerResult(unsigned int channelCount);
const DataChannel *data(int channel) const;
DataChannel *modifyData(int channel);
const DataChannel *data(unsigned channel) const;
DataChannel *modifyData(unsigned channel);
unsigned int sampleCount() const;
unsigned int channelCount() const;

Expand Down
1 change: 0 additions & 1 deletion openhantek/src/configdialog/DsoConfigAnalysisPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <QWidget>

#include "definitions.h"
#include "settings.h"

#include <QCheckBox>
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/configdialog/DsoConfigColorsPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <QWidget>

#include "definitions.h"
#include "settings.h"

#include <QCheckBox>
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/configdialog/DsoConfigFilesPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <QWidget>

#include "definitions.h"
#include "settings.h"

#include <QCheckBox>
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/configdialog/DsoConfigScopePage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <QWidget>

#include "definitions.h"
#include "settings.h"

#include <QCheckBox>
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/docks/HorizontalDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <QGridLayout>

#include <vector>
#include "definitions.h"
#include "settings.h"

class QLabel;
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/docks/SpectrumDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QDockWidget>
#include <QGridLayout>

#include "definitions.h"
#include "settings.h"

class QLabel;
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/docks/TriggerDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QDockWidget>
#include <QGridLayout>

#include "definitions.h"
#include "settings.h"

class QLabel;
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/docks/VoltageDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QDockWidget>
#include <QGridLayout>

#include "definitions.h"
#include "settings.h"

class QLabel;
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/docks/dockwindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "analyse/enums.h"
#include "hantekdso/enums.h"
#include "definitions.h"
#include "dockwindows.h"

void SetupDockWidget(QDockWidget *dockWindow, QWidget *dockWidget, QLayout *layout) {
Expand Down
1 change: 0 additions & 1 deletion openhantek/src/exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "exporter.h"

#include "analyse/dataanalyzerresult.h"
#include "definitions.h"
#include "glgenerator.h"
#include "settings.h"
#include "utils/dsoStrings.h"
Expand Down
180 changes: 97 additions & 83 deletions openhantek/src/glgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ GlGenerator::PrePostStartTriggerSamples GlGenerator::computeSoftwareTriggerTY(co
result->data(channel)->voltage.sample.empty())
return PrePostStartTriggerSamples(preTrigSamples, postTrigSamples, swTriggerStart);

double value;
const std::vector<double>& samples = result->data(channel)->voltage.sample;
double level = settings->voltage[channel].trigger;
unsigned int sampleCount = result->data(channel)->voltage.sample.size();
size_t sampleCount = samples.size();
double timeDisplay = settings->horizontal.timebase * 10;
double samplesDisplay = timeDisplay * settings->horizontal.samplerate;
if (samplesDisplay >= sampleCount) {
Expand All @@ -137,29 +137,29 @@ GlGenerator::PrePostStartTriggerSamples GlGenerator::computeSoftwareTriggerTY(co
"picture. Decrease sample rate"));
return PrePostStartTriggerSamples(preTrigSamples, postTrigSamples, swTriggerStart);
}
preTrigSamples = (settings->trigger.position * samplesDisplay);
postTrigSamples = sampleCount - (samplesDisplay - preTrigSamples);
preTrigSamples = (unsigned)(settings->trigger.position * samplesDisplay);
postTrigSamples = (unsigned)sampleCount - ((unsigned)samplesDisplay - preTrigSamples);

const int threshold = 7;
double prev;
bool (*opcmp)(int,int,int);
bool (*smplcmp)(int,int);
bool (*opcmp)(double,double,double);
bool (*smplcmp)(double,double);
if (settings->trigger.slope == Dso::SLOPE_POSITIVE) {
prev = INT_MAX;
opcmp = [](int value, int level, int prev) { return value > level && prev <= level;};
smplcmp = [](int sampleK, int value) { return sampleK >= value;};
opcmp = [](double value, double level, double prev) { return value > level && prev <= level;};
smplcmp = [](double sampleK, double value) { return sampleK >= value;};
} else {
prev = INT_MIN;
opcmp = [](int value, int level, int prev) { return value > level && prev <= level;};
smplcmp = [](int sampleK, int value) { return sampleK < value;};
opcmp = [](double value, double level, double prev) { return value < level && prev >= level;};
smplcmp = [](double sampleK, double value) { return sampleK < value;};
}

for (unsigned int i = preTrigSamples; i < postTrigSamples; i++) {
value = result->data(channel)->voltage.sample[i];
double value = samples[i];
if (opcmp(value, level, prev)) {
int rising = 0;
for (unsigned int k = i + 1; k < i + 11 && k < sampleCount; k++) {
if (smplcmp(result->data(channel)->voltage.sample[k], value)) { rising++; }
if (smplcmp(samples[k], value)) { rising++; }
}
if (rising > threshold) {
swTriggerStart = i;
Expand All @@ -170,13 +170,26 @@ GlGenerator::PrePostStartTriggerSamples GlGenerator::computeSoftwareTriggerTY(co
}
if (swTriggerStart == 0) {
timestampDebug(QString("Trigger not asserted. Data ignored"));
preTrigSamples = 0; // preTrigSamples may never be greater than swTriggerStart
}
return PrePostStartTriggerSamples(preTrigSamples, postTrigSamples, swTriggerStart);
}

const SampleValues& GlGenerator::useSamplesOf(int mode, unsigned channel, const DataAnalyzerResult *result) const
{
static SampleValues emptyDefault;
if (mode == Dso::CHANNELMODE_VOLTAGE) {
if (!settings->voltage[channel].used || !result->data(channel)) return emptyDefault;
return result->data(channel)->voltage;
} else {
if (!settings->spectrum[channel].used || !result->data(channel)) return emptyDefault;
return result->data(channel)->spectrum;
}
}

void GlGenerator::generateGraphs(const DataAnalyzerResult *result) {

int digitalPhosphorDepth = view->digitalPhosphor ? view->digitalPhosphorDepth : 1;
unsigned digitalPhosphorDepth = view->digitalPhosphor ? view->digitalPhosphorDepth : 1;

// Handle all digital phosphor related list manipulations
for (int mode = Dso::CHANNELMODE_VOLTAGE; mode < Dso::CHANNELMODE_COUNT; ++mode) {
Expand All @@ -197,105 +210,106 @@ void GlGenerator::generateGraphs(const DataAnalyzerResult *result) {

ready = true;

unsigned preTrigSamples;
unsigned postTrigSamples;
unsigned swTriggerStart;
unsigned preTrigSamples=0;
unsigned postTrigSamples=0;
unsigned swTriggerStart=0;
switch (settings->horizontal.format) {
case Dso::GRAPHFORMAT_TY:
std::tie(preTrigSamples, postTrigSamples, swTriggerStart) = computeSoftwareTriggerTY(result);

// Add graphs for channels
for (int mode = Dso::CHANNELMODE_VOLTAGE; mode < Dso::CHANNELMODE_COUNT; ++mode) {
for (int channel = 0; channel < (int)settings->voltage.size(); ++channel) {
DrawLinesWithHistoryPerChannel& dPerChannel = vaChannel[mode];
for (unsigned channel = 0; channel < settings->voltage.size(); ++channel) {
DrawLinesWithHistory& withHistory = dPerChannel[channel];
const SampleValues& samples = useSamplesOf(mode, channel, result);

// Check if this channel is used and available at the data analyzer
if (((mode == Dso::CHANNELMODE_VOLTAGE) ? settings->voltage[channel].used
: settings->spectrum[channel].used) &&
result->data(channel) && !result->data(channel)->voltage.sample.empty()) {
// Check if the sample count has changed
size_t sampleCount = (mode == Dso::CHANNELMODE_VOLTAGE)
? result->data(channel)->voltage.sample.size()
: result->data(channel)->spectrum.sample.size();
if (mode == Dso::CHANNELMODE_VOLTAGE) sampleCount -= (swTriggerStart - preTrigSamples);
size_t neededSize = sampleCount * 2;
if (samples.sample.empty()) {
// Delete all vector arrays
for (unsigned index = 0; index < digitalPhosphorDepth; ++index)
withHistory[index].clear();
continue;
}
// Check if the sample count has changed
size_t sampleCount = samples.sample.size();
if (sampleCount>9000) {
throw new std::runtime_error("");
}
if (mode == Dso::CHANNELMODE_VOLTAGE)
sampleCount -= (swTriggerStart - preTrigSamples);
size_t neededSize = sampleCount * 2;

#if 0
for(unsigned int index = 0; index < digitalPhosphorDepth; ++index) {
if(vaChannel[mode][channel][index].size() != neededSize)
vaChannel[mode][channel][index].clear(); // Something was changed, drop old traces
}
for(unsigned int index = 0; index < digitalPhosphorDepth; ++index) {
if(vaChannel[mode][channel][index].size() != neededSize)
vaChannel[mode][channel][index].clear(); // Something was changed, drop old traces
}
#endif

// Set size directly to avoid reallocations
vaChannel[mode][(size_t)channel].front().resize(neededSize);

// Iterator to data for direct access
std::vector<GLfloat>::iterator glIterator = vaChannel[mode][(size_t)channel].front().begin();

// What's the horizontal distance between sampling points?
double horizontalFactor;
if (mode == Dso::CHANNELMODE_VOLTAGE)
horizontalFactor = result->data(channel)->voltage.interval / settings->horizontal.timebase;
else
horizontalFactor =
result->data(channel)->spectrum.interval / settings->horizontal.frequencybase;

// Fill vector array
if (mode == Dso::CHANNELMODE_VOLTAGE) {
std::vector<double>::const_iterator dataIterator =
result->data(channel)->voltage.sample.begin();
const double gain = settings->voltage[channel].gain;
const double offset = settings->voltage[channel].offset;
const double invert = settings->voltage[channel].inverted ? -1.0 : 1.0;

std::advance(dataIterator, swTriggerStart - preTrigSamples);

for (unsigned int position = 0; position < sampleCount; ++position) {
*(glIterator++) = position * horizontalFactor - DIVS_TIME / 2;
*(glIterator++) = *(dataIterator++) / gain * invert + offset;
}
} else {
std::vector<double>::const_iterator dataIterator =
result->data(channel)->spectrum.sample.begin();
const double magnitude = settings->spectrum[channel].magnitude;
const double offset = settings->spectrum[channel].offset;

for (unsigned int position = 0; position < sampleCount; ++position) {
*(glIterator++) = position * horizontalFactor - DIVS_TIME / 2;
*(glIterator++) = *(dataIterator++) / magnitude + offset;
}
// Set size directly to avoid reallocations
withHistory.front().resize(neededSize);

// Iterator to data for direct access
DrawLines::iterator glIterator = withHistory.front().begin();

// What's the horizontal distance between sampling points?
float horizontalFactor;
if (mode == Dso::CHANNELMODE_VOLTAGE)
horizontalFactor = (float)(samples.interval / settings->horizontal.timebase);
else
horizontalFactor = (float)(samples.interval / settings->horizontal.frequencybase);

// Fill vector array
if (mode == Dso::CHANNELMODE_VOLTAGE) {
std::vector<double>::const_iterator dataIterator = samples.sample.begin();
const float gain = (float) settings->voltage[channel].gain;
const float offset = (float) settings->voltage[channel].offset;
const float invert = settings->voltage[channel].inverted ? -1.0f : 1.0f;

std::advance(dataIterator, swTriggerStart - preTrigSamples);

for (unsigned int position = 0; position < sampleCount; ++position) {
*(glIterator++) = position * horizontalFactor - DIVS_TIME / 2;
*(glIterator++) = (float)*(dataIterator++) / gain * invert + offset;
}
} else {
// Delete all vector arrays
for (unsigned index = 0; index < (unsigned)digitalPhosphorDepth; ++index)
vaChannel[mode][channel][index].clear();
std::vector<double>::const_iterator dataIterator = samples.sample.begin();
const float magnitude = (float)settings->spectrum[channel].magnitude;
const float offset = (float)settings->spectrum[channel].offset;

for (unsigned int position = 0; position < sampleCount; ++position) {
*(glIterator++) = position * horizontalFactor - DIVS_TIME / 2;
*(glIterator++) = (float)*(dataIterator++) / magnitude + offset;
}
}
}
}
break;

case Dso::GRAPHFORMAT_XY:
for (int channel = 0; channel < settings->voltage.size(); ++channel) {
for (unsigned channel = 0; channel < settings->voltage.size(); ++channel) {
// For even channel numbers check if this channel is used and this and the
// following channel are available at the data analyzer
if (channel % 2 == 0 && channel + 1 < settings->voltage.size() && settings->voltage[channel].used &&
result->data(channel) && !result->data(channel)->voltage.sample.empty() && result->data(channel + 1) &&
!result->data(channel + 1)->voltage.sample.empty()) {
// Check if the sample count has changed
const unsigned sampleCount = qMin(result->data(channel)->voltage.sample.size(),
const size_t sampleCount = std::min(result->data(channel)->voltage.sample.size(),
result->data(channel + 1)->voltage.sample.size());
const unsigned neededSize = sampleCount * 2;
for (unsigned index = 0; index < (unsigned)digitalPhosphorDepth; ++index) {
if (vaChannel[Dso::CHANNELMODE_VOLTAGE][(size_t)channel][index].size() != neededSize)
vaChannel[Dso::CHANNELMODE_VOLTAGE][(size_t)channel][index]
.clear(); // Something was changed, drop old traces
const size_t neededSize = sampleCount * 2;
DrawLinesWithHistory& withHistory = vaChannel[Dso::CHANNELMODE_VOLTAGE][(size_t)channel];
for (unsigned index = 0; index < digitalPhosphorDepth; ++index) {
if (withHistory[index].size() != neededSize)
withHistory[index].clear(); // Something was changed, drop old traces
}

// Set size directly to avoid reallocations
vaChannel[Dso::CHANNELMODE_VOLTAGE][(size_t)channel].front().resize(neededSize);
DrawLines& drawLines = withHistory.front();
drawLines.resize(neededSize);

// Iterator to data for direct access
std::vector<GLfloat>::iterator glIterator =
vaChannel[Dso::CHANNELMODE_VOLTAGE][channel].front().begin();
std::vector<GLfloat>::iterator glIterator = drawLines.begin();

// Fill vector array
unsigned int xChannel = channel;
Expand All @@ -310,8 +324,8 @@ void GlGenerator::generateGraphs(const DataAnalyzerResult *result) {
const double yInvert = settings->voltage[yChannel].inverted ? -1.0 : 1.0;

for (unsigned int position = 0; position < sampleCount; ++position) {
*(glIterator++) = *(xIterator++) / xGain * xInvert + xOffset;
*(glIterator++) = *(yIterator++) / yGain * yInvert + yOffset;
*(glIterator++) = (GLfloat)( *(xIterator++) / xGain * xInvert + xOffset);
*(glIterator++) = (GLfloat)( *(yIterator++) / yGain * yInvert + yOffset);
}
} else {
// Delete all vector arrays
Expand Down
Loading

0 comments on commit 997a01e

Please sign in to comment.