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