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