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;
36 using std::unique_lock;
41 const int SignalBase::ColourBGAlpha = 8 * 256 / 100;
42 const uint64_t SignalBase::ConversionBlockSize = 4096;
44 SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) :
46 channel_type_(channel_type),
47 conversion_type_(NoConversion)
50 internal_name_ = QString::fromStdString(channel_->name());
53 SignalBase::~SignalBase()
58 shared_ptr<sigrok::Channel> SignalBase::channel() const
63 QString SignalBase::name() const
65 return (channel_) ? QString::fromStdString(channel_->name()) : name_;
68 QString SignalBase::internal_name() const
70 return internal_name_;
73 void SignalBase::set_name(QString name)
76 channel_->set_name(name.toUtf8().constData());
83 bool SignalBase::enabled() const
85 return (channel_) ? channel_->enabled() : true;
88 void SignalBase::set_enabled(bool value)
91 channel_->set_enabled(value);
92 enabled_changed(value);
96 SignalBase::ChannelType SignalBase::type() const
101 unsigned int SignalBase::index() const
103 return (channel_) ? channel_->index() : 0;
106 unsigned int SignalBase::logic_bit_index() const
108 if (channel_type_ == LogicChannel)
109 return channel_->index();
114 QColor SignalBase::colour() const
119 void SignalBase::set_colour(QColor colour)
124 bgcolour_.setAlpha(ColourBGAlpha);
126 colour_changed(colour);
129 QColor SignalBase::bgcolour() const
134 void SignalBase::set_data(shared_ptr<pv::data::SignalData> data)
137 disconnect(data.get(), SIGNAL(samples_cleared()),
138 this, SLOT(on_samples_cleared()));
139 disconnect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
140 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
146 connect(data.get(), SIGNAL(samples_cleared()),
147 this, SLOT(on_samples_cleared()));
148 connect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
149 this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t)));
153 shared_ptr<data::Analog> SignalBase::analog_data() const
155 shared_ptr<Analog> result = nullptr;
157 if (channel_type_ == AnalogChannel)
158 result = dynamic_pointer_cast<Analog>(data_);
163 shared_ptr<data::Logic> SignalBase::logic_data() const
165 shared_ptr<Logic> result = nullptr;
167 if (channel_type_ == LogicChannel)
168 result = dynamic_pointer_cast<Logic>(data_);
170 if (((conversion_type_ == A2LConversionByTreshold) ||
171 (conversion_type_ == A2LConversionBySchmittTrigger)))
172 result = dynamic_pointer_cast<Logic>(converted_data_);
177 void SignalBase::set_conversion_type(ConversionType t)
179 if (conversion_type_ != NoConversion) {
182 // Discard converted data
183 converted_data_.reset();
187 conversion_type_ = t;
189 // Re-create an empty container
190 // so that the signal is recognized as providing logic data
191 // and thus can be assigned to a decoder
192 if (conversion_is_a2l())
193 if (!converted_data_)
194 converted_data_ = make_shared<Logic>(1); // Contains only one channel
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 bool SignalBase::conversion_is_a2l() const
226 return ((channel_type_ == AnalogChannel) &&
227 ((conversion_type_ == A2LConversionByTreshold) ||
228 (conversion_type_ == A2LConversionBySchmittTrigger)));
231 void SignalBase::conversion_thread_proc(QObject* segment)
233 // TODO Support for multiple segments is missing
235 uint64_t start_sample, end_sample;
236 start_sample = end_sample = 0;
239 if (conversion_is_a2l()) {
241 AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment);
243 const shared_ptr<Logic> logic_data = dynamic_pointer_cast<Logic>(converted_data_);
245 // Create the initial logic data segment if needed
246 if (logic_data->segments().size() == 0) {
247 shared_ptr<LogicSegment> lsegment =
248 make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate());
249 logic_data->push_segment(lsegment);
252 LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get());
254 start_sample = lsegment->get_sample_count();
255 end_sample = asegment->get_sample_count();
257 if (end_sample > start_sample) {
259 tie(min_v, max_v) = asegment->get_min_max();
261 // Create sigrok::Analog instance
262 float *asamples = new float[ConversionBlockSize];
263 uint8_t *lsamples = new uint8_t[ConversionBlockSize];
265 vector<shared_ptr<sigrok::Channel> > channels;
266 channels.push_back(channel_);
268 vector<const sigrok::QuantityFlag*> mq_flags;
269 const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE;
270 const sigrok::Unit * const unit = sigrok::Unit::VOLT;
272 shared_ptr<sigrok::Packet> packet =
273 Session::sr_context->create_analog_packet(channels,
274 asamples, ConversionBlockSize, mq, unit, mq_flags);
276 shared_ptr<sigrok::Analog> analog =
277 dynamic_pointer_cast<sigrok::Analog>(packet->payload());
280 uint64_t i = start_sample;
282 if (conversion_type_ == A2LConversionByTreshold) {
283 const float threshold = (min_v + max_v) * 0.5; // middle between min and max
285 // Convert as many sample blocks as we can
286 while ((end_sample - i) > ConversionBlockSize) {
287 asegment->get_samples(i, i + ConversionBlockSize, asamples);
289 shared_ptr<sigrok::Logic> logic =
290 analog->get_logic_via_threshold(threshold, lsamples);
292 lsegment->append_payload(logic->data_pointer(), logic->data_length());
294 samples_added(lsegment, i, i + ConversionBlockSize);
295 i += ConversionBlockSize;
298 // Re-create sigrok::Analog and convert remaining samples
299 packet = Session::sr_context->create_analog_packet(channels,
300 asamples, end_sample - i, mq, unit, mq_flags);
302 analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
304 asegment->get_samples(i, end_sample, asamples);
305 shared_ptr<sigrok::Logic> logic =
306 analog->get_logic_via_threshold(threshold, lsamples);
307 lsegment->append_payload(logic->data_pointer(), logic->data_length());
308 samples_added(lsegment, i, end_sample);
311 if (conversion_type_ == A2LConversionBySchmittTrigger) {
312 const float amplitude = max_v - min_v;
313 const float center = min_v + (amplitude / 2);
314 const float lo_thr = center - (amplitude * 0.15); // 15% margin
315 const float hi_thr = center + (amplitude * 0.15); // 15% margin
316 uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0
318 // Convert as many sample blocks as we can
319 while ((end_sample - i) > ConversionBlockSize) {
320 asegment->get_samples(i, i + ConversionBlockSize, asamples);
322 shared_ptr<sigrok::Logic> logic =
323 analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
326 lsegment->append_payload(logic->data_pointer(), logic->data_length());
328 samples_added(lsegment, i, i + ConversionBlockSize);
329 i += ConversionBlockSize;
332 // Re-create sigrok::Analog and convert remaining samples
333 packet = Session::sr_context->create_analog_packet(channels,
334 asamples, end_sample - i, mq, unit, mq_flags);
336 analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
338 asegment->get_samples(i, end_sample, asamples);
339 shared_ptr<sigrok::Logic> logic =
340 analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
342 lsegment->append_payload(logic->data_pointer(), logic->data_length());
343 samples_added(lsegment, i, end_sample);
346 // If acquisition is ongoing, start-/endsample may have changed
347 end_sample = asegment->get_sample_count();
354 if (!conversion_interrupt_ && (start_sample == end_sample)) {
355 unique_lock<mutex> input_lock(conversion_input_mutex_);
356 conversion_input_cond_.wait(input_lock);
358 } while (!conversion_interrupt_);
361 void SignalBase::start_conversion()
365 if (conversion_is_a2l()) {
366 shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_);
368 if (analog_data->analog_segments().size() > 0) {
369 // TODO Support for multiple segments is missing
370 AnalogSegment *asegment = analog_data->analog_segments().front().get();
372 conversion_interrupt_ = false;
373 conversion_thread_ = std::thread(
374 &SignalBase::conversion_thread_proc, this, asegment);
379 void SignalBase::stop_conversion()
381 // Stop conversion so we can restart it from the beginning
382 conversion_interrupt_ = true;
383 conversion_input_cond_.notify_one();
384 if (conversion_thread_.joinable())
385 conversion_thread_.join();
388 void SignalBase::on_samples_cleared()
391 converted_data_->clear();
396 void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample,
399 if (conversion_type_ != NoConversion) {
400 if (conversion_thread_.joinable()) {
401 // Notify the conversion thread since it's running
402 conversion_input_cond_.notify_one();
404 // Start the conversion thread
409 samples_added(segment, start_sample, end_sample);
412 void SignalBase::on_capture_state_changed(int state)
414 if (state == Session::Running) {
415 if (conversion_type_ != NoConversion) {
416 // Restart conversion