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