]> sigrok.org Git - pulseview.git/blob - pv/data/signalbase.cpp
Fix some random clang-tidy warnings.
[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 <pv/binding/decoder.hpp>
30 #include <pv/session.hpp>
31
32 using std::dynamic_pointer_cast;
33 using std::make_shared;
34 using std::shared_ptr;
35 using std::tie;
36 using std::unique_lock;
37
38 namespace pv {
39 namespace data {
40
41 const int SignalBase::ColourBGAlpha = 8 * 256 / 100;
42 const uint64_t SignalBase::ConversionBlockSize = 4096;
43
44 SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) :
45         channel_(channel),
46         channel_type_(channel_type),
47         conversion_type_(NoConversion),
48         min_value_(0),
49         max_value_(0)
50 {
51         if (channel_)
52                 internal_name_ = QString::fromStdString(channel_->name());
53 }
54
55 SignalBase::~SignalBase()
56 {
57         stop_conversion();
58 }
59
60 shared_ptr<sigrok::Channel> SignalBase::channel() const
61 {
62         return channel_;
63 }
64
65 QString SignalBase::name() const
66 {
67         return (channel_) ? QString::fromStdString(channel_->name()) : name_;
68 }
69
70 QString SignalBase::internal_name() const
71 {
72         return internal_name_;
73 }
74
75 void SignalBase::set_name(QString name)
76 {
77         if (channel_)
78                 channel_->set_name(name.toUtf8().constData());
79
80         name_ = name;
81
82         name_changed(name);
83 }
84
85 bool SignalBase::enabled() const
86 {
87         return (channel_) ? channel_->enabled() : true;
88 }
89
90 void SignalBase::set_enabled(bool value)
91 {
92         if (channel_) {
93                 channel_->set_enabled(value);
94                 enabled_changed(value);
95         }
96 }
97
98 SignalBase::ChannelType SignalBase::type() const
99 {
100         return channel_type_;
101 }
102
103 unsigned int SignalBase::index() const
104 {
105         return (channel_) ? channel_->index() : 0;
106 }
107
108 unsigned int SignalBase::logic_bit_index() const
109 {
110         if (channel_type_ == LogicChannel)
111                 return channel_->index();
112         else
113                 return 0;
114 }
115
116 QColor SignalBase::colour() const
117 {
118         return colour_;
119 }
120
121 void SignalBase::set_colour(QColor colour)
122 {
123         colour_ = colour;
124
125         bgcolour_ = colour;
126         bgcolour_.setAlpha(ColourBGAlpha);
127
128         colour_changed(colour);
129 }
130
131 QColor SignalBase::bgcolour() const
132 {
133         return bgcolour_;
134 }
135
136 void SignalBase::set_data(shared_ptr<pv::data::SignalData> data)
137 {
138         if (data_) {
139                 disconnect(data.get(), SIGNAL(samples_cleared()),
140                         this, SLOT(on_samples_cleared()));
141                 disconnect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
142                         this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
143         }
144
145         data_ = data;
146
147         if (data_) {
148                 connect(data.get(), SIGNAL(samples_cleared()),
149                         this, SLOT(on_samples_cleared()));
150                 connect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
151                         this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
152         }
153 }
154
155 shared_ptr<data::Analog> SignalBase::analog_data() const
156 {
157         shared_ptr<Analog> result = nullptr;
158
159         if (channel_type_ == AnalogChannel)
160                 result = dynamic_pointer_cast<Analog>(data_);
161
162         return result;
163 }
164
165 shared_ptr<data::Logic> SignalBase::logic_data() const
166 {
167         shared_ptr<Logic> result = nullptr;
168
169         if (channel_type_ == LogicChannel)
170                 result = dynamic_pointer_cast<Logic>(data_);
171
172         if (((conversion_type_ == A2LConversionByThreshold) ||
173                 (conversion_type_ == A2LConversionBySchmittTrigger)))
174                 result = dynamic_pointer_cast<Logic>(converted_data_);
175
176         return result;
177 }
178
179 SignalBase::ConversionType SignalBase::get_conversion_type() const
180 {
181         return conversion_type_;
182 }
183
184 void SignalBase::set_conversion_type(ConversionType t)
185 {
186         if (conversion_type_ != NoConversion) {
187                 stop_conversion();
188
189                 // Discard converted data
190                 converted_data_.reset();
191                 samples_cleared();
192         }
193
194         conversion_type_ = t;
195
196         // Re-create an empty container
197         // so that the signal is recognized as providing logic data
198         // and thus can be assigned to a decoder
199         if (conversion_is_a2l())
200                 if (!converted_data_)
201                         converted_data_ = make_shared<Logic>(1);  // Contains only one channel
202
203         start_conversion();
204
205         conversion_type_changed(t);
206 }
207
208 map<QString, QVariant> SignalBase::get_conversion_options() const
209 {
210         return conversion_options_;
211 }
212
213 bool SignalBase::set_conversion_option(QString key, QVariant value)
214 {
215         QVariant old_value;
216
217         auto key_iter = conversion_options_.find(key);
218         if (key_iter != conversion_options_.end())
219                 old_value = key_iter->second;
220
221         conversion_options_[key] = value;
222
223         return (value != old_value);
224 }
225
226 vector<double> SignalBase::get_conversion_thresholds(const ConversionType t,
227         const bool always_custom) const
228 {
229         vector<double> result;
230         ConversionType conv_type = t;
231         int preset;
232
233         // Use currently active conversion if no conversion type was supplied
234         if (conv_type == NoConversion)
235                 conv_type = conversion_type_;
236
237         if (always_custom)
238                 preset = -1;
239         else
240                 preset = get_current_conversion_preset();
241
242         if (conv_type == A2LConversionByThreshold) {
243                 double thr = 0;
244
245                 if (preset == -1) {
246                         auto thr_iter = conversion_options_.find("threshold_value");
247                         if (thr_iter != conversion_options_.end())
248                                 thr = (thr_iter->second).toDouble();
249                 }
250
251                 if (preset == 0)
252                         thr = (min_value_ + max_value_) * 0.5;  // middle between min and max
253
254                 if (preset == 1) thr = 0.9;
255                 if (preset == 2) thr = 1.8;
256                 if (preset == 3) thr = 2.5;
257                 if (preset == 4) thr = 1.5;
258
259                 result.push_back(thr);
260         }
261
262         if (conv_type == A2LConversionBySchmittTrigger) {
263                 double thr_lo = 0, thr_hi = 0;
264
265                 if (preset == -1) {
266                         auto thr_lo_iter = conversion_options_.find("threshold_value_low");
267                         if (thr_lo_iter != conversion_options_.end())
268                                 thr_lo = (thr_lo_iter->second).toDouble();
269
270                         auto thr_hi_iter = conversion_options_.find("threshold_value_high");
271                         if (thr_hi_iter != conversion_options_.end())
272                                 thr_hi = (thr_hi_iter->second).toDouble();
273                 }
274
275                 if (preset == 0) {
276                         const double amplitude = max_value_ - min_value_;
277                         const double center = min_value_ + (amplitude / 2);
278                         thr_lo = center - (amplitude * 0.15);  // 15% margin
279                         thr_hi = center + (amplitude * 0.15);  // 15% margin
280                 }
281
282                 if (preset == 1) { thr_lo = 0.3; thr_hi = 1.2; }
283                 if (preset == 2) { thr_lo = 0.7; thr_hi = 2.5; }
284                 if (preset == 3) { thr_lo = 1.3; thr_hi = 3.7; }
285                 if (preset == 4) { thr_lo = 0.8; thr_hi = 2.0; }
286
287                 result.push_back(thr_lo);
288                 result.push_back(thr_hi);
289         }
290
291         return result;
292 }
293
294 vector< pair<QString, int> > SignalBase::get_conversion_presets() const
295 {
296         vector< pair<QString, int> > presets;
297
298         if (conversion_type_ == A2LConversionByThreshold) {
299                 // Source: http://www.interfacebus.com/voltage_threshold.html
300                 presets.emplace_back(tr("Signal average"), 0);
301                 presets.emplace_back(tr("0.9V (for 1.8V CMOS)"), 1);
302                 presets.emplace_back(tr("1.8V (for 3.3V CMOS)"), 2);
303                 presets.emplace_back(tr("2.5V (for 5.0V CMOS)"), 3);
304                 presets.emplace_back(tr("1.5V (for TTL)"), 4);
305         }
306
307         if (conversion_type_ == A2LConversionBySchmittTrigger) {
308                 // Source: http://www.interfacebus.com/voltage_threshold.html
309                 presets.emplace_back(tr("Signal average +/- 15%"), 0);
310                 presets.emplace_back(tr("0.3V/1.2V (for 1.8V CMOS)"), 1);
311                 presets.emplace_back(tr("0.7V/2.5V (for 3.3V CMOS)"), 2);
312                 presets.emplace_back(tr("1.3V/3.7V (for 5.0V CMOS)"), 3);
313                 presets.emplace_back(tr("0.8V/2.0V (for TTL)"), 4);
314         }
315
316         return presets;
317 }
318
319 int SignalBase::get_current_conversion_preset() const
320 {
321         auto preset = conversion_options_.find("preset");
322         if (preset != conversion_options_.end())
323                 return (preset->second).toInt();
324
325         return -1;
326 }
327
328 void SignalBase::set_conversion_preset(int id)
329 {
330         conversion_options_["preset"] = id;
331 }
332
333 #ifdef ENABLE_DECODE
334 bool SignalBase::is_decode_signal() const
335 {
336         return (channel_type_ == DecodeChannel);
337 }
338 #endif
339
340 void SignalBase::save_settings(QSettings &settings) const
341 {
342         settings.setValue("name", name());
343         settings.setValue("enabled", enabled());
344         settings.setValue("colour", colour());
345         settings.setValue("conversion_type", (int)conversion_type_);
346
347         settings.setValue("conv_options", (int)(conversion_options_.size()));
348         int i = 0;
349         for (auto kvp : conversion_options_) {
350                 settings.setValue(QString("conv_option%1_key").arg(i), kvp.first);
351                 settings.setValue(QString("conv_option%1_value").arg(i), kvp.second);
352                 i++;
353         }
354 }
355
356 void SignalBase::restore_settings(QSettings &settings)
357 {
358         set_name(settings.value("name").toString());
359         set_enabled(settings.value("enabled").toBool());
360         set_colour(settings.value("colour").value<QColor>());
361         set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
362
363         int conv_options = settings.value("conv_options").toInt();
364
365         if (conv_options)
366                 for (int i = 0; i < conv_options; i++) {
367                         QString key = settings.value(QString("conv_option%1_key").arg(i)).toString();
368                         QVariant value = settings.value(QString("conv_option%1_value").arg(i));
369                         conversion_options_[key] = value;
370                 }
371 }
372
373 bool SignalBase::conversion_is_a2l() const
374 {
375         return ((channel_type_ == AnalogChannel) &&
376                 ((conversion_type_ == A2LConversionByThreshold) ||
377                 (conversion_type_ == A2LConversionBySchmittTrigger)));
378 }
379
380 void SignalBase::conversion_thread_proc(QObject* segment)
381 {
382         // TODO Support for multiple segments is missing
383
384         uint64_t start_sample, end_sample;
385         start_sample = end_sample = 0;
386
387         do {
388                 if (conversion_is_a2l()) {
389
390                         AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
391
392                         const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(converted_data_);
393
394                         // Create the initial logic data segment if needed
395                         if (logic_data->segments().size() == 0) {
396                                 shared_ptr<LogicSegment> lsegment =
397                                         make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
398                                 logic_data->push_segment(lsegment);
399                         }
400
401                         LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
402
403                         start_sample = lsegment->get_sample_count();
404                         end_sample = asegment->get_sample_count();
405
406                         if (end_sample > start_sample) {
407                                 tie(min_value_, max_value_) = asegment->get_min_max();
408
409                                 // Create sigrok::Analog instance
410                                 float *asamples = new float[ConversionBlockSize];
411                                 uint8_t *lsamples = new uint8_t[ConversionBlockSize];
412
413                                 vector<shared_ptr<sigrok::Channel> > channels;
414                                 channels.push_back(channel_);
415
416                                 vector<const sigrok::QuantityFlag*> mq_flags;
417                                 const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE;
418                                 const sigrok::Unit * const unit = sigrok::Unit::VOLT;
419
420                                 shared_ptr<sigrok::Packet> packet =
421                                         Session::sr_context->create_analog_packet(channels,
422                                         asamples, ConversionBlockSize, mq, unit, mq_flags);
423
424                                 shared_ptr<sigrok::Analog> analog =
425                                         dynamic_pointer_cast<sigrok::Analog>(packet->payload());
426
427                                 // Convert
428                                 uint64_t i = start_sample;
429
430                                 if (conversion_type_ == A2LConversionByThreshold) {
431                                         const double threshold = get_conversion_thresholds()[0];
432
433                                         // Convert as many sample blocks as we can
434                                         while ((end_sample - i) > ConversionBlockSize) {
435                                                 asegment->get_samples(i, i + ConversionBlockSize, asamples);
436
437                                                 shared_ptr<sigrok::Logic> logic =
438                                                         analog->get_logic_via_threshold(threshold, lsamples);
439
440                                                 lsegment->append_payload(logic->data_pointer(), logic->data_length());
441
442                                                 samples_added(lsegment, i, i + ConversionBlockSize);
443                                                 i += ConversionBlockSize;
444                                         }
445
446                                         // Re-create sigrok::Analog and convert remaining samples
447                                         packet = Session::sr_context->create_analog_packet(channels,
448                                                 asamples, end_sample - i, mq, unit, mq_flags);
449
450                                         analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
451
452                                         asegment->get_samples(i, end_sample, asamples);
453                                         shared_ptr<sigrok::Logic> logic =
454                                                 analog->get_logic_via_threshold(threshold, lsamples);
455                                         lsegment->append_payload(logic->data_pointer(), logic->data_length());
456                                         samples_added(lsegment, i, end_sample);
457                                 }
458
459                                 if (conversion_type_ == A2LConversionBySchmittTrigger) {
460                                         const vector<double> thresholds = get_conversion_thresholds();
461                                         const double lo_thr = thresholds[0];
462                                         const double hi_thr = thresholds[1];
463
464                                         uint8_t state = 0;  // TODO Use value of logic sample n-1 instead of 0
465
466                                         // Convert as many sample blocks as we can
467                                         while ((end_sample - i) > ConversionBlockSize) {
468                                                 asegment->get_samples(i, i + ConversionBlockSize, asamples);
469
470                                                 shared_ptr<sigrok::Logic> logic =
471                                                         analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
472                                                                 &state, lsamples);
473
474                                                 lsegment->append_payload(logic->data_pointer(), logic->data_length());
475
476                                                 samples_added(lsegment, i, i + ConversionBlockSize);
477                                                 i += ConversionBlockSize;
478                                         }
479
480                                         // Re-create sigrok::Analog and convert remaining samples
481                                         packet = Session::sr_context->create_analog_packet(channels,
482                                                 asamples, end_sample - i, mq, unit, mq_flags);
483
484                                         analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
485
486                                         asegment->get_samples(i, end_sample, asamples);
487                                         shared_ptr<sigrok::Logic> logic =
488                                                 analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
489                                                         &state, lsamples);
490                                         lsegment->append_payload(logic->data_pointer(), logic->data_length());
491                                         samples_added(lsegment, i, end_sample);
492                                 }
493
494                                 // If acquisition is ongoing, start-/endsample may have changed
495                                 end_sample = asegment->get_sample_count();
496
497                                 delete[] lsamples;
498                                 delete[] asamples;
499                         }
500                 }
501
502                 if (!conversion_interrupt_ && (start_sample == end_sample)) {
503                         unique_lock<mutex> input_lock(conversion_input_mutex_);
504                         conversion_input_cond_.wait(input_lock);
505                 }
506         } while (!conversion_interrupt_);
507 }
508
509 void SignalBase::start_conversion()
510 {
511         stop_conversion();
512
513         if (converted_data_)
514                 converted_data_->clear();
515
516         if (conversion_is_a2l()) {
517                 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
518
519                 if (analog_data->analog_segments().size() > 0) {
520                         // TODO Support for multiple segments is missing
521                         AnalogSegment *asegment = analog_data->analog_segments().front().get();
522
523                         conversion_interrupt_ = false;
524                         conversion_thread_ = std::thread(
525                                 &SignalBase::conversion_thread_proc, this, asegment);
526                 }
527         }
528 }
529
530 void SignalBase::stop_conversion()
531 {
532         // Stop conversion so we can restart it from the beginning
533         conversion_interrupt_ = true;
534         conversion_input_cond_.notify_one();
535         if (conversion_thread_.joinable())
536                 conversion_thread_.join();
537 }
538
539 void SignalBase::on_samples_cleared()
540 {
541         if (converted_data_)
542                 converted_data_->clear();
543
544         samples_cleared();
545 }
546
547 void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
548         uint64_t end_sample)
549 {
550         if (conversion_type_ != NoConversion) {
551                 if (conversion_thread_.joinable()) {
552                         // Notify the conversion thread since it's running
553                         conversion_input_cond_.notify_one();
554                 } else {
555                         // Start the conversion thread
556                         start_conversion();
557                 }
558         }
559
560         samples_added(segment, start_sample, end_sample);
561 }
562
563 void SignalBase::on_capture_state_changed(int state)
564 {
565         if (state == Session::Running) {
566                 if (conversion_type_ != NoConversion) {
567                         // Restart conversion
568                         stop_conversion();
569                         start_conversion();
570                 }
571         }
572 }
573
574 } // namespace data
575 } // namespace pv