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