]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.cpp
Remove DecodeStack dependency from decode binding wrapper
[pulseview.git] / pv / data / decodesignal.cpp
CommitLineData
ad908057
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "logic.hpp"
21#include "logicsegment.hpp"
22#include "decodesignal.hpp"
23#include "signaldata.hpp"
24
25#include <pv/binding/decoder.hpp>
26#include <pv/data/decode/decoder.hpp>
ecd07c20 27#include <pv/data/decode/row.hpp>
ad908057
SA
28#include <pv/data/decoderstack.hpp>
29#include <pv/session.hpp>
30
31using std::make_shared;
32using std::shared_ptr;
33using pv::data::decode::Decoder;
ecd07c20 34using pv::data::decode::Row;
ad908057
SA
35
36namespace pv {
37namespace data {
38
132a5c6d
SA
39DecodeSignal::DecodeSignal(shared_ptr<pv::data::DecoderStack> decoder_stack,
40 const unordered_set< shared_ptr<data::SignalBase> > &all_signals) :
ad908057 41 SignalBase(nullptr, SignalBase::DecodeChannel),
132a5c6d
SA
42 decoder_stack_(decoder_stack),
43 all_signals_(all_signals)
ad908057
SA
44{
45 set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
46
9f97b357 47 update_channel_list();
132a5c6d 48 auto_assign_signals();
9f97b357 49
ad908057
SA
50 connect(decoder_stack_.get(), SIGNAL(new_annotations()),
51 this, SLOT(on_new_annotations()));
52}
53
54DecodeSignal::~DecodeSignal()
55{
56}
57
58bool DecodeSignal::is_decode_signal() const
59{
60 return true;
61}
62
63shared_ptr<pv::data::DecoderStack> DecodeSignal::decoder_stack() const
64{
65 return decoder_stack_;
66}
67
ecd07c20
SA
68const list< shared_ptr<Decoder> >& DecodeSignal::decoder_stack_list() const
69{
70 return decoder_stack_->stack();
71}
72
ad908057
SA
73void DecodeSignal::stack_decoder(srd_decoder *decoder)
74{
75 assert(decoder);
76 assert(decoder_stack);
77 decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
132a5c6d
SA
78
79 // Include the newly created decode channels in the channel list
9f97b357 80 update_channel_list();
132a5c6d
SA
81
82 auto_assign_signals();
ad908057
SA
83 decoder_stack_->begin_decode();
84}
85
86void DecodeSignal::remove_decoder(int index)
87{
88 decoder_stack_->remove(index);
9f97b357 89 update_channel_list();
ad908057
SA
90 decoder_stack_->begin_decode();
91}
92
93bool DecodeSignal::toggle_decoder_visibility(int index)
94{
95 const list< shared_ptr<Decoder> > stack(decoder_stack_->stack());
96
97 auto iter = stack.cbegin();
98 for (int i = 0; i < index; i++, iter++)
99 assert(iter != stack.end());
100
101 shared_ptr<Decoder> dec = *iter;
102
103 // Toggle decoder visibility
104 bool state = false;
105 if (dec) {
106 state = !dec->shown();
107 dec->show(state);
108 }
109
110 return state;
111}
112
946b52e1
SA
113void DecodeSignal::begin_decode()
114{
115 decoder_stack_->begin_decode();
116}
117
ecd07c20
SA
118QString DecodeSignal::error_message() const
119{
120 return decoder_stack_->error_message();
121}
122
9f97b357
SA
123const list<data::DecodeChannel> DecodeSignal::get_channels() const
124{
125 return channels_;
126}
127
132a5c6d
SA
128void DecodeSignal::auto_assign_signals()
129{
130 // Try to auto-select channels that don't have signals assigned yet
131 for (data::DecodeChannel &ch : channels_) {
132 if (ch.assigned_signal)
133 continue;
134
135 for (shared_ptr<data::SignalBase> s : all_signals_)
136 if (s->logic_data() && (ch.name.toLower().contains(s->name().toLower())))
137 ch.assigned_signal = s.get();
138 }
139}
140
9f97b357
SA
141void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
142{
143 for (data::DecodeChannel &ch : channels_)
144 if (ch.id == channel_id)
145 ch.assigned_signal = signal;
146
147 channels_updated();
148
149 decoder_stack_->begin_decode();
150}
151
152void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
153{
154 for (data::DecodeChannel &ch : channels_)
155 if (ch.id == channel_id)
156 ch.initial_pin_state = init_state;
157
158 channels_updated();
159
160 decoder_stack_->begin_decode();
161}
162
ff83d980
SA
163int64_t DecodeSignal::sample_count() const
164{
165 shared_ptr<Logic> data;
166 shared_ptr<data::SignalBase> signalbase;
167
168 // We get the logic data of the first channel in the list.
169 // This works because we are currently assuming all
170 // LogicSignals have the same data/segment
171 for (const shared_ptr<Decoder> &dec : decoder_stack_->stack())
172 if (dec && !dec->channels().empty() &&
173 ((signalbase = (*dec->channels().begin()).second)) &&
174 ((data = signalbase->logic_data())))
175 break;
176
177 if (!data || data->logic_segments().empty())
178 return 0;
179
180 const shared_ptr<LogicSegment> segment = data->logic_segments().front();
181 assert(segment);
182
183 return (int64_t)segment->get_sample_count();
184}
185
186double DecodeSignal::samplerate() const
187{
188 return decoder_stack_->samplerate();
189}
190
191const pv::util::Timestamp& DecodeSignal::start_time() const
192{
193 return decoder_stack_->start_time();
194}
195
196int64_t DecodeSignal::samples_decoded() const
197{
198 return decoder_stack_->samples_decoded();
199}
200
ecd07c20
SA
201vector<Row> DecodeSignal::visible_rows() const
202{
203 return decoder_stack_->get_visible_rows();
204}
205
206void DecodeSignal::get_annotation_subset(
207 vector<pv::data::decode::Annotation> &dest,
208 const decode::Row &row, uint64_t start_sample,
209 uint64_t end_sample) const
210{
211 return decoder_stack_->get_annotation_subset(dest, row,
212 start_sample, end_sample);
213}
214
9f97b357
SA
215void DecodeSignal::update_channel_list()
216{
217 list<data::DecodeChannel> prev_channels = channels_;
218 channels_.clear();
219
220 uint16_t id = 0;
221
222 // Copy existing entries, create new as needed
223 for (shared_ptr<Decoder> decoder : decoder_stack_->stack()) {
224 const srd_decoder* srd_d = decoder->decoder();
225 const GSList *l;
226
227 // Mandatory channels
228 for (l = srd_d->channels; l; l = l->next) {
229 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
230 bool ch_added = false;
231
232 // Copy but update ID if this channel was in the list before
233 for (data::DecodeChannel ch : prev_channels)
234 if (ch.pdch_ == pdch) {
235 ch.id = id++;
236 channels_.push_back(ch);
237 ch_added = true;
238 break;
239 }
240
241 if (!ch_added) {
242 // Create new entry without a mapped signal
243 data::DecodeChannel ch = {id++, false, nullptr,
244 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
245 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
246 channels_.push_back(ch);
247 }
248 }
249
250 // Optional channels
251 for (l = srd_d->opt_channels; l; l = l->next) {
252 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
253 bool ch_added = false;
254
255 // Copy but update ID if this channel was in the list before
256 for (data::DecodeChannel ch : prev_channels)
257 if (ch.pdch_ == pdch) {
258 ch.id = id++;
259 channels_.push_back(ch);
260 ch_added = true;
261 break;
262 }
263
264 if (!ch_added) {
265 // Create new entry without a mapped signal
266 data::DecodeChannel ch = {id++, true, nullptr,
267 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
268 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
269 channels_.push_back(ch);
270 }
271 }
272 }
273
274 channels_updated();
275}
276
ad908057
SA
277void DecodeSignal::on_new_annotations()
278{
279 // Forward the signal to the frontend
280 new_annotations();
281}
282
283} // namespace data
284} // namespace pv