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