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