]>
Commit | Line | Data |
---|---|---|
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 | 28 | #include <QSettings> |
ecd07c20 SA |
29 | #include <QString> |
30 | ||
ad908057 SA |
31 | #include <libsigrokdecode/libsigrokdecode.h> |
32 | ||
a82325d1 | 33 | #include <pv/data/decode/decoder.hpp> |
47747218 SA |
34 | #include <pv/data/decode/row.hpp> |
35 | #include <pv/data/decode/rowdata.hpp> | |
ad908057 | 36 | #include <pv/data/signalbase.hpp> |
946b52e1 | 37 | #include <pv/util.hpp> |
ad908057 | 38 | |
47747218 SA |
39 | using std::atomic; |
40 | using std::condition_variable; | |
41 | using std::map; | |
42 | using std::mutex; | |
43 | using std::pair; | |
ecd07c20 | 44 | using std::vector; |
ad908057 SA |
45 | using std::shared_ptr; |
46 | ||
47 | namespace pv { | |
47747218 SA |
48 | class Session; |
49 | ||
ad908057 SA |
50 | namespace data { |
51 | ||
ecd07c20 SA |
52 | namespace decode { |
53 | class Annotation; | |
54 | class Decoder; | |
55 | class Row; | |
56 | } | |
57 | ||
ad908057 | 58 | class Logic; |
47747218 | 59 | class LogicSegment; |
9f97b357 | 60 | class SignalBase; |
ad908057 SA |
61 | class SignalData; |
62 | ||
e77de61f | 63 | struct DecodeBinaryDataChunk |
c0c3e62a SA |
64 | { |
65 | vector<uint8_t> data; | |
66 | uint64_t sample; ///< Number of the sample where this data was provided by the PD | |
67 | }; | |
68 | ||
e77de61f SA |
69 | struct DecodeBinaryClass |
70 | { | |
71 | const decode::Decoder* decoder; | |
72 | const decode::DecodeBinaryClassInfo* info; | |
73 | vector<DecodeBinaryDataChunk> chunks; | |
74 | }; | |
75 | ||
72435789 SA |
76 | struct DecodeSegment |
77 | { | |
78 | map<const decode::Row, decode::RowData> annotation_rows; | |
79 | pv::util::Timestamp start_time; | |
80 | double samplerate; | |
b82908ab | 81 | int64_t samples_decoded_incl, samples_decoded_excl; |
e77de61f | 82 | vector<DecodeBinaryClass> binary_classes; |
72435789 SA |
83 | }; |
84 | ||
ad908057 SA |
85 | class DecodeSignal : public SignalBase |
86 | { | |
87 | Q_OBJECT | |
88 | ||
47747218 SA |
89 | private: |
90 | static const double DecodeMargin; | |
91 | static const double DecodeThreshold; | |
92 | static const int64_t DecodeChunkLength; | |
47747218 | 93 | |
ad908057 | 94 | public: |
47747218 | 95 | DecodeSignal(pv::Session &session); |
ad908057 SA |
96 | virtual ~DecodeSignal(); |
97 | ||
98 | bool is_decode_signal() const; | |
47747218 | 99 | const vector< shared_ptr<data::decode::Decoder> >& decoder_stack() const; |
ad908057 | 100 | |
c8e60bdf | 101 | void stack_decoder(const srd_decoder *decoder); |
ad908057 SA |
102 | void remove_decoder(int index); |
103 | bool toggle_decoder_visibility(int index); | |
104 | ||
8ce0e732 | 105 | void reset_decode(bool shutting_down = false); |
946b52e1 | 106 | void begin_decode(); |
556259d2 SA |
107 | void pause_decode(); |
108 | void resume_decode(); | |
109 | bool is_paused() const; | |
ecd07c20 SA |
110 | QString error_message() const; |
111 | ||
a82325d1 | 112 | const vector<data::decode::DecodeChannel> get_channels() const; |
762ab7a4 | 113 | void auto_assign_signals(const shared_ptr<pv::data::decode::Decoder> dec); |
9f97b357 | 114 | void assign_signal(const uint16_t channel_id, const SignalBase *signal); |
27a3f09b | 115 | int get_assigned_signal_count() const; |
9f97b357 SA |
116 | |
117 | void set_initial_pin_state(const uint16_t channel_id, const int init_state); | |
118 | ||
ff83d980 | 119 | double samplerate() const; |
72435789 | 120 | const pv::util::Timestamp start_time() const; |
0c5fe73e SA |
121 | |
122 | /** | |
123 | * Returns the number of samples that can be worked on, | |
124 | * i.e. the number of samples where samples are available | |
125 | * for all connected channels. | |
126 | */ | |
5ecf957f | 127 | int64_t get_working_sample_count(uint32_t segment_id) const; |
0c5fe73e | 128 | |
b82908ab SA |
129 | /** |
130 | * Returns the number of processed samples. Newly generated annotations will | |
131 | * have sample numbers greater than this. | |
132 | * | |
133 | * If include_processing is true, this number will include the ones being | |
134 | * currently processed (in case the decoder stack is running). In this case, | |
135 | * newly generated annotations will have sample numbers smaller than this. | |
136 | */ | |
137 | int64_t get_decoded_sample_count(uint32_t segment_id, | |
138 | bool include_processing) const; | |
ff83d980 | 139 | |
ab185f78 | 140 | vector<decode::Row> get_rows(bool visible_only) const; |
ecd07c20 SA |
141 | |
142 | /** | |
5a914348 SA |
143 | * Extracts annotations from a single row into a vector. |
144 | * Note: The annotations may be unsorted and only annotations that fully | |
4caaaace | 145 | * fit into the sample range are considered. |
ecd07c20 SA |
146 | */ |
147 | void get_annotation_subset( | |
148 | vector<pv::data::decode::Annotation> &dest, | |
72435789 | 149 | const decode::Row &row, uint32_t segment_id, uint64_t start_sample, |
ecd07c20 SA |
150 | uint64_t end_sample) const; |
151 | ||
5a914348 SA |
152 | /** |
153 | * Extracts annotations from all rows into a vector. | |
154 | * Note: The annotations may be unsorted and only annotations that fully | |
155 | * fit into the sample range are considered. | |
156 | */ | |
157 | void get_annotation_subset( | |
158 | vector<pv::data::decode::Annotation> &dest, | |
159 | uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const; | |
160 | ||
e77de61f | 161 | uint32_t get_binary_data_chunk_count(uint32_t segment_id, |
ac9494ef | 162 | const data::decode::Decoder* dec, uint32_t bin_class_id) const; |
e77de61f | 163 | void get_binary_data_chunk(uint32_t segment_id, const data::decode::Decoder* dec, |
ac9494ef | 164 | uint32_t bin_class_id, uint32_t chunk_id, const vector<uint8_t> **dest, |
e77de61f SA |
165 | uint64_t *size); |
166 | void get_binary_data_chunks_merged(uint32_t segment_id, const data::decode::Decoder* dec, | |
ac9494ef | 167 | uint32_t bin_class_id, uint64_t start_sample, uint64_t end_sample, |
e77de61f | 168 | vector<uint8_t> *dest) const; |
628b45cc | 169 | const DecodeBinaryClass* get_binary_data_class(uint32_t segment_id, |
ac9494ef | 170 | const data::decode::Decoder* dec, uint32_t bin_class_id) const; |
c0c3e62a | 171 | |
47747218 SA |
172 | virtual void save_settings(QSettings &settings) const; |
173 | ||
174 | virtual void restore_settings(QSettings &settings); | |
175 | ||
9f97b357 | 176 | private: |
403c3e87 SA |
177 | void set_error_message(QString msg); |
178 | ||
8a603e13 SA |
179 | uint32_t get_input_segment_count() const; |
180 | ||
181 | uint32_t get_input_samplerate(uint32_t segment_id) const; | |
182 | ||
9f97b357 SA |
183 | void update_channel_list(); |
184 | ||
27a3f09b | 185 | void commit_decoder_channels(); |
47747218 | 186 | |
ed535cd7 | 187 | void mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end); |
27a3f09b SA |
188 | |
189 | void logic_mux_proc(); | |
47747218 | 190 | |
8a603e13 SA |
191 | void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count, |
192 | const shared_ptr<LogicSegment> input_segment); | |
47747218 SA |
193 | |
194 | void decode_proc(); | |
195 | ||
e91883bb | 196 | void start_srd_session(); |
ce0b6a40 | 197 | void terminate_srd_session(); |
e91883bb SA |
198 | void stop_srd_session(); |
199 | ||
a8a9222d SA |
200 | void connect_input_notifiers(); |
201 | ||
65efd025 | 202 | void create_decode_segment(); |
72435789 | 203 | |
47747218 | 204 | static void annotation_callback(srd_proto_data *pdata, void *decode_signal); |
c0c3e62a | 205 | static void binary_callback(srd_proto_data *pdata, void *decode_signal); |
47747218 | 206 | |
ad908057 | 207 | Q_SIGNALS: |
e77de61f SA |
208 | void decoder_stacked(void* decoder); ///< decoder is of type decode::Decoder* |
209 | void decoder_removed(void* decoder); ///< decoder is of type decode::Decoder* | |
5ecf957f | 210 | void new_annotations(); // TODO Supply segment for which they belong to |
b2b18d3a | 211 | void new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id); |
eee3eab9 | 212 | void decode_reset(); |
1b56c646 | 213 | void decode_finished(); |
9f97b357 | 214 | void channels_updated(); |
ad908057 SA |
215 | |
216 | private Q_SLOTS: | |
47747218 | 217 | void on_capture_state_changed(int state); |
1b56c646 | 218 | void on_data_cleared(); |
47747218 | 219 | void on_data_received(); |
ad908057 SA |
220 | |
221 | private: | |
47747218 SA |
222 | pv::Session &session_; |
223 | ||
a82325d1 | 224 | vector<data::decode::DecodeChannel> channels_; |
47747218 | 225 | |
e91883bb SA |
226 | struct srd_session *srd_session_; |
227 | ||
47747218 | 228 | shared_ptr<Logic> logic_mux_data_; |
ed535cd7 | 229 | uint32_t logic_mux_unit_size_; |
47747218 SA |
230 | bool logic_mux_data_invalid_; |
231 | ||
47747218 | 232 | vector< shared_ptr<decode::Decoder> > stack_; |
e3202557 | 233 | bool stack_config_changed_; |
47747218 SA |
234 | map<pair<const srd_decoder*, int>, decode::Row> class_rows_; |
235 | ||
72435789 SA |
236 | vector<DecodeSegment> segments_; |
237 | uint32_t current_segment_id_; | |
4913560f | 238 | |
556259d2 SA |
239 | mutable mutex input_mutex_, output_mutex_, decode_pause_mutex_, logic_mux_mutex_; |
240 | mutable condition_variable decode_input_cond_, decode_pause_cond_, | |
241 | logic_mux_cond_; | |
47747218 SA |
242 | |
243 | std::thread decode_thread_, logic_mux_thread_; | |
244 | atomic<bool> decode_interrupt_, logic_mux_interrupt_; | |
245 | ||
556259d2 SA |
246 | bool decode_paused_; |
247 | ||
47747218 | 248 | QString error_message_; |
ad908057 SA |
249 | }; |
250 | ||
251 | } // namespace data | |
252 | } // namespace pv | |
253 | ||
254 | #endif // PULSEVIEW_PV_DATA_DECODESIGNAL_HPP |