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