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