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