From: Soeren Apel Date: Thu, 21 Dec 2017 22:10:36 +0000 (+0100) Subject: SignalBase: Implement multi-segment conversion X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=63253d727162257aa0a3765bfb897c6826ebf611;p=pulseview.git SignalBase: Implement multi-segment conversion --- diff --git a/pv/data/signalbase.cpp b/pv/data/signalbase.cpp index e500231b..9f47a975 100644 --- a/pv/data/signalbase.cpp +++ b/pv/data/signalbase.cpp @@ -26,11 +26,14 @@ #include "signalbase.hpp" #include "signaldata.hpp" +#include + #include #include using std::dynamic_pointer_cast; using std::make_shared; +using std::out_of_range; using std::shared_ptr; using std::tie; using std::unique_lock; @@ -399,131 +402,168 @@ bool SignalBase::conversion_is_a2l() const (conversion_type_ == A2LConversionBySchmittTrigger))); } -void SignalBase::conversion_thread_proc(QObject* segment) +void SignalBase::convert_single_segment(AnalogSegment *asegment, LogicSegment *lsegment) { - // TODO Support for multiple segments is missing - uint64_t start_sample, end_sample; start_sample = end_sample = 0; - do { - if (conversion_is_a2l()) { + start_sample = lsegment->get_sample_count(); + end_sample = asegment->get_sample_count(); - AnalogSegment *asegment = qobject_cast(segment); + if (end_sample > start_sample) { + tie(min_value_, max_value_) = asegment->get_min_max(); - const shared_ptr logic_data = dynamic_pointer_cast(converted_data_); + // Create sigrok::Analog instance + float *asamples = new float[ConversionBlockSize]; + uint8_t *lsamples = new uint8_t[ConversionBlockSize]; - // Create the initial logic data segment if needed - if (logic_data->segments().size() == 0) { - shared_ptr lsegment = - make_shared(*logic_data.get(), 1, asegment->samplerate()); - logic_data->push_segment(lsegment); - } + vector > channels; + channels.push_back(channel_); - LogicSegment *lsegment = dynamic_cast(logic_data->segments().front().get()); + vector mq_flags; + const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE; + const sigrok::Unit * const unit = sigrok::Unit::VOLT; - start_sample = lsegment->get_sample_count(); - end_sample = asegment->get_sample_count(); + shared_ptr packet = + Session::sr_context->create_analog_packet(channels, + asamples, ConversionBlockSize, mq, unit, mq_flags); - if (end_sample > start_sample) { - tie(min_value_, max_value_) = asegment->get_min_max(); + shared_ptr analog = + dynamic_pointer_cast(packet->payload()); - // Create sigrok::Analog instance - float *asamples = new float[ConversionBlockSize]; - uint8_t *lsamples = new uint8_t[ConversionBlockSize]; + // Convert + uint64_t i = start_sample; - vector > channels; - channels.push_back(channel_); + if (conversion_type_ == A2LConversionByThreshold) { + const double threshold = get_conversion_thresholds()[0]; - vector mq_flags; - const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE; - const sigrok::Unit * const unit = sigrok::Unit::VOLT; + // Convert as many sample blocks as we can + while ((end_sample - i) > ConversionBlockSize) { + asegment->get_samples(i, i + ConversionBlockSize, asamples); - shared_ptr packet = - Session::sr_context->create_analog_packet(channels, - asamples, ConversionBlockSize, mq, unit, mq_flags); + shared_ptr logic = + analog->get_logic_via_threshold(threshold, lsamples); - shared_ptr analog = - dynamic_pointer_cast(packet->payload()); + lsegment->append_payload(logic->data_pointer(), logic->data_length()); - // Convert - uint64_t i = start_sample; + samples_added(lsegment, i, i + ConversionBlockSize); + i += ConversionBlockSize; + } - if (conversion_type_ == A2LConversionByThreshold) { - const double threshold = get_conversion_thresholds()[0]; + // Re-create sigrok::Analog and convert remaining samples + packet = Session::sr_context->create_analog_packet(channels, + asamples, end_sample - i, mq, unit, mq_flags); - // Convert as many sample blocks as we can - while ((end_sample - i) > ConversionBlockSize) { - asegment->get_samples(i, i + ConversionBlockSize, asamples); + analog = dynamic_pointer_cast(packet->payload()); - shared_ptr logic = - analog->get_logic_via_threshold(threshold, lsamples); + asegment->get_samples(i, end_sample, asamples); + shared_ptr logic = + analog->get_logic_via_threshold(threshold, lsamples); + lsegment->append_payload(logic->data_pointer(), logic->data_length()); + samples_added(lsegment, i, end_sample); + } - lsegment->append_payload(logic->data_pointer(), logic->data_length()); + if (conversion_type_ == A2LConversionBySchmittTrigger) { + const vector thresholds = get_conversion_thresholds(); + const double lo_thr = thresholds[0]; + const double hi_thr = thresholds[1]; - samples_added(lsegment, i, i + ConversionBlockSize); - i += ConversionBlockSize; - } + uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0 - // Re-create sigrok::Analog and convert remaining samples - packet = Session::sr_context->create_analog_packet(channels, - asamples, end_sample - i, mq, unit, mq_flags); + // Convert as many sample blocks as we can + while ((end_sample - i) > ConversionBlockSize) { + asegment->get_samples(i, i + ConversionBlockSize, asamples); - analog = dynamic_pointer_cast(packet->payload()); + shared_ptr logic = + analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr, + &state, lsamples); - asegment->get_samples(i, end_sample, asamples); - shared_ptr logic = - analog->get_logic_via_threshold(threshold, lsamples); - lsegment->append_payload(logic->data_pointer(), logic->data_length()); - samples_added(lsegment, i, end_sample); - } + lsegment->append_payload(logic->data_pointer(), logic->data_length()); - if (conversion_type_ == A2LConversionBySchmittTrigger) { - const vector thresholds = get_conversion_thresholds(); - const double lo_thr = thresholds[0]; - const double hi_thr = thresholds[1]; + samples_added(lsegment, i, i + ConversionBlockSize); + i += ConversionBlockSize; + } - uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0 + // Re-create sigrok::Analog and convert remaining samples + packet = Session::sr_context->create_analog_packet(channels, + asamples, end_sample - i, mq, unit, mq_flags); - // Convert as many sample blocks as we can - while ((end_sample - i) > ConversionBlockSize) { - asegment->get_samples(i, i + ConversionBlockSize, asamples); + analog = dynamic_pointer_cast(packet->payload()); - shared_ptr logic = - analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr, - &state, lsamples); + asegment->get_samples(i, end_sample, asamples); + shared_ptr logic = + analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr, + &state, lsamples); + lsegment->append_payload(logic->data_pointer(), logic->data_length()); + samples_added(lsegment, i, end_sample); + } - lsegment->append_payload(logic->data_pointer(), logic->data_length()); + // If acquisition is ongoing, start-/endsample may have changed + end_sample = asegment->get_sample_count(); - samples_added(lsegment, i, i + ConversionBlockSize); - i += ConversionBlockSize; - } + delete[] lsamples; + delete[] asamples; + } +} - // Re-create sigrok::Analog and convert remaining samples - packet = Session::sr_context->create_analog_packet(channels, - asamples, end_sample - i, mq, unit, mq_flags); +void SignalBase::conversion_thread_proc() +{ + shared_ptr analog_data; - analog = dynamic_pointer_cast(packet->payload()); + if (conversion_is_a2l()) { + analog_data = dynamic_pointer_cast(data_); + + if (analog_data->analog_segments().size() == 0) + return; + + } else + // Currently, we only handle A2L conversions + return; - asegment->get_samples(i, end_sample, asamples); - shared_ptr logic = - analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr, - &state, lsamples); - lsegment->append_payload(logic->data_pointer(), logic->data_length()); - samples_added(lsegment, i, end_sample); - } + uint32_t segment_id = 0; - // If acquisition is ongoing, start-/endsample may have changed - end_sample = asegment->get_sample_count(); + AnalogSegment *asegment = analog_data->analog_segments().front().get(); + assert(asegment); - delete[] lsamples; - delete[] asamples; + const shared_ptr logic_data = dynamic_pointer_cast(converted_data_); + assert(logic_data); + + // Create the initial logic data segment if needed + if (logic_data->logic_segments().size() == 0) { + shared_ptr new_segment = + make_shared(*logic_data.get(), 1, asegment->samplerate()); + logic_data->push_segment(new_segment); + } + + LogicSegment *lsegment = logic_data->logic_segments().front().get(); + assert(lsegment); + + do { + convert_single_segment(asegment, lsegment); + + if (analog_data->analog_segments().size() > logic_data->logic_segments().size()) { + // There are more segments to process + segment_id++; + + try { + asegment = analog_data->analog_segments().at(segment_id).get(); + } catch (out_of_range) { + qDebug() << "Conversion error for" << name() << ": no analog segment" \ + << segment_id << ", segments size is" << analog_data->analog_segments().size(); + return; } - } - if (!conversion_interrupt_ && (start_sample == end_sample)) { - unique_lock input_lock(conversion_input_mutex_); - conversion_input_cond_.wait(input_lock); + shared_ptr new_segment = + make_shared(*logic_data.get(), 1, asegment->samplerate()); + logic_data->push_segment(new_segment); + + lsegment = logic_data->logic_segments().back().get(); + } else { + // No more segments to process, wait for data or interrupt + if (!conversion_interrupt_) { + unique_lock input_lock(conversion_input_mutex_); + conversion_input_cond_.wait(input_lock); + } } } while (!conversion_interrupt_); } @@ -541,18 +581,9 @@ void SignalBase::start_conversion(bool delayed_start) converted_data_->clear(); samples_cleared(); - if (conversion_is_a2l()) { - shared_ptr analog_data = dynamic_pointer_cast(data_); - - if (analog_data->analog_segments().size() > 0) { - // TODO Support for multiple segments is missing - AnalogSegment *asegment = analog_data->analog_segments().front().get(); - - conversion_interrupt_ = false; - conversion_thread_ = std::thread( - &SignalBase::conversion_thread_proc, this, asegment); - } - } + conversion_interrupt_ = false; + conversion_thread_ = std::thread( + &SignalBase::conversion_thread_proc, this); } void SignalBase::stop_conversion() diff --git a/pv/data/signalbase.hpp b/pv/data/signalbase.hpp index 865f2551..977a9cd6 100644 --- a/pv/data/signalbase.hpp +++ b/pv/data/signalbase.hpp @@ -51,8 +51,10 @@ namespace pv { namespace data { class Analog; +class AnalogSegment; class DecoderStack; class Logic; +class LogicSegment; class SignalData; class SignalBase : public QObject @@ -266,7 +268,9 @@ private: uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr, float value, uint8_t &state); - void conversion_thread_proc(QObject* segment); + void convert_single_segment(pv::data::AnalogSegment *asegment, + pv::data::LogicSegment *lsegment); + void conversion_thread_proc(); void stop_conversion();