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