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