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