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