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/>.
21 #include "logicsegment.hpp"
22 #include "decodesignal.hpp"
23 #include "signaldata.hpp"
25 #include <pv/binding/decoder.hpp>
26 #include <pv/data/decode/decoder.hpp>
27 #include <pv/data/decode/row.hpp>
28 #include <pv/data/decoderstack.hpp>
29 #include <pv/session.hpp>
31 using std::make_shared;
32 using std::shared_ptr;
33 using pv::data::decode::Decoder;
34 using pv::data::decode::Row;
39 DecodeSignal::DecodeSignal(shared_ptr<pv::data::DecoderStack> decoder_stack,
40 const unordered_set< shared_ptr<data::SignalBase> > &all_signals) :
41 SignalBase(nullptr, SignalBase::DecodeChannel),
42 decoder_stack_(decoder_stack),
43 all_signals_(all_signals)
45 set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
47 update_channel_list();
48 auto_assign_signals();
50 connect(decoder_stack_.get(), SIGNAL(new_annotations()),
51 this, SLOT(on_new_annotations()));
54 DecodeSignal::~DecodeSignal()
58 bool DecodeSignal::is_decode_signal() const
63 shared_ptr<pv::data::DecoderStack> DecodeSignal::decoder_stack() const
65 return decoder_stack_;
68 const list< shared_ptr<Decoder> >& DecodeSignal::decoder_stack_list() const
70 return decoder_stack_->stack();
73 void DecodeSignal::stack_decoder(srd_decoder *decoder)
76 assert(decoder_stack);
77 decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
79 // Include the newly created decode channels in the channel list
80 update_channel_list();
82 auto_assign_signals();
83 decoder_stack_->begin_decode();
86 void DecodeSignal::remove_decoder(int index)
88 decoder_stack_->remove(index);
89 update_channel_list();
90 decoder_stack_->begin_decode();
93 bool DecodeSignal::toggle_decoder_visibility(int index)
95 const list< shared_ptr<Decoder> > stack(decoder_stack_->stack());
97 auto iter = stack.cbegin();
98 for (int i = 0; i < index; i++, iter++)
99 assert(iter != stack.end());
101 shared_ptr<Decoder> dec = *iter;
103 // Toggle decoder visibility
106 state = !dec->shown();
113 QString DecodeSignal::error_message() const
115 return decoder_stack_->error_message();
118 const list<data::DecodeChannel> DecodeSignal::get_channels() const
123 void DecodeSignal::auto_assign_signals()
125 // Try to auto-select channels that don't have signals assigned yet
126 for (data::DecodeChannel &ch : channels_) {
127 if (ch.assigned_signal)
130 for (shared_ptr<data::SignalBase> s : all_signals_)
131 if (s->logic_data() && (ch.name.toLower().contains(s->name().toLower())))
132 ch.assigned_signal = s.get();
136 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
138 for (data::DecodeChannel &ch : channels_)
139 if (ch.id == channel_id)
140 ch.assigned_signal = signal;
144 decoder_stack_->begin_decode();
147 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
149 for (data::DecodeChannel &ch : channels_)
150 if (ch.id == channel_id)
151 ch.initial_pin_state = init_state;
155 decoder_stack_->begin_decode();
158 vector<Row> DecodeSignal::visible_rows() const
160 return decoder_stack_->get_visible_rows();
163 void DecodeSignal::get_annotation_subset(
164 vector<pv::data::decode::Annotation> &dest,
165 const decode::Row &row, uint64_t start_sample,
166 uint64_t end_sample) const
168 return decoder_stack_->get_annotation_subset(dest, row,
169 start_sample, end_sample);
172 void DecodeSignal::update_channel_list()
174 list<data::DecodeChannel> prev_channels = channels_;
179 // Copy existing entries, create new as needed
180 for (shared_ptr<Decoder> decoder : decoder_stack_->stack()) {
181 const srd_decoder* srd_d = decoder->decoder();
184 // Mandatory channels
185 for (l = srd_d->channels; l; l = l->next) {
186 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
187 bool ch_added = false;
189 // Copy but update ID if this channel was in the list before
190 for (data::DecodeChannel ch : prev_channels)
191 if (ch.pdch_ == pdch) {
193 channels_.push_back(ch);
199 // Create new entry without a mapped signal
200 data::DecodeChannel ch = {id++, false, nullptr,
201 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
202 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
203 channels_.push_back(ch);
208 for (l = srd_d->opt_channels; l; l = l->next) {
209 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
210 bool ch_added = false;
212 // Copy but update ID if this channel was in the list before
213 for (data::DecodeChannel ch : prev_channels)
214 if (ch.pdch_ == pdch) {
216 channels_.push_back(ch);
222 // Create new entry without a mapped signal
223 data::DecodeChannel ch = {id++, true, nullptr,
224 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
225 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
226 channels_.push_back(ch);
234 void DecodeSignal::on_new_annotations()
236 // Forward the signal to the frontend