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 SignalBase(nullptr, SignalBase::DecodeChannel),
41 decoder_stack_(decoder_stack)
43 set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
45 update_channel_list();
47 connect(decoder_stack_.get(), SIGNAL(new_annotations()),
48 this, SLOT(on_new_annotations()));
51 DecodeSignal::~DecodeSignal()
55 bool DecodeSignal::is_decode_signal() const
60 shared_ptr<pv::data::DecoderStack> DecodeSignal::decoder_stack() const
62 return decoder_stack_;
65 const list< shared_ptr<Decoder> >& DecodeSignal::decoder_stack_list() const
67 return decoder_stack_->stack();
70 void DecodeSignal::stack_decoder(srd_decoder *decoder)
73 assert(decoder_stack);
74 decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
75 update_channel_list();
76 decoder_stack_->begin_decode();
79 void DecodeSignal::remove_decoder(int index)
81 decoder_stack_->remove(index);
82 update_channel_list();
83 decoder_stack_->begin_decode();
86 bool DecodeSignal::toggle_decoder_visibility(int index)
88 const list< shared_ptr<Decoder> > stack(decoder_stack_->stack());
90 auto iter = stack.cbegin();
91 for (int i = 0; i < index; i++, iter++)
92 assert(iter != stack.end());
94 shared_ptr<Decoder> dec = *iter;
96 // Toggle decoder visibility
99 state = !dec->shown();
106 QString DecodeSignal::error_message() const
108 return decoder_stack_->error_message();
111 const list<data::DecodeChannel> DecodeSignal::get_channels() const
116 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
118 for (data::DecodeChannel &ch : channels_)
119 if (ch.id == channel_id)
120 ch.assigned_signal = signal;
124 decoder_stack_->begin_decode();
127 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
129 for (data::DecodeChannel &ch : channels_)
130 if (ch.id == channel_id)
131 ch.initial_pin_state = init_state;
135 decoder_stack_->begin_decode();
138 vector<Row> DecodeSignal::visible_rows() const
140 return decoder_stack_->get_visible_rows();
143 void DecodeSignal::get_annotation_subset(
144 vector<pv::data::decode::Annotation> &dest,
145 const decode::Row &row, uint64_t start_sample,
146 uint64_t end_sample) const
148 return decoder_stack_->get_annotation_subset(dest, row,
149 start_sample, end_sample);
152 void DecodeSignal::update_channel_list()
154 list<data::DecodeChannel> prev_channels = channels_;
159 // Copy existing entries, create new as needed
160 for (shared_ptr<Decoder> decoder : decoder_stack_->stack()) {
161 const srd_decoder* srd_d = decoder->decoder();
164 // Mandatory channels
165 for (l = srd_d->channels; l; l = l->next) {
166 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
167 bool ch_added = false;
169 // Copy but update ID if this channel was in the list before
170 for (data::DecodeChannel ch : prev_channels)
171 if (ch.pdch_ == pdch) {
173 channels_.push_back(ch);
179 // Create new entry without a mapped signal
180 data::DecodeChannel ch = {id++, false, nullptr,
181 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
182 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
183 channels_.push_back(ch);
188 for (l = srd_d->opt_channels; l; l = l->next) {
189 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
190 bool ch_added = false;
192 // Copy but update ID if this channel was in the list before
193 for (data::DecodeChannel ch : prev_channels)
194 if (ch.pdch_ == pdch) {
196 channels_.push_back(ch);
202 // Create new entry without a mapped signal
203 data::DecodeChannel ch = {id++, true, nullptr,
204 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
205 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
206 channels_.push_back(ch);
214 void DecodeSignal::on_new_annotations()
216 // Forward the signal to the frontend