]> sigrok.org Git - pulseview.git/commitdiff
SignalBase: Implement multi-segment conversion
authorSoeren Apel <redacted>
Thu, 21 Dec 2017 22:10:36 +0000 (23:10 +0100)
committerSoeren Apel <redacted>
Fri, 5 Jan 2018 21:50:26 +0000 (22:50 +0100)
pv/data/signalbase.cpp
pv/data/signalbase.hpp

index e500231b6e0404aeb75fbf5d603276e443bda1b5..9f47a9755eee6fc6ec085e3a831fb51062971183 100644 (file)
 #include "signalbase.hpp"
 #include "signaldata.hpp"
 
+#include <QDebug>
+
 #include <pv/binding/decoder.hpp>
 #include <pv/session.hpp>
 
 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<AnalogSegment*>(segment);
+       if (end_sample > start_sample) {
+               tie(min_value_, max_value_) = asegment->get_min_max();
 
-                       const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(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<LogicSegment> lsegment =
-                                       make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
-                               logic_data->push_segment(lsegment);
-                       }
+               vector<shared_ptr<sigrok::Channel> > channels;
+               channels.push_back(channel_);
 
-                       LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
+               vector<const sigrok::QuantityFlag*> 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<sigrok::Packet> 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<sigrok::Analog> analog =
+                       dynamic_pointer_cast<sigrok::Analog>(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<shared_ptr<sigrok::Channel> > channels;
-                               channels.push_back(channel_);
+               if (conversion_type_ == A2LConversionByThreshold) {
+                       const double threshold = get_conversion_thresholds()[0];
 
-                               vector<const sigrok::QuantityFlag*> 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<sigrok::Packet> packet =
-                                       Session::sr_context->create_analog_packet(channels,
-                                       asamples, ConversionBlockSize, mq, unit, mq_flags);
+                               shared_ptr<sigrok::Logic> logic =
+                                       analog->get_logic_via_threshold(threshold, lsamples);
 
-                               shared_ptr<sigrok::Analog> analog =
-                                       dynamic_pointer_cast<sigrok::Analog>(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<sigrok::Analog>(packet->payload());
 
-                                               shared_ptr<sigrok::Logic> logic =
-                                                       analog->get_logic_via_threshold(threshold, lsamples);
+                       asegment->get_samples(i, end_sample, asamples);
+                       shared_ptr<sigrok::Logic> 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<double> 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<sigrok::Analog>(packet->payload());
+                               shared_ptr<sigrok::Logic> logic =
+                                       analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
+                                               &state, lsamples);
 
-                                       asegment->get_samples(i, end_sample, asamples);
-                                       shared_ptr<sigrok::Logic> 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<double> 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<sigrok::Analog>(packet->payload());
 
-                                               shared_ptr<sigrok::Logic> logic =
-                                                       analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
-                                                               &state, lsamples);
+                       asegment->get_samples(i, end_sample, asamples);
+                       shared_ptr<sigrok::Logic> 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> analog_data;
 
-                                       analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
+       if (conversion_is_a2l()) {
+               analog_data = dynamic_pointer_cast<Analog>(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<sigrok::Logic> 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> logic_data = dynamic_pointer_cast<Logic>(converted_data_);
+       assert(logic_data);
+
+       // Create the initial logic data segment if needed
+       if (logic_data->logic_segments().size() == 0) {
+               shared_ptr<LogicSegment> new_segment =
+                       make_shared<LogicSegment>(*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<mutex> input_lock(conversion_input_mutex_);
-                       conversion_input_cond_.wait(input_lock);
+                       shared_ptr<LogicSegment> new_segment =
+                               make_shared<LogicSegment>(*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<mutex> 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> 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();
-
-                       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()
index 865f2551662fed2ab137781752c9064191cb698d..977a9cd68121f743e7e7070d67957e9d7e07081e 100644 (file)
@@ -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();