]> sigrok.org Git - pulseview.git/blob - pv/data/decodesignal.cpp
6bc691abb5d49a29185a6b404c80bb88f22a548f
[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         SignalBase(nullptr, SignalBase::DecodeChannel),
41         decoder_stack_(decoder_stack)
42 {
43         set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
44
45         update_channel_list();
46
47         connect(decoder_stack_.get(), SIGNAL(new_annotations()),
48                 this, SLOT(on_new_annotations()));
49 }
50
51 DecodeSignal::~DecodeSignal()
52 {
53 }
54
55 bool DecodeSignal::is_decode_signal() const
56 {
57         return true;
58 }
59
60 shared_ptr<pv::data::DecoderStack> DecodeSignal::decoder_stack() const
61 {
62         return decoder_stack_;
63 }
64
65 const list< shared_ptr<Decoder> >& DecodeSignal::decoder_stack_list() const
66 {
67         return decoder_stack_->stack();
68 }
69
70 void DecodeSignal::stack_decoder(srd_decoder *decoder)
71 {
72         assert(decoder);
73         assert(decoder_stack);
74         decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
75         update_channel_list();
76         decoder_stack_->begin_decode();
77 }
78
79 void DecodeSignal::remove_decoder(int index)
80 {
81         decoder_stack_->remove(index);
82         update_channel_list();
83         decoder_stack_->begin_decode();
84 }
85
86 bool DecodeSignal::toggle_decoder_visibility(int index)
87 {
88         const list< shared_ptr<Decoder> > stack(decoder_stack_->stack());
89
90         auto iter = stack.cbegin();
91         for (int i = 0; i < index; i++, iter++)
92                 assert(iter != stack.end());
93
94         shared_ptr<Decoder> dec = *iter;
95
96         // Toggle decoder visibility
97         bool state = false;
98         if (dec) {
99                 state = !dec->shown();
100                 dec->show(state);
101         }
102
103         return state;
104 }
105
106 QString DecodeSignal::error_message() const
107 {
108         return decoder_stack_->error_message();
109 }
110
111 const list<data::DecodeChannel> DecodeSignal::get_channels() const
112 {
113         return channels_;
114 }
115
116 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
117 {
118         for (data::DecodeChannel &ch : channels_)
119                 if (ch.id == channel_id)
120                         ch.assigned_signal = signal;
121
122         channels_updated();
123
124         decoder_stack_->begin_decode();
125 }
126
127 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
128 {
129         for (data::DecodeChannel &ch : channels_)
130                 if (ch.id == channel_id)
131                         ch.initial_pin_state = init_state;
132
133         channels_updated();
134
135         decoder_stack_->begin_decode();
136 }
137
138 vector<Row> DecodeSignal::visible_rows() const
139 {
140         return decoder_stack_->get_visible_rows();
141 }
142
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
147 {
148         return decoder_stack_->get_annotation_subset(dest, row,
149                 start_sample, end_sample);
150 }
151
152 void DecodeSignal::update_channel_list()
153 {
154         list<data::DecodeChannel> prev_channels = channels_;
155         channels_.clear();
156
157         uint16_t id = 0;
158
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();
162                 const GSList *l;
163
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;
168
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) {
172                                         ch.id = id++;
173                                         channels_.push_back(ch);
174                                         ch_added = true;
175                                         break;
176                                 }
177
178                         if (!ch_added) {
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);
184                         }
185                 }
186
187                 // Optional channels
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;
191
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) {
195                                         ch.id = id++;
196                                         channels_.push_back(ch);
197                                         ch_added = true;
198                                         break;
199                                 }
200
201                         if (!ch_added) {
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);
207                         }
208                 }
209         }
210
211         channels_updated();
212 }
213
214 void DecodeSignal::on_new_annotations()
215 {
216         // Forward the signal to the frontend
217         new_annotations();
218 }
219
220 } // namespace data
221 } // namespace pv