]>
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 | ||
c063290a | 44 | const int SignalBase::ColourBGAlpha = 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 | ||
133 | QColor SignalBase::colour() const | |
134 | { | |
135 | return colour_; | |
136 | } | |
137 | ||
138 | void SignalBase::set_colour(QColor colour) | |
139 | { | |
140 | colour_ = colour; | |
141 | ||
142 | bgcolour_ = colour; | |
143 | bgcolour_.setAlpha(ColourBGAlpha); | |
144 | ||
145 | colour_changed(colour); | |
146 | } | |
147 | ||
148 | QColor SignalBase::bgcolour() const | |
149 | { | |
150 | return bgcolour_; | |
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(); | |
222 | } catch (out_of_range) { | |
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(); | |
233 | } catch (out_of_range) { | |
234 | // Do nothing | |
235 | } | |
236 | } | |
237 | ||
238 | return result; | |
239 | } | |
240 | ||
06b6ce26 SA |
241 | SignalBase::ConversionType SignalBase::get_conversion_type() const |
242 | { | |
243 | return conversion_type_; | |
244 | } | |
245 | ||
12ea3616 SA |
246 | void SignalBase::set_conversion_type(ConversionType t) |
247 | { | |
248 | if (conversion_type_ != NoConversion) { | |
27a3f09b | 249 | stop_conversion(); |
12ea3616 SA |
250 | |
251 | // Discard converted data | |
252 | converted_data_.reset(); | |
1b56c646 | 253 | samples_cleared(); |
12ea3616 SA |
254 | } |
255 | ||
256 | conversion_type_ = t; | |
257 | ||
ccccb914 SA |
258 | // Re-create an empty container |
259 | // so that the signal is recognized as providing logic data | |
260 | // and thus can be assigned to a decoder | |
261 | if (conversion_is_a2l()) | |
262 | if (!converted_data_) | |
263 | converted_data_ = make_shared<Logic>(1); // Contains only one channel | |
264 | ||
27a3f09b | 265 | start_conversion(); |
12ea3616 SA |
266 | |
267 | conversion_type_changed(t); | |
cbd2a2de SA |
268 | } |
269 | ||
52c900ac SA |
270 | map<QString, QVariant> SignalBase::get_conversion_options() const |
271 | { | |
272 | return conversion_options_; | |
273 | } | |
274 | ||
275 | bool SignalBase::set_conversion_option(QString key, QVariant value) | |
276 | { | |
277 | QVariant old_value; | |
278 | ||
279 | auto key_iter = conversion_options_.find(key); | |
280 | if (key_iter != conversion_options_.end()) | |
281 | old_value = key_iter->second; | |
282 | ||
283 | conversion_options_[key] = value; | |
284 | ||
285 | return (value != old_value); | |
286 | } | |
287 | ||
288 | vector<double> SignalBase::get_conversion_thresholds(const ConversionType t, | |
289 | const bool always_custom) const | |
290 | { | |
291 | vector<double> result; | |
292 | ConversionType conv_type = t; | |
f0f9c856 | 293 | ConversionPreset preset; |
52c900ac SA |
294 | |
295 | // Use currently active conversion if no conversion type was supplied | |
296 | if (conv_type == NoConversion) | |
297 | conv_type = conversion_type_; | |
298 | ||
299 | if (always_custom) | |
f0f9c856 | 300 | preset = NoPreset; |
52c900ac SA |
301 | else |
302 | preset = get_current_conversion_preset(); | |
303 | ||
b9cdbe03 | 304 | if (conv_type == A2LConversionByThreshold) { |
52c900ac SA |
305 | double thr = 0; |
306 | ||
f0f9c856 | 307 | if (preset == NoPreset) { |
52c900ac SA |
308 | auto thr_iter = conversion_options_.find("threshold_value"); |
309 | if (thr_iter != conversion_options_.end()) | |
310 | thr = (thr_iter->second).toDouble(); | |
311 | } | |
312 | ||
f0f9c856 | 313 | if (preset == DynamicPreset) |
52c900ac SA |
314 | thr = (min_value_ + max_value_) * 0.5; // middle between min and max |
315 | ||
f0f9c856 SA |
316 | if ((int)preset == 1) thr = 0.9; |
317 | if ((int)preset == 2) thr = 1.8; | |
318 | if ((int)preset == 3) thr = 2.5; | |
319 | if ((int)preset == 4) thr = 1.5; | |
52c900ac SA |
320 | |
321 | result.push_back(thr); | |
322 | } | |
323 | ||
324 | if (conv_type == A2LConversionBySchmittTrigger) { | |
325 | double thr_lo = 0, thr_hi = 0; | |
326 | ||
f0f9c856 | 327 | if (preset == NoPreset) { |
52c900ac SA |
328 | auto thr_lo_iter = conversion_options_.find("threshold_value_low"); |
329 | if (thr_lo_iter != conversion_options_.end()) | |
330 | thr_lo = (thr_lo_iter->second).toDouble(); | |
331 | ||
332 | auto thr_hi_iter = conversion_options_.find("threshold_value_high"); | |
333 | if (thr_hi_iter != conversion_options_.end()) | |
334 | thr_hi = (thr_hi_iter->second).toDouble(); | |
335 | } | |
336 | ||
f0f9c856 | 337 | if (preset == DynamicPreset) { |
52c900ac SA |
338 | const double amplitude = max_value_ - min_value_; |
339 | const double center = min_value_ + (amplitude / 2); | |
340 | thr_lo = center - (amplitude * 0.15); // 15% margin | |
341 | thr_hi = center + (amplitude * 0.15); // 15% margin | |
342 | } | |
343 | ||
f0f9c856 SA |
344 | if ((int)preset == 1) { thr_lo = 0.3; thr_hi = 1.2; } |
345 | if ((int)preset == 2) { thr_lo = 0.7; thr_hi = 2.5; } | |
346 | if ((int)preset == 3) { thr_lo = 1.3; thr_hi = 3.7; } | |
347 | if ((int)preset == 4) { thr_lo = 0.8; thr_hi = 2.0; } | |
52c900ac SA |
348 | |
349 | result.push_back(thr_lo); | |
350 | result.push_back(thr_hi); | |
351 | } | |
352 | ||
353 | return result; | |
354 | } | |
355 | ||
356 | vector< pair<QString, int> > SignalBase::get_conversion_presets() const | |
357 | { | |
358 | vector< pair<QString, int> > presets; | |
359 | ||
b9cdbe03 | 360 | if (conversion_type_ == A2LConversionByThreshold) { |
52c900ac SA |
361 | // Source: http://www.interfacebus.com/voltage_threshold.html |
362 | presets.emplace_back(tr("Signal average"), 0); | |
363 | presets.emplace_back(tr("0.9V (for 1.8V CMOS)"), 1); | |
364 | presets.emplace_back(tr("1.8V (for 3.3V CMOS)"), 2); | |
365 | presets.emplace_back(tr("2.5V (for 5.0V CMOS)"), 3); | |
366 | presets.emplace_back(tr("1.5V (for TTL)"), 4); | |
367 | } | |
368 | ||
369 | if (conversion_type_ == A2LConversionBySchmittTrigger) { | |
370 | // Source: http://www.interfacebus.com/voltage_threshold.html | |
371 | presets.emplace_back(tr("Signal average +/- 15%"), 0); | |
372 | presets.emplace_back(tr("0.3V/1.2V (for 1.8V CMOS)"), 1); | |
373 | presets.emplace_back(tr("0.7V/2.5V (for 3.3V CMOS)"), 2); | |
374 | presets.emplace_back(tr("1.3V/3.7V (for 5.0V CMOS)"), 3); | |
375 | presets.emplace_back(tr("0.8V/2.0V (for TTL)"), 4); | |
376 | } | |
377 | ||
378 | return presets; | |
379 | } | |
380 | ||
f0f9c856 | 381 | SignalBase::ConversionPreset SignalBase::get_current_conversion_preset() const |
52c900ac SA |
382 | { |
383 | auto preset = conversion_options_.find("preset"); | |
384 | if (preset != conversion_options_.end()) | |
f0f9c856 | 385 | return (ConversionPreset)((preset->second).toInt()); |
52c900ac | 386 | |
753c8f08 | 387 | return DynamicPreset; |
52c900ac SA |
388 | } |
389 | ||
f0f9c856 | 390 | void SignalBase::set_conversion_preset(ConversionPreset id) |
52c900ac | 391 | { |
f0f9c856 | 392 | conversion_options_["preset"] = (int)id; |
52c900ac SA |
393 | } |
394 | ||
bb7dd726 SA |
395 | #ifdef ENABLE_DECODE |
396 | bool SignalBase::is_decode_signal() const | |
397 | { | |
47747218 | 398 | return (channel_type_ == DecodeChannel); |
bb7dd726 SA |
399 | } |
400 | #endif | |
cbd2a2de | 401 | |
6de38b17 SA |
402 | void SignalBase::save_settings(QSettings &settings) const |
403 | { | |
404 | settings.setValue("name", name()); | |
405 | settings.setValue("enabled", enabled()); | |
406 | settings.setValue("colour", colour()); | |
12ea3616 | 407 | settings.setValue("conversion_type", (int)conversion_type_); |
52c900ac SA |
408 | |
409 | settings.setValue("conv_options", (int)(conversion_options_.size())); | |
410 | int i = 0; | |
411 | for (auto kvp : conversion_options_) { | |
412 | settings.setValue(QString("conv_option%1_key").arg(i), kvp.first); | |
413 | settings.setValue(QString("conv_option%1_value").arg(i), kvp.second); | |
414 | i++; | |
415 | } | |
6de38b17 SA |
416 | } |
417 | ||
418 | void SignalBase::restore_settings(QSettings &settings) | |
419 | { | |
420 | set_name(settings.value("name").toString()); | |
421 | set_enabled(settings.value("enabled").toBool()); | |
422 | set_colour(settings.value("colour").value<QColor>()); | |
12ea3616 | 423 | set_conversion_type((ConversionType)settings.value("conversion_type").toInt()); |
52c900ac SA |
424 | |
425 | int conv_options = settings.value("conv_options").toInt(); | |
426 | ||
427 | if (conv_options) | |
428 | for (int i = 0; i < conv_options; i++) { | |
429 | QString key = settings.value(QString("conv_option%1_key").arg(i)).toString(); | |
430 | QVariant value = settings.value(QString("conv_option%1_value").arg(i)); | |
431 | conversion_options_[key] = value; | |
432 | } | |
12ea3616 SA |
433 | } |
434 | ||
ccccb914 SA |
435 | bool SignalBase::conversion_is_a2l() const |
436 | { | |
437 | return ((channel_type_ == AnalogChannel) && | |
b9cdbe03 | 438 | ((conversion_type_ == A2LConversionByThreshold) || |
ccccb914 SA |
439 | (conversion_type_ == A2LConversionBySchmittTrigger))); |
440 | } | |
441 | ||
63253d72 | 442 | void SignalBase::convert_single_segment(AnalogSegment *asegment, LogicSegment *lsegment) |
12ea3616 | 443 | { |
27a3f09b SA |
444 | uint64_t start_sample, end_sample; |
445 | start_sample = end_sample = 0; | |
12ea3616 | 446 | |
63253d72 SA |
447 | start_sample = lsegment->get_sample_count(); |
448 | end_sample = asegment->get_sample_count(); | |
27a3f09b | 449 | |
63253d72 SA |
450 | if (end_sample > start_sample) { |
451 | tie(min_value_, max_value_) = asegment->get_min_max(); | |
27a3f09b | 452 | |
63253d72 SA |
453 | // Create sigrok::Analog instance |
454 | float *asamples = new float[ConversionBlockSize]; | |
455 | uint8_t *lsamples = new uint8_t[ConversionBlockSize]; | |
27a3f09b | 456 | |
63253d72 SA |
457 | vector<shared_ptr<sigrok::Channel> > channels; |
458 | channels.push_back(channel_); | |
459 | ||
460 | vector<const sigrok::QuantityFlag*> mq_flags; | |
461 | const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE; | |
462 | const sigrok::Unit * const unit = sigrok::Unit::VOLT; | |
463 | ||
464 | shared_ptr<sigrok::Packet> packet = | |
465 | Session::sr_context->create_analog_packet(channels, | |
466 | asamples, ConversionBlockSize, mq, unit, mq_flags); | |
467 | ||
468 | shared_ptr<sigrok::Analog> analog = | |
469 | dynamic_pointer_cast<sigrok::Analog>(packet->payload()); | |
12ea3616 | 470 | |
63253d72 SA |
471 | // Convert |
472 | uint64_t i = start_sample; | |
12ea3616 | 473 | |
63253d72 SA |
474 | if (conversion_type_ == A2LConversionByThreshold) { |
475 | const double threshold = get_conversion_thresholds()[0]; | |
12ea3616 | 476 | |
63253d72 SA |
477 | // Convert as many sample blocks as we can |
478 | while ((end_sample - i) > ConversionBlockSize) { | |
479 | asegment->get_samples(i, i + ConversionBlockSize, asamples); | |
27a3f09b | 480 | |
63253d72 SA |
481 | shared_ptr<sigrok::Logic> logic = |
482 | analog->get_logic_via_threshold(threshold, lsamples); | |
419ec4e1 | 483 | |
63253d72 | 484 | lsegment->append_payload(logic->data_pointer(), logic->data_length()); |
419ec4e1 | 485 | |
63253d72 SA |
486 | samples_added(lsegment, i, i + ConversionBlockSize); |
487 | i += ConversionBlockSize; | |
488 | } | |
419ec4e1 | 489 | |
63253d72 SA |
490 | // Re-create sigrok::Analog and convert remaining samples |
491 | packet = Session::sr_context->create_analog_packet(channels, | |
492 | asamples, end_sample - i, mq, unit, mq_flags); | |
419ec4e1 | 493 | |
63253d72 | 494 | analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload()); |
27a3f09b | 495 | |
63253d72 SA |
496 | asegment->get_samples(i, end_sample, asamples); |
497 | shared_ptr<sigrok::Logic> logic = | |
498 | analog->get_logic_via_threshold(threshold, lsamples); | |
499 | lsegment->append_payload(logic->data_pointer(), logic->data_length()); | |
500 | samples_added(lsegment, i, end_sample); | |
501 | } | |
27a3f09b | 502 | |
63253d72 SA |
503 | if (conversion_type_ == A2LConversionBySchmittTrigger) { |
504 | const vector<double> thresholds = get_conversion_thresholds(); | |
505 | const double lo_thr = thresholds[0]; | |
506 | const double hi_thr = thresholds[1]; | |
27a3f09b | 507 | |
63253d72 | 508 | uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0 |
419ec4e1 | 509 | |
63253d72 SA |
510 | // Convert as many sample blocks as we can |
511 | while ((end_sample - i) > ConversionBlockSize) { | |
512 | asegment->get_samples(i, i + ConversionBlockSize, asamples); | |
419ec4e1 | 513 | |
63253d72 SA |
514 | shared_ptr<sigrok::Logic> logic = |
515 | analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr, | |
516 | &state, lsamples); | |
419ec4e1 | 517 | |
63253d72 | 518 | lsegment->append_payload(logic->data_pointer(), logic->data_length()); |
27a3f09b | 519 | |
63253d72 SA |
520 | samples_added(lsegment, i, i + ConversionBlockSize); |
521 | i += ConversionBlockSize; | |
522 | } | |
419ec4e1 | 523 | |
63253d72 SA |
524 | // Re-create sigrok::Analog and convert remaining samples |
525 | packet = Session::sr_context->create_analog_packet(channels, | |
526 | asamples, end_sample - i, mq, unit, mq_flags); | |
419ec4e1 | 527 | |
63253d72 | 528 | analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload()); |
27a3f09b | 529 | |
63253d72 SA |
530 | asegment->get_samples(i, end_sample, asamples); |
531 | shared_ptr<sigrok::Logic> logic = | |
532 | analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr, | |
533 | &state, lsamples); | |
534 | lsegment->append_payload(logic->data_pointer(), logic->data_length()); | |
535 | samples_added(lsegment, i, end_sample); | |
536 | } | |
537 | ||
538 | // If acquisition is ongoing, start-/endsample may have changed | |
539 | end_sample = asegment->get_sample_count(); | |
540 | ||
541 | delete[] lsamples; | |
542 | delete[] asamples; | |
543 | } | |
544 | } | |
52c900ac | 545 | |
63253d72 SA |
546 | void SignalBase::conversion_thread_proc() |
547 | { | |
548 | shared_ptr<Analog> analog_data; | |
27a3f09b | 549 | |
63253d72 SA |
550 | if (conversion_is_a2l()) { |
551 | analog_data = dynamic_pointer_cast<Analog>(data_); | |
419ec4e1 | 552 | |
2b3dda54 SA |
553 | if (analog_data->analog_segments().size() == 0) { |
554 | unique_lock<mutex> input_lock(conversion_input_mutex_); | |
555 | conversion_input_cond_.wait(input_lock); | |
556 | } | |
419ec4e1 | 557 | |
63253d72 SA |
558 | } else |
559 | // Currently, we only handle A2L conversions | |
560 | return; | |
419ec4e1 | 561 | |
2b3dda54 SA |
562 | // If we had to wait for input data, we may have been notified to terminate |
563 | if (conversion_interrupt_) | |
564 | return; | |
565 | ||
63253d72 | 566 | uint32_t segment_id = 0; |
27a3f09b | 567 | |
63253d72 SA |
568 | AnalogSegment *asegment = analog_data->analog_segments().front().get(); |
569 | assert(asegment); | |
419ec4e1 | 570 | |
63253d72 SA |
571 | const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(converted_data_); |
572 | assert(logic_data); | |
419ec4e1 | 573 | |
63253d72 SA |
574 | // Create the initial logic data segment if needed |
575 | if (logic_data->logic_segments().size() == 0) { | |
576 | shared_ptr<LogicSegment> new_segment = | |
85a70280 | 577 | make_shared<LogicSegment>(*logic_data.get(), 0, 1, asegment->samplerate()); |
63253d72 SA |
578 | logic_data->push_segment(new_segment); |
579 | } | |
27a3f09b | 580 | |
63253d72 SA |
581 | LogicSegment *lsegment = logic_data->logic_segments().front().get(); |
582 | assert(lsegment); | |
b82243f7 | 583 | |
63253d72 SA |
584 | do { |
585 | convert_single_segment(asegment, lsegment); | |
586 | ||
587 | if (analog_data->analog_segments().size() > logic_data->logic_segments().size()) { | |
588 | // There are more segments to process | |
589 | segment_id++; | |
590 | ||
591 | try { | |
592 | asegment = analog_data->analog_segments().at(segment_id).get(); | |
593 | } catch (out_of_range) { | |
594 | qDebug() << "Conversion error for" << name() << ": no analog segment" \ | |
595 | << segment_id << ", segments size is" << analog_data->analog_segments().size(); | |
596 | return; | |
12ea3616 SA |
597 | } |
598 | ||
85a70280 SA |
599 | shared_ptr<LogicSegment> new_segment = make_shared<LogicSegment>( |
600 | *logic_data.get(), segment_id, 1, asegment->samplerate()); | |
63253d72 SA |
601 | logic_data->push_segment(new_segment); |
602 | ||
603 | lsegment = logic_data->logic_segments().back().get(); | |
604 | } else { | |
605 | // No more segments to process, wait for data or interrupt | |
606 | if (!conversion_interrupt_) { | |
607 | unique_lock<mutex> input_lock(conversion_input_mutex_); | |
608 | conversion_input_cond_.wait(input_lock); | |
609 | } | |
12ea3616 | 610 | } |
27a3f09b SA |
611 | } while (!conversion_interrupt_); |
612 | } | |
12ea3616 | 613 | |
932bc246 | 614 | void SignalBase::start_conversion(bool delayed_start) |
27a3f09b | 615 | { |
932bc246 SA |
616 | if (delayed_start) { |
617 | delayed_conversion_starter_.start(); | |
618 | return; | |
619 | } | |
620 | ||
27a3f09b SA |
621 | stop_conversion(); |
622 | ||
52c900ac SA |
623 | if (converted_data_) |
624 | converted_data_->clear(); | |
8d3c0f7a | 625 | samples_cleared(); |
52c900ac | 626 | |
63253d72 SA |
627 | conversion_interrupt_ = false; |
628 | conversion_thread_ = std::thread( | |
629 | &SignalBase::conversion_thread_proc, this); | |
12ea3616 SA |
630 | } |
631 | ||
27a3f09b SA |
632 | void SignalBase::stop_conversion() |
633 | { | |
634 | // Stop conversion so we can restart it from the beginning | |
635 | conversion_interrupt_ = true; | |
636 | conversion_input_cond_.notify_one(); | |
637 | if (conversion_thread_.joinable()) | |
638 | conversion_thread_.join(); | |
639 | } | |
640 | ||
12ea3616 SA |
641 | void SignalBase::on_samples_cleared() |
642 | { | |
643 | if (converted_data_) | |
644 | converted_data_->clear(); | |
eae3bbbb SA |
645 | |
646 | samples_cleared(); | |
12ea3616 SA |
647 | } |
648 | ||
649 | void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample, | |
650 | uint64_t end_sample) | |
651 | { | |
12ea3616 | 652 | if (conversion_type_ != NoConversion) { |
27a3f09b SA |
653 | if (conversion_thread_.joinable()) { |
654 | // Notify the conversion thread since it's running | |
655 | conversion_input_cond_.notify_one(); | |
656 | } else { | |
932bc246 SA |
657 | // Start the conversion thread unless the delay timer is running |
658 | if (!delayed_conversion_starter_.isActive()) | |
659 | start_conversion(); | |
27a3f09b | 660 | } |
12ea3616 | 661 | } |
eae3bbbb SA |
662 | |
663 | samples_added(segment, start_sample, end_sample); | |
12ea3616 SA |
664 | } |
665 | ||
8e15445c SA |
666 | void SignalBase::on_min_max_changed(float min, float max) |
667 | { | |
f0f9c856 | 668 | // Restart conversion if one is enabled and uses a calculated threshold |
8e15445c | 669 | if ((conversion_type_ != NoConversion) && |
f0f9c856 | 670 | (get_current_conversion_preset() == DynamicPreset)) |
932bc246 | 671 | start_conversion(true); |
c6d9cf65 SA |
672 | |
673 | min_max_changed(min, max); | |
8e15445c SA |
674 | } |
675 | ||
12ea3616 SA |
676 | void SignalBase::on_capture_state_changed(int state) |
677 | { | |
27a3f09b | 678 | if (state == Session::Running) { |
8e15445c SA |
679 | // Restart conversion if one is enabled |
680 | if (conversion_type_ != NoConversion) | |
27a3f09b | 681 | start_conversion(); |
12ea3616 | 682 | } |
6de38b17 SA |
683 | } |
684 | ||
932bc246 SA |
685 | void SignalBase::on_delayed_conversion_start() |
686 | { | |
687 | start_conversion(); | |
688 | } | |
689 | ||
bf0edd2b SA |
690 | } // namespace data |
691 | } // namespace pv |