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