]> sigrok.org Git - pulseview.git/blob - pv/data/decodesignal.cpp
Rework decode sample count getters
[pulseview.git] / pv / data / decodesignal.cpp
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 <limits>
21
22 #include "logic.hpp"
23 #include "logicsegment.hpp"
24 #include "decodesignal.hpp"
25 #include "signaldata.hpp"
26
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>
32
33 using std::make_shared;
34 using std::min;
35 using std::shared_ptr;
36 using pv::data::decode::Decoder;
37 using pv::data::decode::Row;
38
39 namespace pv {
40 namespace data {
41
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)
47 {
48         set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
49
50         update_channel_list();
51         auto_assign_signals();
52
53         connect(decoder_stack_.get(), SIGNAL(new_annotations()),
54                 this, SLOT(on_new_annotations()));
55 }
56
57 DecodeSignal::~DecodeSignal()
58 {
59 }
60
61 bool DecodeSignal::is_decode_signal() const
62 {
63         return true;
64 }
65
66 shared_ptr<pv::data::DecoderStack> DecodeSignal::decoder_stack() const
67 {
68         return decoder_stack_;
69 }
70
71 const list< shared_ptr<Decoder> >& DecodeSignal::decoder_stack_list() const
72 {
73         return decoder_stack_->stack();
74 }
75
76 void DecodeSignal::stack_decoder(srd_decoder *decoder)
77 {
78         assert(decoder);
79         assert(decoder_stack);
80         decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
81
82         // Include the newly created decode channels in the channel list
83         update_channel_list();
84
85         auto_assign_signals();
86         decoder_stack_->begin_decode();
87 }
88
89 void DecodeSignal::remove_decoder(int index)
90 {
91         decoder_stack_->remove(index);
92         update_channel_list();
93         decoder_stack_->begin_decode();
94 }
95
96 bool DecodeSignal::toggle_decoder_visibility(int index)
97 {
98         const list< shared_ptr<Decoder> > stack(decoder_stack_->stack());
99
100         auto iter = stack.cbegin();
101         for (int i = 0; i < index; i++, iter++)
102                 assert(iter != stack.end());
103
104         shared_ptr<Decoder> dec = *iter;
105
106         // Toggle decoder visibility
107         bool state = false;
108         if (dec) {
109                 state = !dec->shown();
110                 dec->show(state);
111         }
112
113         return state;
114 }
115
116 void DecodeSignal::begin_decode()
117 {
118         decoder_stack_->begin_decode();
119 }
120
121 QString DecodeSignal::error_message() const
122 {
123         return decoder_stack_->error_message();
124 }
125
126 const list<data::DecodeChannel> DecodeSignal::get_channels() const
127 {
128         return channels_;
129 }
130
131 void DecodeSignal::auto_assign_signals()
132 {
133         // Try to auto-select channels that don't have signals assigned yet
134         for (data::DecodeChannel &ch : channels_) {
135                 if (ch.assigned_signal)
136                         continue;
137
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();
141         }
142 }
143
144 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
145 {
146         for (data::DecodeChannel &ch : channels_)
147                 if (ch.id == channel_id)
148                         ch.assigned_signal = signal;
149
150         channels_updated();
151
152         decoder_stack_->begin_decode();
153 }
154
155 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
156 {
157         for (data::DecodeChannel &ch : channels_)
158                 if (ch.id == channel_id)
159                         ch.initial_pin_state = init_state;
160
161         channels_updated();
162
163         decoder_stack_->begin_decode();
164 }
165
166 double DecodeSignal::samplerate() const
167 {
168         return decoder_stack_->samplerate();
169 }
170
171 const pv::util::Timestamp& DecodeSignal::start_time() const
172 {
173         return decoder_stack_->start_time();
174 }
175
176 int64_t DecodeSignal::get_working_sample_count() const
177 {
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
181         // current segment
182
183         // TODO Currently, we assume only a single segment exists
184
185         int64_t count = std::numeric_limits<int64_t>::max();
186         bool no_signals_assigned = true;
187
188         for (const data::DecodeChannel &ch : channels_)
189                 if (ch.assigned_signal) {
190                         no_signals_assigned = false;
191
192                         const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
193                         if (!logic_data || logic_data->logic_segments().empty())
194                                 return 0;
195
196                         const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
197                         count = min(count, (int64_t)segment->get_sample_count());
198                 }
199
200         return (no_signals_assigned ? 0 : count);
201 }
202
203 int64_t DecodeSignal::get_decoded_sample_count() const
204 {
205         return decoder_stack_->samples_decoded();
206 }
207
208 vector<Row> DecodeSignal::visible_rows() const
209 {
210         return decoder_stack_->get_visible_rows();
211 }
212
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
217 {
218         return decoder_stack_->get_annotation_subset(dest, row,
219                 start_sample, end_sample);
220 }
221
222 void DecodeSignal::update_channel_list()
223 {
224         list<data::DecodeChannel> prev_channels = channels_;
225         channels_.clear();
226
227         uint16_t id = 0;
228
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();
232                 const GSList *l;
233
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;
238
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) {
242                                         ch.id = id++;
243                                         channels_.push_back(ch);
244                                         ch_added = true;
245                                         break;
246                                 }
247
248                         if (!ch_added) {
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);
254                         }
255                 }
256
257                 // Optional channels
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;
261
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) {
265                                         ch.id = id++;
266                                         channels_.push_back(ch);
267                                         ch_added = true;
268                                         break;
269                                 }
270
271                         if (!ch_added) {
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);
277                         }
278                 }
279         }
280
281         channels_updated();
282 }
283
284 void DecodeSignal::on_new_annotations()
285 {
286         // Forward the signal to the frontend
287         new_annotations();
288 }
289
290 } // namespace data
291 } // namespace pv