2 * This file is part of the PulseView project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "logicsegment.hpp"
24 #include "decodesignal.hpp"
25 #include "signaldata.hpp"
27 #include <pv/binding/decoder.hpp>
28 #include <pv/data/decode/decoder.hpp>
29 #include <pv/data/decode/row.hpp>
30 #include <pv/data/decoderstack.hpp>
31 #include <pv/session.hpp>
33 using std::make_shared;
35 using std::shared_ptr;
36 using pv::data::decode::Decoder;
37 using pv::data::decode::Row;
42 DecodeSignal::DecodeSignal(shared_ptr<pv::data::DecoderStack> decoder_stack,
43 const unordered_set< shared_ptr<data::SignalBase> > &all_signals) :
44 SignalBase(nullptr, SignalBase::DecodeChannel),
45 decoder_stack_(decoder_stack),
46 all_signals_(all_signals)
48 set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
50 update_channel_list();
51 auto_assign_signals();
53 connect(decoder_stack_.get(), SIGNAL(new_annotations()),
54 this, SLOT(on_new_annotations()));
57 DecodeSignal::~DecodeSignal()
61 bool DecodeSignal::is_decode_signal() const
66 shared_ptr<pv::data::DecoderStack> DecodeSignal::decoder_stack() const
68 return decoder_stack_;
71 const list< shared_ptr<Decoder> >& DecodeSignal::decoder_stack_list() const
73 return decoder_stack_->stack();
76 void DecodeSignal::stack_decoder(srd_decoder *decoder)
79 assert(decoder_stack);
80 decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
82 // Include the newly created decode channels in the channel list
83 update_channel_list();
85 auto_assign_signals();
86 decoder_stack_->begin_decode();
89 void DecodeSignal::remove_decoder(int index)
91 decoder_stack_->remove(index);
92 update_channel_list();
93 decoder_stack_->begin_decode();
96 bool DecodeSignal::toggle_decoder_visibility(int index)
98 const list< shared_ptr<Decoder> > stack(decoder_stack_->stack());
100 auto iter = stack.cbegin();
101 for (int i = 0; i < index; i++, iter++)
102 assert(iter != stack.end());
104 shared_ptr<Decoder> dec = *iter;
106 // Toggle decoder visibility
109 state = !dec->shown();
116 void DecodeSignal::begin_decode()
118 decoder_stack_->begin_decode();
121 QString DecodeSignal::error_message() const
123 return decoder_stack_->error_message();
126 const list<data::DecodeChannel> DecodeSignal::get_channels() const
131 void DecodeSignal::auto_assign_signals()
133 // Try to auto-select channels that don't have signals assigned yet
134 for (data::DecodeChannel &ch : channels_) {
135 if (ch.assigned_signal)
138 for (shared_ptr<data::SignalBase> s : all_signals_)
139 if (s->logic_data() && (ch.name.toLower().contains(s->name().toLower())))
140 ch.assigned_signal = s.get();
144 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
146 for (data::DecodeChannel &ch : channels_)
147 if (ch.id == channel_id)
148 ch.assigned_signal = signal;
152 decoder_stack_->begin_decode();
155 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
157 for (data::DecodeChannel &ch : channels_)
158 if (ch.id == channel_id)
159 ch.initial_pin_state = init_state;
163 decoder_stack_->begin_decode();
166 double DecodeSignal::samplerate() const
168 return decoder_stack_->samplerate();
171 const pv::util::Timestamp& DecodeSignal::start_time() const
173 return decoder_stack_->start_time();
176 int64_t DecodeSignal::get_working_sample_count() const
178 // The working sample count is the highest sample number for
179 // which all used signals have data available, so go through
180 // all channels and use the lowest overall sample count of the
183 // TODO Currently, we assume only a single segment exists
185 int64_t count = std::numeric_limits<int64_t>::max();
186 bool no_signals_assigned = true;
188 for (const data::DecodeChannel &ch : channels_)
189 if (ch.assigned_signal) {
190 no_signals_assigned = false;
192 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
193 if (!logic_data || logic_data->logic_segments().empty())
196 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
197 count = min(count, (int64_t)segment->get_sample_count());
200 return (no_signals_assigned ? 0 : count);
203 int64_t DecodeSignal::get_decoded_sample_count() const
205 return decoder_stack_->samples_decoded();
208 vector<Row> DecodeSignal::visible_rows() const
210 return decoder_stack_->get_visible_rows();
213 void DecodeSignal::get_annotation_subset(
214 vector<pv::data::decode::Annotation> &dest,
215 const decode::Row &row, uint64_t start_sample,
216 uint64_t end_sample) const
218 return decoder_stack_->get_annotation_subset(dest, row,
219 start_sample, end_sample);
222 void DecodeSignal::update_channel_list()
224 list<data::DecodeChannel> prev_channels = channels_;
229 // Copy existing entries, create new as needed
230 for (shared_ptr<Decoder> decoder : decoder_stack_->stack()) {
231 const srd_decoder* srd_d = decoder->decoder();
234 // Mandatory channels
235 for (l = srd_d->channels; l; l = l->next) {
236 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
237 bool ch_added = false;
239 // Copy but update ID if this channel was in the list before
240 for (data::DecodeChannel ch : prev_channels)
241 if (ch.pdch_ == pdch) {
243 channels_.push_back(ch);
249 // Create new entry without a mapped signal
250 data::DecodeChannel ch = {id++, false, nullptr,
251 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
252 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
253 channels_.push_back(ch);
258 for (l = srd_d->opt_channels; l; l = l->next) {
259 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
260 bool ch_added = false;
262 // Copy but update ID if this channel was in the list before
263 for (data::DecodeChannel ch : prev_channels)
264 if (ch.pdch_ == pdch) {
266 channels_.push_back(ch);
272 // Create new entry without a mapped signal
273 data::DecodeChannel ch = {id++, true, nullptr,
274 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
275 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
276 channels_.push_back(ch);
284 void DecodeSignal::on_new_annotations()
286 // Forward the signal to the frontend