]> sigrok.org Git - pulseview.git/blame - pv/data/signalbase.cpp
Merge DecoderStack into DecodeSignal
[pulseview.git] / pv / data / signalbase.cpp
CommitLineData
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
SA
28
29#include <pv/binding/decoder.hpp>
aca9aa83 30#include <pv/session.hpp>
bf0edd2b 31
cbd2a2de 32using std::dynamic_pointer_cast;
12ea3616 33using std::make_shared;
bf0edd2b 34using std::shared_ptr;
12ea3616 35using std::tie;
bf0edd2b
SA
36
37namespace pv {
38namespace data {
39
c063290a 40const int SignalBase::ColourBGAlpha = 8 * 256 / 100;
bf0edd2b 41
472a80c5
SA
42SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) :
43 channel_(channel),
12ea3616
SA
44 channel_type_(channel_type),
45 conversion_type_(NoConversion)
bf0edd2b 46{
050b5a6c
SA
47 if (channel_)
48 internal_name_ = QString::fromStdString(channel_->name());
bf0edd2b
SA
49}
50
12ea3616
SA
51SignalBase::~SignalBase()
52{
53 // Wait for the currently ongoing conversion to finish
54 if (conversion_thread_.joinable())
55 conversion_thread_.join();
56}
57
bf0edd2b
SA
58shared_ptr<sigrok::Channel> SignalBase::channel() const
59{
60 return channel_;
61}
62
63QString SignalBase::name() const
64{
65 return (channel_) ? QString::fromStdString(channel_->name()) : name_;
66}
67
050b5a6c
SA
68QString SignalBase::internal_name() const
69{
70 return internal_name_;
71}
72
bf0edd2b
SA
73void SignalBase::set_name(QString name)
74{
75 if (channel_)
76 channel_->set_name(name.toUtf8().constData());
77
78 name_ = name;
79
80 name_changed(name);
81}
82
83bool SignalBase::enabled() const
84{
85 return (channel_) ? channel_->enabled() : true;
86}
87
88void SignalBase::set_enabled(bool value)
89{
90 if (channel_) {
91 channel_->set_enabled(value);
92 enabled_changed(value);
93 }
94}
95
472a80c5 96SignalBase::ChannelType SignalBase::type() const
bf0edd2b 97{
472a80c5 98 return channel_type_;
bf0edd2b
SA
99}
100
101unsigned int SignalBase::index() const
102{
103 return (channel_) ? channel_->index() : (unsigned int)-1;
104}
105
106QColor SignalBase::colour() const
107{
108 return colour_;
109}
110
111void SignalBase::set_colour(QColor colour)
112{
113 colour_ = colour;
114
115 bgcolour_ = colour;
116 bgcolour_.setAlpha(ColourBGAlpha);
117
118 colour_changed(colour);
119}
120
121QColor SignalBase::bgcolour() const
122{
123 return bgcolour_;
124}
125
cbd2a2de
SA
126void SignalBase::set_data(shared_ptr<pv::data::SignalData> data)
127{
8ce8ebb9
SA
128 if (data_) {
129 disconnect(data.get(), SIGNAL(samples_cleared()),
12ea3616 130 this, SLOT(on_samples_cleared()));
8ce8ebb9 131 disconnect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
12ea3616
SA
132 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
133 }
134
cbd2a2de 135 data_ = data;
12ea3616 136
8ce8ebb9
SA
137 if (data_) {
138 connect(data.get(), SIGNAL(samples_cleared()),
12ea3616 139 this, SLOT(on_samples_cleared()));
8ce8ebb9 140 connect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
12ea3616
SA
141 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
142 }
cbd2a2de
SA
143}
144
145shared_ptr<data::Analog> SignalBase::analog_data() const
146{
12ea3616
SA
147 shared_ptr<Analog> result = nullptr;
148
472a80c5 149 if (channel_type_ == AnalogChannel)
12ea3616
SA
150 result = dynamic_pointer_cast<Analog>(data_);
151
152 return result;
cbd2a2de
SA
153}
154
155shared_ptr<data::Logic> SignalBase::logic_data() const
156{
12ea3616
SA
157 shared_ptr<Logic> result = nullptr;
158
472a80c5 159 if (channel_type_ == LogicChannel)
12ea3616
SA
160 result = dynamic_pointer_cast<Logic>(data_);
161
162 if (((conversion_type_ == A2LConversionByTreshold) ||
163 (conversion_type_ == A2LConversionBySchmittTrigger)))
164 result = dynamic_pointer_cast<Logic>(converted_data_);
165
166 return result;
167}
168
169void SignalBase::set_conversion_type(ConversionType t)
170{
171 if (conversion_type_ != NoConversion) {
172 // Wait for the currently ongoing conversion to finish
173 if (conversion_thread_.joinable())
174 conversion_thread_.join();
175
176 // Discard converted data
177 converted_data_.reset();
178 }
179
180 conversion_type_ = t;
181
182 if ((channel_type_ == AnalogChannel) &&
183 ((conversion_type_ == A2LConversionByTreshold) ||
184 (conversion_type_ == A2LConversionBySchmittTrigger))) {
185
186 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
187
188 if (analog_data->analog_segments().size() > 0) {
189 AnalogSegment *asegment = analog_data->analog_segments().front().get();
190
191 // Begin conversion of existing sample data
192 // TODO Support for multiple segments is missing
193 on_samples_added(asegment, 0, 0);
194 }
195 }
196
197 conversion_type_changed(t);
cbd2a2de
SA
198}
199
bb7dd726
SA
200#ifdef ENABLE_DECODE
201bool SignalBase::is_decode_signal() const
202{
47747218 203 return (channel_type_ == DecodeChannel);
bb7dd726
SA
204}
205#endif
cbd2a2de 206
6de38b17
SA
207void SignalBase::save_settings(QSettings &settings) const
208{
209 settings.setValue("name", name());
210 settings.setValue("enabled", enabled());
211 settings.setValue("colour", colour());
12ea3616 212 settings.setValue("conversion_type", (int)conversion_type_);
6de38b17
SA
213}
214
215void SignalBase::restore_settings(QSettings &settings)
216{
217 set_name(settings.value("name").toString());
218 set_enabled(settings.value("enabled").toBool());
219 set_colour(settings.value("colour").value<QColor>());
12ea3616
SA
220 set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
221}
222
223uint8_t SignalBase::convert_a2l_threshold(float threshold, float value)
224{
225 return (value >= threshold) ? 1 : 0;
226}
227
5d9fe823
SA
228uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
229 float value, uint8_t &state)
12ea3616 230{
12ea3616
SA
231 if (value < lo_thr)
232 state = 0;
233 else if (value > hi_thr)
234 state = 1;
235
236 return state;
237}
238
239void SignalBase::conversion_thread_proc(QObject* segment, uint64_t start_sample,
240 uint64_t end_sample)
241{
242 const uint64_t block_size = 4096;
243
244 // TODO Support for multiple segments is missing
245
246 if ((channel_type_ == AnalogChannel) &&
247 ((conversion_type_ == A2LConversionByTreshold) ||
248 (conversion_type_ == A2LConversionBySchmittTrigger))) {
249
250 AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
251
252 // Create the logic data container if needed
253 shared_ptr<Logic> logic_data;
254 if (!converted_data_) {
255 logic_data = make_shared<Logic>(1); // Contains only one channel
256 converted_data_ = logic_data;
257 } else
258 logic_data = dynamic_pointer_cast<Logic>(converted_data_);
259
260 // Create the initial logic data segment if needed
261 if (logic_data->segments().size() == 0) {
262 shared_ptr<LogicSegment> lsegment =
263 make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
264 logic_data->push_segment(lsegment);
265 }
266
267 LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
268
269 // start_sample=end_sample=0 means we need to figure out the unprocessed range
270 if ((start_sample == 0) && (end_sample == 0)) {
271 start_sample = lsegment->get_sample_count();
272 end_sample = asegment->get_sample_count();
273 }
274
275 if (start_sample == end_sample)
276 return; // Nothing to do
277
278 float min_v, max_v;
279 tie(min_v, max_v) = asegment->get_min_max();
280
281 vector<uint8_t> lsamples;
282 lsamples.reserve(block_size);
283
284 uint64_t i = start_sample;
285
286 if (conversion_type_ == A2LConversionByTreshold) {
287 const float threshold = (min_v + max_v) * 0.5; // middle between min and max
288
289 // Convert as many sample blocks as we can
290 while ((end_sample - i) > block_size) {
291 const float* asamples = asegment->get_samples(i, i + block_size);
292 for (uint32_t j = 0; j < block_size; j++)
293 lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
294 lsegment->append_payload(lsamples.data(), lsamples.size());
295 i += block_size;
296 lsamples.clear();
297 delete[] asamples;
298 }
299
300 // Convert remaining samples
301 const float* asamples = asegment->get_samples(i, end_sample);
302 for (uint32_t j = 0; j < (end_sample - i); j++)
303 lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
304 lsegment->append_payload(lsamples.data(), lsamples.size());
305 delete[] asamples;
eae3bbbb
SA
306
307 samples_added(lsegment, start_sample, end_sample);
12ea3616
SA
308 }
309
310 if (conversion_type_ == A2LConversionBySchmittTrigger) {
311 const float amplitude = max_v - min_v;
312 const float lo_thr = min_v + (amplitude * 0.1); // 10% above min
313 const float hi_thr = max_v - (amplitude * 0.1); // 10% below max
5d9fe823 314 uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0
12ea3616
SA
315
316 // Convert as many sample blocks as we can
317 while ((end_sample - i) > block_size) {
318 const float* asamples = asegment->get_samples(i, i + block_size);
319 for (uint32_t j = 0; j < block_size; j++)
5d9fe823 320 lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
12ea3616
SA
321 lsegment->append_payload(lsamples.data(), lsamples.size());
322 i += block_size;
323 lsamples.clear();
324 delete[] asamples;
325 }
326
327 // Convert remaining samples
328 const float* asamples = asegment->get_samples(i, end_sample);
329 for (uint32_t j = 0; j < (end_sample - i); j++)
5d9fe823 330 lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
12ea3616
SA
331 lsegment->append_payload(lsamples.data(), lsamples.size());
332 delete[] asamples;
eae3bbbb
SA
333
334 samples_added(lsegment, start_sample, end_sample);
12ea3616
SA
335 }
336 }
337}
338
339void SignalBase::on_samples_cleared()
340{
341 if (converted_data_)
342 converted_data_->clear();
eae3bbbb
SA
343
344 samples_cleared();
12ea3616
SA
345}
346
347void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
348 uint64_t end_sample)
349{
12ea3616
SA
350 if (conversion_type_ != NoConversion) {
351
352 // Wait for the currently ongoing conversion to finish
353 if (conversion_thread_.joinable())
354 conversion_thread_.join();
355
356 conversion_thread_ = std::thread(
357 &SignalBase::conversion_thread_proc, this,
358 segment, start_sample, end_sample);
359 }
eae3bbbb
SA
360
361 samples_added(segment, start_sample, end_sample);
12ea3616
SA
362}
363
364void SignalBase::on_capture_state_changed(int state)
365{
366 return;
367 if (state == Session::Stopped) {
368 // Make sure that all data is converted
369
370 if ((channel_type_ == AnalogChannel) &&
371 ((conversion_type_ == A2LConversionByTreshold) ||
372 (conversion_type_ == A2LConversionBySchmittTrigger))) {
373
374 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
375
376 if (analog_data->analog_segments().size() > 0) {
377 // TODO Support for multiple segments is missing
378 AnalogSegment *asegment = analog_data->analog_segments().front().get();
379
380 if (conversion_thread_.joinable())
381 conversion_thread_.join();
382
383 conversion_thread_ = std::thread(
384 &SignalBase::conversion_thread_proc, this, asegment, 0, 0);
385 }
386 }
387 }
6de38b17
SA
388}
389
bf0edd2b
SA
390} // namespace data
391} // namespace pv