SignalBase: Implement A2D conversions

This commit is contained in:
Soeren Apel 2017-03-22 23:03:21 +01:00
parent a7336fb2d5
commit 12ea361676
3 changed files with 283 additions and 10 deletions

View File

@ -19,17 +19,20 @@
*/
#include "analog.hpp"
#include "analogsegment.hpp"
#include "logic.hpp"
#include "logicsegment.hpp"
#include "signalbase.hpp"
#include "signaldata.hpp"
#include "decode/row.hpp"
#include <pv/session.hpp>
#include <pv/binding/decoder.hpp>
using std::dynamic_pointer_cast;
using std::make_shared;
using std::shared_ptr;
using sigrok::Channel;
using std::tie;
namespace pv {
namespace data {
@ -38,12 +41,20 @@ const int SignalBase::ColourBGAlpha = 8*256/100;
SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) :
channel_(channel),
channel_type_(channel_type)
channel_type_(channel_type),
conversion_type_(NoConversion)
{
if (channel_)
internal_name_ = QString::fromStdString(channel_->name());
}
SignalBase::~SignalBase()
{
// Wait for the currently ongoing conversion to finish
if (conversion_thread_.joinable())
conversion_thread_.join();
}
shared_ptr<sigrok::Channel> SignalBase::channel() const
{
return channel_;
@ -114,23 +125,80 @@ QColor SignalBase::bgcolour() const
void SignalBase::set_data(shared_ptr<pv::data::SignalData> data)
{
if (data_ && channel_type_ == AnalogChannel) {
shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
disconnect(analog_data.get(), SIGNAL(samples_cleared()),
this, SLOT(on_samples_cleared()));
disconnect(analog_data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
}
data_ = data;
if (data_ && channel_type_ == AnalogChannel) {
shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
connect(analog_data.get(), SIGNAL(samples_cleared()),
this, SLOT(on_samples_cleared()));
connect(analog_data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
}
}
shared_ptr<data::Analog> SignalBase::analog_data() const
{
shared_ptr<Analog> result = nullptr;
if (channel_type_ == AnalogChannel)
return dynamic_pointer_cast<data::Analog>(data_);
else
return nullptr;
result = dynamic_pointer_cast<Analog>(data_);
return result;
}
shared_ptr<data::Logic> SignalBase::logic_data() const
{
shared_ptr<Logic> result = nullptr;
if (channel_type_ == LogicChannel)
return dynamic_pointer_cast<data::Logic>(data_);
else
return nullptr;
result = dynamic_pointer_cast<Logic>(data_);
if (((conversion_type_ == A2LConversionByTreshold) ||
(conversion_type_ == A2LConversionBySchmittTrigger)))
result = dynamic_pointer_cast<Logic>(converted_data_);
return result;
}
void SignalBase::set_conversion_type(ConversionType t)
{
if (conversion_type_ != NoConversion) {
// Wait for the currently ongoing conversion to finish
if (conversion_thread_.joinable())
conversion_thread_.join();
// Discard converted data
converted_data_.reset();
}
conversion_type_ = t;
if ((channel_type_ == AnalogChannel) &&
((conversion_type_ == A2LConversionByTreshold) ||
(conversion_type_ == A2LConversionBySchmittTrigger))) {
shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
if (analog_data->analog_segments().size() > 0) {
AnalogSegment *asegment = analog_data->analog_segments().front().get();
// Begin conversion of existing sample data
// TODO Support for multiple segments is missing
on_samples_added(asegment, 0, 0);
}
}
conversion_type_changed(t);
}
#ifdef ENABLE_DECODE
@ -156,6 +224,7 @@ void SignalBase::save_settings(QSettings &settings) const
settings.setValue("name", name());
settings.setValue("enabled", enabled());
settings.setValue("colour", colour());
settings.setValue("conversion_type", (int)conversion_type_);
}
void SignalBase::restore_settings(QSettings &settings)
@ -163,6 +232,170 @@ void SignalBase::restore_settings(QSettings &settings)
set_name(settings.value("name").toString());
set_enabled(settings.value("enabled").toBool());
set_colour(settings.value("colour").value<QColor>());
set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
}
uint8_t SignalBase::convert_a2l_threshold(float threshold, float value)
{
return (value >= threshold) ? 1 : 0;
}
uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr, float value)
{
static uint8_t state = 0;
if (value < lo_thr)
state = 0;
else if (value > hi_thr)
state = 1;
return state;
}
void SignalBase::conversion_thread_proc(QObject* segment, uint64_t start_sample,
uint64_t end_sample)
{
const uint64_t block_size = 4096;
// TODO Support for multiple segments is missing
if ((channel_type_ == AnalogChannel) &&
((conversion_type_ == A2LConversionByTreshold) ||
(conversion_type_ == A2LConversionBySchmittTrigger))) {
AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
// Create the logic data container if needed
shared_ptr<Logic> logic_data;
if (!converted_data_) {
logic_data = make_shared<Logic>(1); // Contains only one channel
converted_data_ = logic_data;
} else
logic_data = dynamic_pointer_cast<Logic>(converted_data_);
// Create the initial logic data segment if needed
if (logic_data->segments().size() == 0) {
shared_ptr<LogicSegment> lsegment =
make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
logic_data->push_segment(lsegment);
}
LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
// start_sample=end_sample=0 means we need to figure out the unprocessed range
if ((start_sample == 0) && (end_sample == 0)) {
start_sample = lsegment->get_sample_count();
end_sample = asegment->get_sample_count();
}
if (start_sample == end_sample)
return; // Nothing to do
float min_v, max_v;
tie(min_v, max_v) = asegment->get_min_max();
vector<uint8_t> lsamples;
lsamples.reserve(block_size);
uint64_t i = start_sample;
if (conversion_type_ == A2LConversionByTreshold) {
const float threshold = (min_v + max_v) * 0.5; // middle between min and max
// Convert as many sample blocks as we can
while ((end_sample - i) > block_size) {
const float* asamples = asegment->get_samples(i, i + block_size);
for (uint32_t j = 0; j < block_size; j++)
lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
lsegment->append_payload(lsamples.data(), lsamples.size());
i += block_size;
lsamples.clear();
delete[] asamples;
}
// Convert remaining samples
const float* asamples = asegment->get_samples(i, end_sample);
for (uint32_t j = 0; j < (end_sample - i); j++)
lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
lsegment->append_payload(lsamples.data(), lsamples.size());
delete[] asamples;
}
if (conversion_type_ == A2LConversionBySchmittTrigger) {
const float amplitude = max_v - min_v;
const float lo_thr = min_v + (amplitude * 0.1); // 10% above min
const float hi_thr = max_v - (amplitude * 0.1); // 10% below max
// Convert as many sample blocks as we can
while ((end_sample - i) > block_size) {
const float* asamples = asegment->get_samples(i, i + block_size);
for (uint32_t j = 0; j < block_size; j++)
lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j]));
lsegment->append_payload(lsamples.data(), lsamples.size());
i += block_size;
lsamples.clear();
delete[] asamples;
}
// Convert remaining samples
const float* asamples = asegment->get_samples(i, end_sample);
for (uint32_t j = 0; j < (end_sample - i); j++)
lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j]));
lsegment->append_payload(lsamples.data(), lsamples.size());
delete[] asamples;
}
}
}
void SignalBase::on_samples_cleared()
{
if (converted_data_)
converted_data_->clear();
}
void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
uint64_t end_sample)
{
(void)segment;
(void)start_sample;
(void)end_sample;
if (conversion_type_ != NoConversion) {
// Wait for the currently ongoing conversion to finish
if (conversion_thread_.joinable())
conversion_thread_.join();
conversion_thread_ = std::thread(
&SignalBase::conversion_thread_proc, this,
segment, start_sample, end_sample);
}
}
void SignalBase::on_capture_state_changed(int state)
{
return;
if (state == Session::Stopped) {
// Make sure that all data is converted
if ((channel_type_ == AnalogChannel) &&
((conversion_type_ == A2LConversionByTreshold) ||
(conversion_type_ == A2LConversionBySchmittTrigger))) {
shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
if (analog_data->analog_segments().size() > 0) {
// TODO Support for multiple segments is missing
AnalogSegment *asegment = analog_data->analog_segments().front().get();
if (conversion_thread_.joinable())
conversion_thread_.join();
conversion_thread_ = std::thread(
&SignalBase::conversion_thread_proc, this, asegment, 0, 0);
}
}
}
}

