2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "analogsegment.hpp"
23 #include "decode/row.hpp"
25 #include "logicsegment.hpp"
26 #include "signalbase.hpp"
27 #include "signaldata.hpp"
29 #include <pv/binding/decoder.hpp>
30 #include <pv/session.hpp>
32 using std::dynamic_pointer_cast;
33 using std::make_shared;
34 using std::shared_ptr;
40 const int SignalBase::ColourBGAlpha = 8 * 256 / 100;
41 const uint64_t SignalBase::ConversionBlockSize = 4096;
43 SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) :
45 channel_type_(channel_type),
46 conversion_type_(NoConversion)
49 internal_name_ = QString::fromStdString(channel_->name());
52 SignalBase::~SignalBase()
54 // Wait for the currently ongoing conversion to finish
55 if (conversion_thread_.joinable())
56 conversion_thread_.join();
59 shared_ptr<sigrok::Channel> SignalBase::channel() const
64 QString SignalBase::name() const
66 return (channel_) ? QString::fromStdString(channel_->name()) : name_;
69 QString SignalBase::internal_name() const
71 return internal_name_;
74 void SignalBase::set_name(QString name)
77 channel_->set_name(name.toUtf8().constData());
84 bool SignalBase::enabled() const
86 return (channel_) ? channel_->enabled() : true;
89 void SignalBase::set_enabled(bool value)
92 channel_->set_enabled(value);
93 enabled_changed(value);
97 SignalBase::ChannelType SignalBase::type() const
102 unsigned int SignalBase::index() const
104 return (channel_) ? channel_->index() : (unsigned int)-1;
107 QColor SignalBase::colour() const
112 void SignalBase::set_colour(QColor colour)
117 bgcolour_.setAlpha(ColourBGAlpha);
119 colour_changed(colour);
122 QColor SignalBase::bgcolour() const
127 void SignalBase::set_data(shared_ptr<pv::data::SignalData> data)
130 disconnect(data.get(), SIGNAL(samples_cleared()),
131 this, SLOT(on_samples_cleared()));
132 disconnect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
133 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
139 connect(data.get(), SIGNAL(samples_cleared()),
140 this, SLOT(on_samples_cleared()));
141 connect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
142 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
146 shared_ptr<data::Analog> SignalBase::analog_data() const
148 shared_ptr<Analog> result = nullptr;
150 if (channel_type_ == AnalogChannel)
151 result = dynamic_pointer_cast<Analog>(data_);
156 shared_ptr<data::Logic> SignalBase::logic_data() const
158 shared_ptr<Logic> result = nullptr;
160 if (channel_type_ == LogicChannel)
161 result = dynamic_pointer_cast<Logic>(data_);
163 if (((conversion_type_ == A2LConversionByTreshold) ||
164 (conversion_type_ == A2LConversionBySchmittTrigger)))
165 result = dynamic_pointer_cast<Logic>(converted_data_);
170 void SignalBase::set_conversion_type(ConversionType t)
172 if (conversion_type_ != NoConversion) {
173 // Wait for the currently ongoing conversion to finish
174 if (conversion_thread_.joinable())
175 conversion_thread_.join();
177 // Discard converted data
178 converted_data_.reset();
181 conversion_type_ = t;
183 if ((channel_type_ == AnalogChannel) &&
184 ((conversion_type_ == A2LConversionByTreshold) ||
185 (conversion_type_ == A2LConversionBySchmittTrigger))) {
187 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
189 if (analog_data->analog_segments().size() > 0) {
190 AnalogSegment *asegment = analog_data->analog_segments().front().get();
192 // Begin conversion of existing sample data
193 // TODO Support for multiple segments is missing
194 on_samples_added(asegment, 0, 0);
198 conversion_type_changed(t);
202 bool SignalBase::is_decode_signal() const
204 return (channel_type_ == DecodeChannel);
208 void SignalBase::save_settings(QSettings &settings) const
210 settings.setValue("name", name());
211 settings.setValue("enabled", enabled());
212 settings.setValue("colour", colour());
213 settings.setValue("conversion_type", (int)conversion_type_);
216 void SignalBase::restore_settings(QSettings &settings)
218 set_name(settings.value("name").toString());
219 set_enabled(settings.value("enabled").toBool());
220 set_colour(settings.value("colour").value<QColor>());
221 set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
224 uint8_t SignalBase::convert_a2l_threshold(float threshold, float value)
226 return (value >= threshold) ? 1 : 0;
229 uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
230 float value, uint8_t &state)
234 else if (value > hi_thr)
240 void SignalBase::conversion_thread_proc(QObject* segment, uint64_t start_sample,
243 // TODO Support for multiple segments is missing
245 if ((channel_type_ == AnalogChannel) &&
246 ((conversion_type_ == A2LConversionByTreshold) ||
247 (conversion_type_ == A2LConversionBySchmittTrigger))) {
249 AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
251 // Create the logic data container if needed
252 shared_ptr<Logic> logic_data;
253 if (!converted_data_) {
254 logic_data = make_shared<Logic>(1); // Contains only one channel
255 converted_data_ = logic_data;
257 logic_data = dynamic_pointer_cast<Logic>(converted_data_);
259 // Create the initial logic data segment if needed
260 if (logic_data->segments().size() == 0) {
261 shared_ptr<LogicSegment> lsegment =
262 make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
263 logic_data->push_segment(lsegment);
266 LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
268 // start_sample=end_sample=0 means we need to figure out the unprocessed range
269 if ((start_sample == 0) && (end_sample == 0)) {
270 start_sample = lsegment->get_sample_count();
271 end_sample = asegment->get_sample_count();
274 if (start_sample == end_sample)
275 return; // Nothing to do
278 tie(min_v, max_v) = asegment->get_min_max();
280 vector<uint8_t> lsamples;
281 lsamples.reserve(ConversionBlockSize);
283 uint64_t i = start_sample;
285 if (conversion_type_ == A2LConversionByTreshold) {
286 const float threshold = (min_v + max_v) * 0.5; // middle between min and max
288 // Convert as many sample blocks as we can
289 while ((end_sample - i) > ConversionBlockSize) {
290 const float* asamples = asegment->get_samples(i, i + ConversionBlockSize);
291 for (uint32_t j = 0; j < ConversionBlockSize; j++)
292 lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
293 lsegment->append_payload(lsamples.data(), lsamples.size());
294 i += ConversionBlockSize;
299 // Convert remaining samples
300 const float* asamples = asegment->get_samples(i, end_sample);
301 for (uint32_t j = 0; j < (end_sample - i); j++)
302 lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
303 lsegment->append_payload(lsamples.data(), lsamples.size());
306 samples_added(lsegment, start_sample, end_sample);
309 if (conversion_type_ == A2LConversionBySchmittTrigger) {
310 const float amplitude = max_v - min_v;
311 const float lo_thr = min_v + (amplitude * 0.1); // 10% above min
312 const float hi_thr = max_v - (amplitude * 0.1); // 10% below max
313 uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0
315 // Convert as many sample blocks as we can
316 while ((end_sample - i) > ConversionBlockSize) {
317 const float* asamples = asegment->get_samples(i, i + ConversionBlockSize);
318 for (uint32_t j = 0; j < ConversionBlockSize; j++)
319 lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
320 lsegment->append_payload(lsamples.data(), lsamples.size());
321 i += ConversionBlockSize;
326 // Convert remaining samples
327 const float* asamples = asegment->get_samples(i, end_sample);
328 for (uint32_t j = 0; j < (end_sample - i); j++)
329 lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
330 lsegment->append_payload(lsamples.data(), lsamples.size());
333 samples_added(lsegment, start_sample, end_sample);
338 void SignalBase::on_samples_cleared()
341 converted_data_->clear();
346 void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
349 if (conversion_type_ != NoConversion) {
351 // Wait for the currently ongoing conversion to finish
352 if (conversion_thread_.joinable())
353 conversion_thread_.join();
355 conversion_thread_ = std::thread(
356 &SignalBase::conversion_thread_proc, this,
357 segment, start_sample, end_sample);
360 samples_added(segment, start_sample, end_sample);
363 void SignalBase::on_capture_state_changed(int state)
366 if (state == Session::Stopped) {
367 // Make sure that all data is converted
369 if ((channel_type_ == AnalogChannel) &&
370 ((conversion_type_ == A2LConversionByTreshold) ||
371 (conversion_type_ == A2LConversionBySchmittTrigger))) {
373 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
375 if (analog_data->analog_segments().size() > 0) {
376 // TODO Support for multiple segments is missing
377 AnalogSegment *asegment = analog_data->analog_segments().front().get();
379 if (conversion_thread_.joinable())
380 conversion_thread_.join();
382 conversion_thread_ = std::thread(
383 &SignalBase::conversion_thread_proc, this, asegment, 0, 0);