]> sigrok.org Git - pulseview.git/blob - pv/data/decodesignal.hpp
9c8d382c808a74087f9566cae9535f66fd4ced78
[pulseview.git] / pv / data / decodesignal.hpp
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
23 #include <atomic>
24 #include <condition_variable>
25 #include <unordered_set>
26 #include <vector>
27
28 #include <boost/optional.hpp>
29
30 #include <QSettings>
31 #include <QString>
32
33 #include <libsigrokdecode/libsigrokdecode.h>
34
35 #include <pv/data/decode/row.hpp>
36 #include <pv/data/decode/rowdata.hpp>
37 #include <pv/data/signalbase.hpp>
38 #include <pv/util.hpp>
39
40 using std::atomic;
41 using std::condition_variable;
42 using std::map;
43 using std::mutex;
44 using std::pair;
45 using std::unordered_set;
46 using std::vector;
47 using std::shared_ptr;
48
49 namespace pv {
50 class Session;
51
52 namespace data {
53
54 namespace decode {
55 class Annotation;
56 class Decoder;
57 class Row;
58 }
59
60 class Logic;
61 class LogicSegment;
62 class SignalBase;
63 class SignalData;
64
65 struct 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
76 class DecodeSignal : public SignalBase
77 {
78         Q_OBJECT
79
80 private:
81         static const double DecodeMargin;
82         static const double DecodeThreshold;
83         static const int64_t DecodeChunkLength;
84         static const unsigned int DecodeNotifyPeriod;
85
86 public:
87         DecodeSignal(pv::Session &session);
88         virtual ~DecodeSignal();
89
90         bool is_decode_signal() const;
91         const vector< shared_ptr<data::decode::Decoder> >& decoder_stack() const;
92
93         void stack_decoder(srd_decoder *decoder);
94         void remove_decoder(int index);
95         bool toggle_decoder_visibility(int index);
96
97         void reset_decode();
98         void begin_decode();
99         QString error_message() const;
100
101         const vector<data::DecodeChannel> get_channels() const;
102         void auto_assign_signals();
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
107         double samplerate() const;
108         const pv::util::Timestamp& start_time() const;
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;
118
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
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
140 private:
141         void update_channel_list();
142
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
154 Q_SIGNALS:
155         void new_annotations();
156         void channels_updated();
157
158 private Q_SLOTS:
159         void on_capture_state_changed(int state);
160         void on_data_received();
161         void on_frame_ended();
162
163 private:
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_;
197 };
198
199 } // namespace data
200 } // namespace pv
201
202 #endif // PULSEVIEW_PV_DATA_DECODESIGNAL_HPP