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