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