]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.cpp
Rework decode sample count getters
[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
0c5fe73e
SA
20#include <limits>
21
ad908057
SA
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>
ecd07c20 29#include <pv/data/decode/row.hpp>
ad908057
SA
30#include <pv/data/decoderstack.hpp>
31#include <pv/session.hpp>
32
33using std::make_shared;
0c5fe73e 34using std::min;
ad908057
SA
35using std::shared_ptr;
36using pv::data::decode::Decoder;
ecd07c20 37using pv::data::decode::Row;
ad908057
SA
38
39namespace pv {
40namespace data {
41
132a5c6d
SA
42DecodeSignal::DecodeSignal(shared_ptr<pv::data::DecoderStack> decoder_stack,
43 const unordered_set< shared_ptr<data::SignalBase> > &all_signals) :
ad908057 44 SignalBase(nullptr, SignalBase::DecodeChannel),
132a5c6d
SA
45 decoder_stack_(decoder_stack),
46 all_signals_(all_signals)
ad908057
SA
47{
48 set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
49
9f97b357 50 update_channel_list();
132a5c6d 51 auto_assign_signals();
9f97b357 52
ad908057
SA
53 connect(decoder_stack_.get(), SIGNAL(new_annotations()),
54 this, SLOT(on_new_annotations()));
55}
56
57DecodeSignal::~DecodeSignal()
58{
59}
60
61bool DecodeSignal::is_decode_signal() const
62{
63 return true;
64}
65
66shared_ptr<pv::data::DecoderStack> DecodeSignal::decoder_stack() const
67{
68 return decoder_stack_;
69}
70
ecd07c20
SA
71const list< shared_ptr<Decoder> >& DecodeSignal::decoder_stack_list() const
72{
73 return decoder_stack_->stack();
74}
75
ad908057
SA
76void DecodeSignal::stack_decoder(srd_decoder *decoder)
77{
78 assert(decoder);
79 assert(decoder_stack);
80 decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
132a5c6d
SA
81
82 // Include the newly created decode channels in the channel list
9f97b357 83 update_channel_list();
132a5c6d
SA
84
85 auto_assign_signals();
ad908057
SA
86 decoder_stack_->begin_decode();
87}
88
89void DecodeSignal::remove_decoder(int index)
90{
91 decoder_stack_->remove(index);
9f97b357 92 update_channel_list();
ad908057
SA
93 decoder_stack_->begin_decode();
94}
95
96bool 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
946b52e1
SA
116void DecodeSignal::begin_decode()
117{
118 decoder_stack_->begin_decode();
119}
120
ecd07c20
SA
121QString DecodeSignal::error_message() const
122{
123 return decoder_stack_->error_message();
124}
125
9f97b357
SA
126const list<data::DecodeChannel> DecodeSignal::get_channels() const
127{
128 return channels_;
129}
130
132a5c6d
SA
131void 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
9f97b357
SA
144void 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
155void 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
ff83d980
SA
166double DecodeSignal::samplerate() const
167{
168 return decoder_stack_->samplerate();
169}
170
171const pv::util::Timestamp& DecodeSignal::start_time() const
172{
173 return decoder_stack_->start_time();
174}
175
0c5fe73e
SA
176int64_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
203int64_t DecodeSignal::get_decoded_sample_count() const
ff83d980
SA
204{
205 return decoder_stack_->samples_decoded();
206}
207
ecd07c20
SA
208vector<Row> DecodeSignal::visible_rows() const
209{
210 return decoder_stack_->get_visible_rows();
211}
212
213void 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
9f97b357
SA
222void 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
ad908057
SA
284void DecodeSignal::on_new_annotations()
285{
286 // Forward the signal to the frontend
287 new_annotations();
288}
289
290} // namespace data
291} // namespace pv