View File

@ -21,6 +21,8 @@
#ifndef PULSEVIEW_PV_DATA_SIGNALBASE_HPP
#define PULSEVIEW_PV_DATA_SIGNALBASE_HPP
#include <thread>
#include <QColor>
#include <QObject>
#include <QSettings>
@ -55,12 +57,18 @@ public:
MathChannel
};
enum ConversionType {
NoConversion = 0,
A2LConversionByTreshold = 1,
A2LConversionBySchmittTrigger = 2
};
private:
static const int ColourBGAlpha;
public:
SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type);
virtual ~SignalBase() {}
virtual ~SignalBase();
public:
/**
@ -134,6 +142,11 @@ public:
*/
shared_ptr<pv::data::Logic> logic_data() const;
/**
* Changes the kind of conversion performed on this channel.
*/
void set_conversion_type(ConversionType t);
#ifdef ENABLE_DECODE
bool is_decode_signal() const;
@ -146,6 +159,13 @@ public:
void restore_settings(QSettings &settings);
private:
uint8_t convert_a2l_threshold(float threshold, float value);
uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr, float value);
void conversion_thread_proc(QObject* segment, uint64_t start_sample,
uint64_t end_sample);
Q_SIGNALS:
void enabled_changed(const bool &value);
@ -153,15 +173,29 @@ Q_SIGNALS:
void colour_changed(const QColor &colour);
void conversion_type_changed(const ConversionType t);
private Q_SLOTS:
void on_samples_cleared();
void on_samples_added(QObject* segment, uint64_t start_sample,
uint64_t end_sample);
void on_capture_state_changed(int state);
private:
shared_ptr<sigrok::Channel> channel_;
ChannelType channel_type_;
shared_ptr<pv::data::SignalData> data_;
shared_ptr<pv::data::SignalData> converted_data_;
int conversion_type_;
#ifdef ENABLE_DECODE
shared_ptr<pv::data::DecoderStack> decoder_stack_;
#endif
std::thread conversion_thread_;
QString internal_name_, name_;
QColor colour_, bgcolour_;
};

View File

@ -788,6 +788,9 @@ void Session::update_signals()
all_signal_data_.insert(logic_data_);
signalbase->set_data(logic_data_);
connect(this, SIGNAL(capture_state_changed(int)),
signalbase.get(), SLOT(on_capture_state_changed(int)));
}
signal = shared_ptr<views::TraceView::Signal>(
@ -806,6 +809,9 @@ void Session::update_signals()
shared_ptr<data::Analog> data(new data::Analog());
all_signal_data_.insert(data);
signalbase->set_data(data);
connect(this, SIGNAL(capture_state_changed(int)),
signalbase.get(), SLOT(on_capture_state_changed(int)));
}
signal = shared_ptr<views::TraceView::Signal>(