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