]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.hpp
Merge DecoderStack into DecodeSignal
[pulseview.git] / pv / data / decodesignal.hpp
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#ifndef PULSEVIEW_PV_DATA_DECODESIGNAL_HPP
21#define PULSEVIEW_PV_DATA_DECODESIGNAL_HPP
22
47747218
SA
23#include <atomic>
24#include <condition_variable>
132a5c6d 25#include <unordered_set>
ecd07c20
SA
26#include <vector>
27
47747218
SA
28#include <boost/optional.hpp>
29
30#include <QSettings>
ecd07c20
SA
31#include <QString>
32
ad908057
SA
33#include <libsigrokdecode/libsigrokdecode.h>
34
47747218
SA
35#include <pv/data/decode/row.hpp>
36#include <pv/data/decode/rowdata.hpp>
ad908057 37#include <pv/data/signalbase.hpp>
946b52e1 38#include <pv/util.hpp>
ad908057 39
47747218
SA
40using std::atomic;
41using std::condition_variable;
42using std::map;
43using std::mutex;
44using std::pair;
132a5c6d 45using std::unordered_set;
ecd07c20 46using std::vector;
ad908057
SA
47using std::shared_ptr;
48
49namespace pv {
47747218
SA
50class Session;
51
ad908057
SA
52namespace data {
53
ecd07c20
SA
54namespace decode {
55class Annotation;
56class Decoder;
57class Row;
58}
59
ad908057 60class Logic;
47747218 61class LogicSegment;
9f97b357 62class SignalBase;
ad908057
SA
63class SignalData;
64
9f97b357
SA
65struct DecodeChannel
66{
67 uint16_t id; // Also tells which bit within a sample represents this channel
68 const bool is_optional;
69 const pv::data::SignalBase *assigned_signal;
70 const QString name, desc;
71 int initial_pin_state;
72 const shared_ptr<pv::data::decode::Decoder> decoder_;
73 const srd_channel *pdch_;
74};
75
ad908057
SA
76class DecodeSignal : public SignalBase
77{
78 Q_OBJECT
79
47747218
SA
80private:
81 static const double DecodeMargin;
82 static const double DecodeThreshold;
83 static const int64_t DecodeChunkLength;
84 static const unsigned int DecodeNotifyPeriod;
85
ad908057 86public:
47747218 87 DecodeSignal(pv::Session &session);
ad908057
SA
88 virtual ~DecodeSignal();
89
90 bool is_decode_signal() const;
47747218 91 const vector< shared_ptr<data::decode::Decoder> >& decoder_stack() const;
ad908057
SA
92
93 void stack_decoder(srd_decoder *decoder);
94 void remove_decoder(int index);
95 bool toggle_decoder_visibility(int index);
96
47747218 97 void reset_decode();
946b52e1 98 void begin_decode();
ecd07c20
SA
99 QString error_message() const;
100
47747218 101 const vector<data::DecodeChannel> get_channels() const;
132a5c6d 102 void auto_assign_signals();
9f97b357
SA
103 void assign_signal(const uint16_t channel_id, const SignalBase *signal);
104
105 void set_initial_pin_state(const uint16_t channel_id, const int init_state);
106
ff83d980
SA
107 double samplerate() const;
108 const pv::util::Timestamp& start_time() const;
0c5fe73e
SA
109
110 /**
111 * Returns the number of samples that can be worked on,
112 * i.e. the number of samples where samples are available
113 * for all connected channels.
114 */
115 int64_t get_working_sample_count() const;
116
117 int64_t get_decoded_sample_count() const;
ff83d980 118
ecd07c20
SA
119 vector<decode::Row> visible_rows() const;
120
121 /**
122 * Extracts sorted annotations between two period into a vector.
123 */
124 void get_annotation_subset(
125 vector<pv::data::decode::Annotation> &dest,
126 const decode::Row &row, uint64_t start_sample,
127 uint64_t end_sample) const;
128
47747218
SA
129 virtual void save_settings(QSettings &settings) const;
130
131 virtual void restore_settings(QSettings &settings);
132
133 /**
134 * Helper function for static annotation_callback(),
135 * must be public so the function can access it.
136 * Don't use from outside this class.
137 */
138 uint64_t inc_annotation_count();
139
9f97b357
SA
140private:
141 void update_channel_list();
142
47747218
SA
143 void logic_mux_proc();
144
145 boost::optional<int64_t> wait_for_data() const;
146
147 void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count,
148 srd_session *const session);
149
150 void decode_proc();
151
152 static void annotation_callback(srd_proto_data *pdata, void *decode_signal);
153
ad908057
SA
154Q_SIGNALS:
155 void new_annotations();
9f97b357 156 void channels_updated();
ad908057
SA
157
158private Q_SLOTS:
47747218
SA
159 void on_capture_state_changed(int state);
160 void on_data_received();
161 void on_frame_ended();
ad908057
SA
162
163private:
47747218
SA
164 pv::Session &session_;
165
166 vector<data::DecodeChannel> channels_;
167
168 shared_ptr<Logic> logic_mux_data_;
169 shared_ptr<LogicSegment> segment_;
170 bool logic_mux_data_invalid_;
171
172 pv::util::Timestamp start_time_;
173 double samplerate_;
174
175 int64_t sample_count_, annotation_count_, samples_decoded_;
176
177 vector< shared_ptr<decode::Decoder> > stack_;
178 map<const decode::Row, decode::RowData> rows_;
179 map<pair<const srd_decoder*, int>, decode::Row> class_rows_;
180
181 /**
182 * This mutex prevents more than one thread from accessing
183 * libsigrokdecode concurrently.
184 * @todo A proper solution should be implemented to allow multiple
185 * decode operations in parallel.
186 */
187 static mutex global_srd_mutex_;
188
189 mutable mutex input_mutex_, output_mutex_;
190 mutable condition_variable decode_input_cond_, logic_mux_cond_;
191 bool frame_complete_;
192
193 std::thread decode_thread_, logic_mux_thread_;
194 atomic<bool> decode_interrupt_, logic_mux_interrupt_;
195
196 QString error_message_;
ad908057
SA
197};
198
199} // namespace data
200} // namespace pv
201
202#endif // PULSEVIEW_PV_DATA_DECODESIGNAL_HPP