]> sigrok.org Git - pulseview.git/blob - pv/data/signalbase.cpp
SignalBase: Don't check for signal types, check based on data
[pulseview.git] / pv / data / signalbase.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "analog.hpp"
22 #include "analogsegment.hpp"
23 #include "decode/row.hpp"
24 #include "logic.hpp"
25 #include "logicsegment.hpp"
26 #include "signalbase.hpp"
27 #include "signaldata.hpp"
28
29 #include <QDebug>
30
31 #include <pv/binding/decoder.hpp>
32 #include <pv/session.hpp>
33
34 using std::dynamic_pointer_cast;
35 using std::make_shared;
36 using std::out_of_range;
37 using std::shared_ptr;
38 using std::tie;
39 using std::unique_lock;
40
41 namespace pv {
42 namespace data {
43
44 const int SignalBase::ColorBGAlpha = 8 * 256 / 100;
45 const uint64_t SignalBase::ConversionBlockSize = 4096;
46 const uint32_t SignalBase::ConversionDelay = 1000;  // 1 second
47
48
49 SignalGroup::SignalGroup(const QString& name)
50 {
51         name_ = name;
52 }
53
54 void SignalGroup::append_signal(shared_ptr<SignalBase> signal)
55 {
56         if (!signal)
57                 return;
58
59         signals_.push_back(signal);
60         signal->set_group(this);
61 }
62
63 void SignalGroup::remove_signal(shared_ptr<SignalBase> signal)
64 {
65         if (!signal)
66                 return;
67
68         signals_.erase(std::remove_if(signals_.begin(), signals_.end(),
69                 [&](shared_ptr<SignalBase> s) { return s == signal; }),
70                 signals_.end());
71 }
72
73 deque<shared_ptr<SignalBase>> SignalGroup::signals() const
74 {
75         return signals_;
76 }
77
78 void SignalGroup::clear()
79 {
80         for (shared_ptr<SignalBase> sb : signals_)
81                 sb->set_group(nullptr);
82
83         signals_.clear();
84 }
85
86 const QString SignalGroup::name() const
87 {
88         return name_;
89 }
90
91
92 SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) :
93         channel_(channel),
94         channel_type_(channel_type),
95         group_(nullptr),
96         conversion_type_(NoConversion),
97         min_value_(0),
98         max_value_(0)
99 {
100         if (channel_) {
101                 internal_name_ = QString::fromStdString(channel_->name());
102                 index_ = channel_->index();
103         }
104
105         connect(&delayed_conversion_starter_, SIGNAL(timeout()),
106                 this, SLOT(on_delayed_conversion_start()));
107         delayed_conversion_starter_.setSingleShot(true);
108         delayed_conversion_starter_.setInterval(ConversionDelay);
109 }
110
111 SignalBase::~SignalBase()
112 {
113         stop_conversion();
114 }
115
116 shared_ptr<sigrok::Channel> SignalBase::channel() const
117 {
118         return channel_;
119 }
120
121 bool SignalBase::enabled() const
122 {
123         return (channel_) ? channel_->enabled() : true;
124 }
125
126 void SignalBase::set_enabled(bool value)
127 {
128         if (channel_) {
129                 channel_->set_enabled(value);
130                 enabled_changed(value);
131         }
132 }
133
134 SignalBase::ChannelType SignalBase::type() const
135 {
136         return channel_type_;
137 }
138
139 unsigned int SignalBase::index() const
140 {
141         return index_;
142 }
143
144 void SignalBase::set_index(unsigned int index)
145 {
146         index_ = index;
147 }
148
149 unsigned int SignalBase::logic_bit_index() const
150 {
151         if (channel_type_ == LogicChannel)
152                 return index_;
153         else
154                 return 0;
155 }
156
157 void SignalBase::set_group(SignalGroup* group)
158 {
159         group_ = group;
160 }
161
162 SignalGroup* SignalBase::group() const
163 {
164         return group_;
165 }
166
167 QString SignalBase::name() const
168 {
169         return (channel_) ? QString::fromStdString(channel_->name()) : name_;
170 }
171
172 QString SignalBase::internal_name() const
173 {
174         return internal_name_;
175 }
176
177 void SignalBase::set_internal_name(QString internal_name)
178 {
179         internal_name_ = internal_name;
180 }
181
182 QString SignalBase::display_name() const
183 {
184         if ((name() != internal_name_) && (!internal_name_.isEmpty()))
185                 return name() + " (" + internal_name_ + ")";
186         else
187                 return name();
188 }
189
190 void SignalBase::set_name(QString name)
191 {
192         if (channel_)
193                 channel_->set_name(name.toUtf8().constData());
194
195         name_ = name;
196
197         name_changed(name);
198 }
199
200 QColor SignalBase::color() const
201 {
202         return color_;
203 }
204
205 void SignalBase::set_color(QColor color)
206 {
207         color_ = color;
208
209         bgcolor_ = color;
210         bgcolor_.setAlpha(ColorBGAlpha);
211
212         color_changed(color);
213 }
214
215 QColor SignalBase::bgcolor() const
216 {
217         return bgcolor_;
218 }
219
220 void SignalBase::set_data(shared_ptr<pv::data::SignalData> data)
221 {
222         if (data_) {
223                 disconnect(data.get(), SIGNAL(samples_cleared()),
224                         this, SLOT(on_samples_cleared()));
225                 disconnect(data.get(), SIGNAL(samples_added(shared_ptr<Segment>, uint64_t, uint64_t)),
226                         this, SLOT(on_samples_added(shared_ptr<Segment>, uint64_t, uint64_t)));
227
228                 shared_ptr<Analog> analog = analog_data();
229                 if (analog)
230                         disconnect(analog.get(), SIGNAL(min_max_changed(float, float)),
231                                 this, SLOT(on_min_max_changed(float, float)));
232         }
233
234         data_ = data;
235
236         if (data_) {
237                 connect(data.get(), SIGNAL(samples_cleared()),
238                         this, SLOT(on_samples_cleared()));
239                 connect(data.get(), SIGNAL(samples_added(SharedPtrToSegment, uint64_t, uint64_t)),
240                         this, SLOT(on_samples_added(SharedPtrToSegment, uint64_t, uint64_t)));
241
242                 shared_ptr<Analog> analog = analog_data();
243                 if (analog)
244                         connect(analog.get(), SIGNAL(min_max_changed(float, float)),
245                                 this, SLOT(on_min_max_changed(float, float)));
246         }
247 }
248
249 void SignalBase::clear_sample_data()
250 {
251         if (analog_data())
252                 analog_data()->clear();
253
254         if (logic_data())
255                 logic_data()->clear();
256 }
257
258 shared_ptr<data::Analog> SignalBase::analog_data() const
259 {
260         return dynamic_pointer_cast<Analog>(data_);
261 }
262
263 shared_ptr<data::Logic> SignalBase::logic_data() const
264 {
265         shared_ptr<Logic> result = dynamic_pointer_cast<Logic>(data_);
266
267         if (((conversion_type_ == A2LConversionByThreshold) ||
268                 (conversion_type_ == A2LConversionBySchmittTrigger)))
269                 result = dynamic_pointer_cast<Logic>(converted_data_);
270
271         return result;
272 }
273
274 bool SignalBase::segment_is_complete(uint32_t segment_id) const
275 {
276         bool result = true;
277
278         shared_ptr<Analog> adata = analog_data();
279         if (adata)
280         {
281                 auto segments = adata->analog_segments();
282                 try {
283                         result = segments.at(segment_id)->is_complete();
284                 } catch (out_of_range&) {
285                         // Do nothing
286                 }
287         } else {
288                 shared_ptr<Logic> ldata = logic_data();
289                 if (ldata) {
290                         shared_ptr<Logic> data = dynamic_pointer_cast<Logic>(data_);
291                         auto segments = data->logic_segments();
292                         try {
293                                 result = segments.at(segment_id)->is_complete();
294                         } catch (out_of_range&) {
295                                 // Do nothing
296                         }
297                 }
298         }
299
300         return result;
301 }
302
303 bool SignalBase::has_samples() const
304 {
305         bool result = false;
306
307         shared_ptr<Analog> adata = analog_data();
308         if (adata)
309         {
310                 auto segments = adata->analog_segments();
311                 if ((segments.size() > 0) && (segments.front()->get_sample_count() > 0))
312                         result = true;
313         } else {
314                 shared_ptr<Logic> ldata = logic_data();
315                 if (ldata) {
316                         auto segments = ldata->logic_segments();
317                         if ((segments.size() > 0) && (segments.front()->get_sample_count() > 0))
318                                 result = true;
319                 }
320         }
321
322         return result;
323 }
324
325 double SignalBase::get_samplerate() const
326 {
327         shared_ptr<Analog> adata = analog_data();
328         if (adata)
329                 return adata->get_samplerate();
330         else {
331                 shared_ptr<Logic> ldata = logic_data();
332                 if (ldata)
333                         return ldata->get_samplerate();
334         }
335
336         // Default samplerate is 1 Hz
337         return 1.0;
338 }
339
340 SignalBase::ConversionType SignalBase::get_conversion_type() const
341 {
342         return conversion_type_;
343 }
344
345 void SignalBase::set_conversion_type(ConversionType t)
346 {
347         if (conversion_type_ != NoConversion) {
348                 stop_conversion();
349
350                 // Discard converted data
351                 converted_data_.reset();
352                 samples_cleared();
353         }
354
355         conversion_type_ = t;
356
357         // Re-create an empty container
358         // so that the signal is recognized as providing logic data
359         // and thus can be assigned to a decoder
360         if (conversion_is_a2l())
361                 if (!converted_data_)
362                         converted_data_ = make_shared<Logic>(1);  // Contains only one channel
363
364         start_conversion();
365
366         conversion_type_changed(t);
367 }
368
369 map<QString, QVariant> SignalBase::get_conversion_options() const
370 {
371         return conversion_options_;
372 }
373
374 bool SignalBase::set_conversion_option(QString key, QVariant value)
375 {
376         QVariant old_value;
377
378         auto key_iter = conversion_options_.find(key);
379         if (key_iter != conversion_options_.end())
380                 old_value = key_iter->second;
381
382         conversion_options_[key] = value;
383
384         return (value != old_value);
385 }
386
387 vector<double> SignalBase::get_conversion_thresholds(const ConversionType t,
388         const bool always_custom) const
389 {
390         vector<double> result;
391         ConversionType conv_type = t;
392         ConversionPreset preset;
393
394         // Use currently active conversion if no conversion type was supplied
395         if (conv_type == NoConversion)
396                 conv_type = conversion_type_;
397
398         if (always_custom)
399                 preset = NoPreset;
400         else
401                 preset = get_current_conversion_preset();
402
403         if (conv_type == A2LConversionByThreshold) {
404                 double thr = 0;
405
406                 if (preset == NoPreset) {
407                         auto thr_iter = conversion_options_.find("threshold_value");
408                         if (thr_iter != conversion_options_.end())
409                                 thr = (thr_iter->second).toDouble();
410                 }
411
412                 if (preset == DynamicPreset)
413                         thr = (min_value_ + max_value_) * 0.5;  // middle between min and max
414
415                 if ((int)preset == 1) thr = 0.9;
416                 if ((int)preset == 2) thr = 1.8;
417                 if ((int)preset == 3) thr = 2.5;
418                 if ((int)preset == 4) thr = 1.5;
419
420                 result.push_back(thr);
421         }
422
423         if (conv_type == A2LConversionBySchmittTrigger) {
424                 double thr_lo = 0, thr_hi = 0;
425
426                 if (preset == NoPreset) {
427                         auto thr_lo_iter = conversion_options_.find("threshold_value_low");
428                         if (thr_lo_iter != conversion_options_.end())
429                                 thr_lo = (thr_lo_iter->second).toDouble();
430
431                         auto thr_hi_iter = conversion_options_.find("threshold_value_high");
432                         if (thr_hi_iter != conversion_options_.end())
433                                 thr_hi = (thr_hi_iter->second).toDouble();
434                 }
435
436                 if (preset == DynamicPreset) {
437                         const double amplitude = max_value_ - min_value_;
438                         const double center = min_value_ + (amplitude / 2);
439                         thr_lo = center - (amplitude * 0.15);  // 15% margin
440                         thr_hi = center + (amplitude * 0.15);  // 15% margin
441                 }
442
443                 if ((int)preset == 1) { thr_lo = 0.3; thr_hi = 1.2; }
444                 if ((int)preset == 2) { thr_lo = 0.7; thr_hi = 2.5; }
445                 if ((int)preset == 3) { thr_lo = 1.3; thr_hi = 3.7; }
446                 if ((int)preset == 4) { thr_lo = 0.8; thr_hi = 2.0; }
447
448                 result.push_back(thr_lo);
449                 result.push_back(thr_hi);
450         }
451
452         return result;
453 }
454
455 vector< pair<QString, int> > SignalBase::get_conversion_presets() const
456 {
457         vector< pair<QString, int> > presets;
458
459         if (conversion_type_ == A2LConversionByThreshold) {
460                 // Source: http://www.interfacebus.com/voltage_threshold.html
461                 presets.emplace_back(tr("Signal average"), 0);
462                 presets.emplace_back(tr("0.9V (for 1.8V CMOS)"), 1);
463                 presets.emplace_back(tr("1.8V (for 3.3V CMOS)"), 2);
464                 presets.emplace_back(tr("2.5V (for 5.0V CMOS)"), 3);
465                 presets.emplace_back(tr("1.5V (for TTL)"), 4);
466         }
467
468         if (conversion_type_ == A2LConversionBySchmittTrigger) {
469                 // Source: http://www.interfacebus.com/voltage_threshold.html
470                 presets.emplace_back(tr("Signal average +/- 15%"), 0);
471                 presets.emplace_back(tr("0.3V/1.2V (for 1.8V CMOS)"), 1);
472                 presets.emplace_back(tr("0.7V/2.5V (for 3.3V CMOS)"), 2);
473                 presets.emplace_back(tr("1.3V/3.7V (for 5.0V CMOS)"), 3);
474                 presets.emplace_back(tr("0.8V/2.0V (for TTL)"), 4);
475         }
476
477         return presets;
478 }
479
480 SignalBase::ConversionPreset SignalBase::get_current_conversion_preset() const
481 {
482         auto preset = conversion_options_.find("preset");
483         if (preset != conversion_options_.end())
484                 return (ConversionPreset)((preset->second).toInt());
485
486         return DynamicPreset;
487 }
488
489 void SignalBase::set_conversion_preset(ConversionPreset id)
490 {
491         conversion_options_["preset"] = (int)id;
492 }
493
494 #ifdef ENABLE_DECODE
495 bool SignalBase::is_decode_signal() const
496 {
497         return (channel_type_ == DecodeChannel);
498 }
499 #endif
500
501 void SignalBase::save_settings(QSettings &settings) const
502 {
503         settings.setValue("name", name());
504         settings.setValue("enabled", enabled());
505         settings.setValue("color", color().rgba());
506         settings.setValue("conversion_type", (int)conversion_type_);
507
508         settings.setValue("conv_options", (int)(conversion_options_.size()));
509         int i = 0;
510         for (auto& kvp : conversion_options_) {
511                 settings.setValue(QString("conv_option%1_key").arg(i), kvp.first);
512                 settings.setValue(QString("conv_option%1_value").arg(i), kvp.second);
513                 i++;
514         }
515 }
516
517 void SignalBase::restore_settings(QSettings &settings)
518 {
519         if (settings.contains("name"))
520                 set_name(settings.value("name").toString());
521
522         if (settings.contains("enabled"))
523                 set_enabled(settings.value("enabled").toBool());
524
525         if (settings.contains("color")) {
526                 QVariant value = settings.value("color");
527
528                 // Workaround for Qt QColor serialization bug on OSX
529                 if ((QMetaType::Type)(value.type()) == QMetaType::QColor)
530                         set_color(value.value<QColor>());
531                 else
532                         set_color(QColor::fromRgba(value.value<uint32_t>()));
533
534                 // A color with an alpha value of 0 makes the signal marker invisible
535                 if (color() == QColor(0, 0, 0, 0))
536                         set_color(Qt::gray);
537         }
538
539         if (settings.contains("conversion_type"))
540                 set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
541
542         int conv_options = 0;
543         if (settings.contains("conv_options"))
544                 conv_options = settings.value("conv_options").toInt();
545
546         if (conv_options)
547                 for (int i = 0; i < conv_options; i++) {
548                         const QString key_id = QString("conv_option%1_key").arg(i);
549                         const QString value_id = QString("conv_option%1_value").arg(i);
550
551                         if (settings.contains(key_id) && settings.contains(value_id))
552                                 conversion_options_[settings.value(key_id).toString()] =
553                                         settings.value(value_id);
554                 }
555 }
556
557 bool SignalBase::conversion_is_a2l() const
558 {
559         return (((conversion_type_ == A2LConversionByThreshold) ||
560                 (conversion_type_ == A2LConversionBySchmittTrigger)));
561 }
562
563 void SignalBase::convert_single_segment_range(AnalogSegment *asegment,
564         LogicSegment *lsegment, uint64_t start_sample, uint64_t end_sample)
565 {
566         if (end_sample > start_sample) {
567                 tie(min_value_, max_value_) = asegment->get_min_max();
568
569                 // Create sigrok::Analog instance
570                 float *asamples = new float[ConversionBlockSize];
571                 uint8_t *lsamples = new uint8_t[ConversionBlockSize];
572
573                 vector<shared_ptr<sigrok::Channel> > channels;
574                 channels.push_back(channel_);
575
576                 vector<const sigrok::QuantityFlag*> mq_flags;
577                 const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE;
578                 const sigrok::Unit * const unit = sigrok::Unit::VOLT;
579
580                 shared_ptr<sigrok::Packet> packet =
581                         Session::sr_context->create_analog_packet(channels,
582                         asamples, ConversionBlockSize, mq, unit, mq_flags);
583
584                 shared_ptr<sigrok::Analog> analog =
585                         dynamic_pointer_cast<sigrok::Analog>(packet->payload());
586
587                 // Convert
588                 uint64_t i = start_sample;
589
590                 if (conversion_type_ == A2LConversionByThreshold) {
591                         const double threshold = get_conversion_thresholds()[0];
592
593                         // Convert as many sample blocks as we can
594                         while ((end_sample - i) > ConversionBlockSize) {
595                                 asegment->get_samples(i, i + ConversionBlockSize, asamples);
596
597                                 shared_ptr<sigrok::Logic> logic =
598                                         analog->get_logic_via_threshold(threshold, lsamples);
599
600                                 lsegment->append_payload(logic->data_pointer(), logic->data_length());
601                                 samples_added(lsegment->segment_id(), i, i + ConversionBlockSize);
602                                 i += ConversionBlockSize;
603                         }
604
605                         // Re-create sigrok::Analog and convert remaining samples
606                         packet = Session::sr_context->create_analog_packet(channels,
607                                 asamples, end_sample - i, mq, unit, mq_flags);
608
609                         analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
610
611                         asegment->get_samples(i, end_sample, asamples);
612                         shared_ptr<sigrok::Logic> logic =
613                                 analog->get_logic_via_threshold(threshold, lsamples);
614                         lsegment->append_payload(logic->data_pointer(), logic->data_length());
615                         samples_added(lsegment->segment_id(), i, end_sample);
616                 }
617
618                 if (conversion_type_ == A2LConversionBySchmittTrigger) {
619                         const vector<double> thresholds = get_conversion_thresholds();
620                         const double lo_thr = thresholds[0];
621                         const double hi_thr = thresholds[1];
622
623                         uint8_t state = 0;  // TODO Use value of logic sample n-1 instead of 0
624
625                         // Convert as many sample blocks as we can
626                         while ((end_sample - i) > ConversionBlockSize) {
627                                 asegment->get_samples(i, i + ConversionBlockSize, asamples);
628
629                                 shared_ptr<sigrok::Logic> logic =
630                                         analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
631                                                 &state, lsamples);
632
633                                 lsegment->append_payload(logic->data_pointer(), logic->data_length());
634                                 samples_added(lsegment->segment_id(), i, i + ConversionBlockSize);
635                                 i += ConversionBlockSize;
636                         }
637
638                         // Re-create sigrok::Analog and convert remaining samples
639                         packet = Session::sr_context->create_analog_packet(channels,
640                                 asamples, end_sample - i, mq, unit, mq_flags);
641
642                         analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
643
644                         asegment->get_samples(i, end_sample, asamples);
645                         shared_ptr<sigrok::Logic> logic =
646                                 analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
647                                         &state, lsamples);
648                         lsegment->append_payload(logic->data_pointer(), logic->data_length());
649                         samples_added(lsegment->segment_id(), i, end_sample);
650                 }
651
652                 // If acquisition is ongoing, start-/endsample may have changed
653                 end_sample = asegment->get_sample_count();
654
655                 delete[] lsamples;
656                 delete[] asamples;
657         }
658 }
659
660 void SignalBase::convert_single_segment(AnalogSegment *asegment, LogicSegment *lsegment)
661 {
662         uint64_t start_sample, end_sample, old_end_sample;
663         start_sample = end_sample = 0;
664         bool complete_state, old_complete_state;
665
666         start_sample = lsegment->get_sample_count();
667         end_sample = asegment->get_sample_count();
668         complete_state = asegment->is_complete();
669
670         // Don't do anything if the segment is still being filled and the sample count is too small
671         if ((!complete_state) && (end_sample - start_sample < ConversionBlockSize))
672                 return;
673
674         do {
675                 convert_single_segment_range(asegment, lsegment, start_sample, end_sample);
676
677                 old_end_sample = end_sample;
678                 old_complete_state = complete_state;
679
680                 start_sample = lsegment->get_sample_count();
681                 end_sample = asegment->get_sample_count();
682                 complete_state = asegment->is_complete();
683
684                 // If the segment has been incomplete when we were called and has been
685                 // completed in the meanwhile, we convert the remaining samples as well.
686                 // Also, if a sufficient number of samples was added in the meanwhile,
687                 // we do another round of sample conversion.
688         } while ((complete_state != old_complete_state) ||
689                 (end_sample - old_end_sample >= ConversionBlockSize));
690 }
691
692 void SignalBase::conversion_thread_proc()
693 {
694         shared_ptr<Analog> analog_data;
695
696         if (conversion_is_a2l()) {
697                 analog_data = dynamic_pointer_cast<Analog>(data_);
698
699                 if (analog_data->analog_segments().size() == 0) {
700                         unique_lock<mutex> input_lock(conversion_input_mutex_);
701                         conversion_input_cond_.wait(input_lock);
702                 }
703
704         } else
705                 // Currently, we only handle A2L conversions
706                 return;
707
708         // If we had to wait for input data, we may have been notified to terminate
709         if (conversion_interrupt_)
710                 return;
711
712         uint32_t segment_id = 0;
713
714         AnalogSegment *asegment = analog_data->analog_segments().front().get();
715         assert(asegment);
716
717         const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(converted_data_);
718         assert(logic_data);
719
720         // Create the initial logic data segment if needed
721         if (logic_data->logic_segments().size() == 0) {
722                 shared_ptr<LogicSegment> new_segment =
723                         make_shared<LogicSegment>(*logic_data.get(), 0, 1, asegment->samplerate());
724                 logic_data->push_segment(new_segment);
725         }
726
727         LogicSegment *lsegment = logic_data->logic_segments().front().get();
728         assert(lsegment);
729
730         do {
731                 convert_single_segment(asegment, lsegment);
732
733                 // Only advance to next segment if the current input segment is complete
734                 if (asegment->is_complete() &&
735                         analog_data->analog_segments().size() > logic_data->logic_segments().size()) {
736                         // There are more segments to process
737                         segment_id++;
738
739                         try {
740                                 asegment = analog_data->analog_segments().at(segment_id).get();
741                         } catch (out_of_range&) {
742                                 qDebug() << "Conversion error for" << name() << ": no analog segment" \
743                                         << segment_id << ", segments size is" << analog_data->analog_segments().size();
744                                 return;
745                         }
746
747                         shared_ptr<LogicSegment> new_segment = make_shared<LogicSegment>(
748                                 *logic_data.get(), segment_id, 1, asegment->samplerate());
749                         logic_data->push_segment(new_segment);
750
751                         lsegment = logic_data->logic_segments().back().get();
752                 } else {
753                         // No more samples/segments to process, wait for data or interrupt
754                         if (!conversion_interrupt_) {
755                                 unique_lock<mutex> input_lock(conversion_input_mutex_);
756                                 conversion_input_cond_.wait(input_lock);
757                         }
758                 }
759         } while (!conversion_interrupt_);
760 }
761
762 void SignalBase::start_conversion(bool delayed_start)
763 {
764         if (delayed_start) {
765                 delayed_conversion_starter_.start();
766                 return;
767         }
768
769         stop_conversion();
770
771         if (converted_data_)
772                 converted_data_->clear();
773         samples_cleared();
774
775         conversion_interrupt_ = false;
776         conversion_thread_ = std::thread(
777                 &SignalBase::conversion_thread_proc, this);
778 }
779
780 void SignalBase::stop_conversion()
781 {
782         // Stop conversion so we can restart it from the beginning
783         conversion_interrupt_ = true;
784         conversion_input_cond_.notify_one();
785         if (conversion_thread_.joinable())
786                 conversion_thread_.join();
787 }
788
789 void SignalBase::on_samples_cleared()
790 {
791         if (converted_data_)
792                 converted_data_->clear();
793
794         samples_cleared();
795 }
796
797 void SignalBase::on_samples_added(SharedPtrToSegment segment, uint64_t start_sample,
798         uint64_t end_sample)
799 {
800         if (conversion_type_ != NoConversion) {
801                 if (conversion_thread_.joinable()) {
802                         // Notify the conversion thread since it's running
803                         conversion_input_cond_.notify_one();
804                 } else {
805                         // Start the conversion thread unless the delay timer is running
806                         if (!delayed_conversion_starter_.isActive())
807                                 start_conversion();
808                 }
809         }
810
811         samples_added(segment->segment_id(), start_sample, end_sample);
812 }
813
814 void SignalBase::on_min_max_changed(float min, float max)
815 {
816         // Restart conversion if one is enabled and uses a calculated threshold
817         if ((conversion_type_ != NoConversion) &&
818                 (get_current_conversion_preset() == DynamicPreset))
819                 start_conversion(true);
820
821         min_max_changed(min, max);
822 }
823
824 void SignalBase::on_capture_state_changed(int state)
825 {
826         if (state == Session::Running) {
827                 // Restart conversion if one is enabled
828                 if (conversion_type_ != NoConversion)
829                         start_conversion();
830         }
831 }
832
833 void SignalBase::on_delayed_conversion_start()
834 {
835         start_conversion();
836 }
837
838 } // namespace data
839 } // namespace pv