]> sigrok.org Git - pulseview.git/blame - pv/data/signalbase.cpp
Typo fix: treshold -> threshold
[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
SA
142 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
143 }
144
cbd2a2de 145 data_ = data;
12ea3616 146
8ce8ebb9
SA
147 if (data_) {
148 connect(data.get(), SIGNAL(samples_cleared()),
12ea3616 149 this, SLOT(on_samples_cleared()));
8ce8ebb9 150 connect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
12ea3616
SA
151 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
152 }
cbd2a2de
SA
153}
154
155shared_ptr<data::Analog> SignalBase::analog_data() const
156{
12ea3616
SA
157 shared_ptr<Analog> result = nullptr;
158
472a80c5 159 if (channel_type_ == AnalogChannel)
12ea3616
SA
160 result = dynamic_pointer_cast<Analog>(data_);
161
162 return result;
cbd2a2de
SA
163}
164
165shared_ptr<data::Logic> SignalBase::logic_data() const
166{
12ea3616
SA
167 shared_ptr<Logic> result = nullptr;
168
472a80c5 169 if (channel_type_ == LogicChannel)
12ea3616
SA
170 result = dynamic_pointer_cast<Logic>(data_);
171
b9cdbe03 172 if (((conversion_type_ == A2LConversionByThreshold) ||
12ea3616
SA
173 (conversion_type_ == A2LConversionBySchmittTrigger)))
174 result = dynamic_pointer_cast<Logic>(converted_data_);
175
176 return result;
177}
178
06b6ce26
SA
179SignalBase::ConversionType SignalBase::get_conversion_type() const
180{
181 return conversion_type_;
182}
183
12ea3616
SA
184void SignalBase::set_conversion_type(ConversionType t)
185{
186 if (conversion_type_ != NoConversion) {
27a3f09b 187 stop_conversion();
12ea3616
SA
188
189 // Discard converted data
190 converted_data_.reset();
1b56c646 191 samples_cleared();
12ea3616
SA
192 }
193
194 conversion_type_ = t;
195
ccccb914
SA
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
27a3f09b 203 start_conversion();
12ea3616
SA
204
205 conversion_type_changed(t);
cbd2a2de
SA
206}
207
52c900ac
SA
208map<QString, QVariant> SignalBase::get_conversion_options() const
209{
210 return conversion_options_;
211}
212
213bool 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
226vector<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
b9cdbe03 242 if (conv_type == A2LConversionByThreshold) {
52c900ac
SA
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
294vector< pair<QString, int> > SignalBase::get_conversion_presets() const
295{
296 vector< pair<QString, int> > presets;
297
b9cdbe03 298 if (conversion_type_ == A2LConversionByThreshold) {
52c900ac
SA
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
319int 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
328void SignalBase::set_conversion_preset(int id)
329{
330 conversion_options_["preset"] = id;
331}
332
bb7dd726
SA
333#ifdef ENABLE_DECODE
334bool SignalBase::is_decode_signal() const
335{
47747218 336 return (channel_type_ == DecodeChannel);
bb7dd726
SA
337}
338#endif
cbd2a2de 339
6de38b17
SA
340void SignalBase::save_settings(QSettings &settings) const
341{
342 settings.setValue("name", name());
343 settings.setValue("enabled", enabled());
344 settings.setValue("colour", colour());
12ea3616 345 settings.setValue("conversion_type", (int)conversion_type_);
52c900ac
SA
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 }
6de38b17
SA
354}
355
356void 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>());
12ea3616 361 set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
52c900ac
SA
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 }
12ea3616
SA
371}
372
ccccb914
SA
373bool SignalBase::conversion_is_a2l() const
374{
375 return ((channel_type_ == AnalogChannel) &&
b9cdbe03 376 ((conversion_type_ == A2LConversionByThreshold) ||
ccccb914
SA
377 (conversion_type_ == A2LConversionBySchmittTrigger)));
378}
379
27a3f09b 380void SignalBase::conversion_thread_proc(QObject* segment)
12ea3616 381{
12ea3616
SA
382 // TODO Support for multiple segments is missing
383
27a3f09b
SA
384 uint64_t start_sample, end_sample;
385 start_sample = end_sample = 0;
12ea3616 386
27a3f09b 387 do {
ccccb914 388 if (conversion_is_a2l()) {
27a3f09b
SA
389
390 AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
391
ccccb914 392 const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(converted_data_);
27a3f09b
SA
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 }
12ea3616 400
27a3f09b 401 LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
12ea3616 402
12ea3616
SA
403 start_sample = lsegment->get_sample_count();
404 end_sample = asegment->get_sample_count();
12ea3616 405
27a3f09b 406 if (end_sample > start_sample) {
52c900ac 407 tie(min_value_, max_value_) = asegment->get_min_max();
27a3f09b 408
419ec4e1
SA
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());
27a3f09b 426
419ec4e1 427 // Convert
27a3f09b
SA
428 uint64_t i = start_sample;
429
b9cdbe03 430 if (conversion_type_ == A2LConversionByThreshold) {
52c900ac 431 const double threshold = get_conversion_thresholds()[0];
27a3f09b
SA
432
433 // Convert as many sample blocks as we can
434 while ((end_sample - i) > ConversionBlockSize) {
b82243f7 435 asegment->get_samples(i, i + ConversionBlockSize, asamples);
419ec4e1
SA
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
27a3f09b
SA
442 samples_added(lsegment, i, i + ConversionBlockSize);
443 i += ConversionBlockSize;
27a3f09b
SA
444 }
445
419ec4e1
SA
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
b82243f7 452 asegment->get_samples(i, end_sample, asamples);
419ec4e1
SA
453 shared_ptr<sigrok::Logic> logic =
454 analog->get_logic_via_threshold(threshold, lsamples);
455 lsegment->append_payload(logic->data_pointer(), logic->data_length());
27a3f09b 456 samples_added(lsegment, i, end_sample);
27a3f09b
SA
457 }
458
459 if (conversion_type_ == A2LConversionBySchmittTrigger) {
52c900ac
SA
460 const vector<double> thresholds = get_conversion_thresholds();
461 const double lo_thr = thresholds[0];
462 const double hi_thr = thresholds[1];
463
27a3f09b
SA
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) {
b82243f7 468 asegment->get_samples(i, i + ConversionBlockSize, asamples);
419ec4e1
SA
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
27a3f09b
SA
476 samples_added(lsegment, i, i + ConversionBlockSize);
477 i += ConversionBlockSize;
27a3f09b
SA
478 }
479
419ec4e1
SA
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
b82243f7 486 asegment->get_samples(i, end_sample, asamples);
419ec4e1
SA
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());
27a3f09b 491 samples_added(lsegment, i, end_sample);
27a3f09b
SA
492 }
493
494 // If acquisition is ongoing, start-/endsample may have changed
495 end_sample = asegment->get_sample_count();
b82243f7 496
419ec4e1 497 delete[] lsamples;
b82243f7 498 delete[] asamples;
12ea3616 499 }
27a3f09b 500 }
12ea3616 501
27a3f09b
SA
502 if (!conversion_interrupt_ && (start_sample == end_sample)) {
503 unique_lock<mutex> input_lock(conversion_input_mutex_);
504 conversion_input_cond_.wait(input_lock);
12ea3616 505 }
27a3f09b
SA
506 } while (!conversion_interrupt_);
507}
12ea3616 508
27a3f09b
SA
509void SignalBase::start_conversion()
510{
511 stop_conversion();
512
52c900ac
SA
513 if (converted_data_)
514 converted_data_->clear();
515
ccccb914 516 if (conversion_is_a2l()) {
27a3f09b 517 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
12ea3616 518
27a3f09b
SA
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();
eae3bbbb 522
27a3f09b
SA
523 conversion_interrupt_ = false;
524 conversion_thread_ = std::thread(
525 &SignalBase::conversion_thread_proc, this, asegment);
12ea3616
SA
526 }
527 }
528}
529
27a3f09b
SA
530void 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
12ea3616
SA
539void SignalBase::on_samples_cleared()
540{
541 if (converted_data_)
542 converted_data_->clear();
eae3bbbb
SA
543
544 samples_cleared();
12ea3616
SA
545}
546
547void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
548 uint64_t end_sample)
549{
12ea3616 550 if (conversion_type_ != NoConversion) {
27a3f09b
SA
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 }
12ea3616 558 }
eae3bbbb
SA
559
560 samples_added(segment, start_sample, end_sample);
12ea3616
SA
561}
562
563void SignalBase::on_capture_state_changed(int state)
564{
27a3f09b
SA
565 if (state == Session::Running) {
566 if (conversion_type_ != NoConversion) {
567 // Restart conversion
568 stop_conversion();
569 start_conversion();
12ea3616
SA
570 }
571 }
6de38b17
SA
572}
573
bf0edd2b
SA
574} // namespace data
575} // namespace pv