]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decoderstack.hpp
Introduce DecodeSignal class
[pulseview.git] / pv / data / decoderstack.hpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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_DECODERSTACK_HPP
21#define PULSEVIEW_PV_DATA_DECODERSTACK_HPP
22
23#include "signaldata.hpp"
24
25#include <atomic>
26#include <condition_variable>
27#include <list>
28#include <map>
29#include <memory>
30#include <thread>
31
32#include <boost/optional.hpp>
33
34#include <QObject>
35#include <QString>
36
37#include <pv/data/decode/row.hpp>
38#include <pv/data/decode/rowdata.hpp>
39#include <pv/util.hpp>
40
41using std::atomic;
42using std::condition_variable;
43using std::list;
44using std::map;
45using std::mutex;
46using std::pair;
47using std::shared_ptr;
48using std::vector;
49
50struct srd_decoder;
51struct srd_decoder_annotation_row;
52struct srd_channel;
53struct srd_proto_data;
54struct srd_session;
55
56namespace DecoderStackTest {
57struct TwoDecoderStack;
58}
59
60namespace pv {
61
62class Session;
63
64namespace view {
65class LogicSignal;
66}
67
68namespace data {
69
70class LogicSegment;
71
72namespace decode {
73class Annotation;
74class Decoder;
75}
76
77class Logic;
78
79class DecoderStack : public QObject
80{
81 Q_OBJECT
82
83private:
84 static const double DecodeMargin;
85 static const double DecodeThreshold;
86 static const int64_t DecodeChunkLength;
87 static const unsigned int DecodeNotifyPeriod;
88
89public:
90 DecoderStack(pv::Session &session, const srd_decoder *const dec);
91
92 virtual ~DecoderStack();
93
94 const list< shared_ptr<decode::Decoder> >& stack() const;
95 void push(shared_ptr<decode::Decoder> decoder);
96 void remove(int index);
97
98 double samplerate() const;
99
100 const pv::util::Timestamp& start_time() const;
101
102 int64_t samples_decoded() const;
103
104 vector<decode::Row> get_visible_rows() const;
105
106 /**
107 * Helper function for static annotation_callback(),
108 * must be public so the function can access it.
109 * Don't use from outside this class.
110 */
111 uint64_t inc_annotation_count();
112
113 /**
114 * Extracts sorted annotations between two period into a vector.
115 */
116 void get_annotation_subset(
117 vector<pv::data::decode::Annotation> &dest,
118 const decode::Row &row, uint64_t start_sample,
119 uint64_t end_sample) const;
120
121 QString error_message();
122
123 void clear();
124
125 uint64_t max_sample_count() const;
126
127 void begin_decode();
128
129private:
130 boost::optional<int64_t> wait_for_data() const;
131
132 void decode_data(const int64_t abs_start_samplenum, const int64_t sample_count,
133 const unsigned int unit_size, srd_session *const session);
134
135 void decode_proc();
136
137 static void annotation_callback(srd_proto_data *pdata, void *decoder_stack);
138
139private Q_SLOTS:
140 void on_new_frame();
141
142 void on_data_received();
143
144 void on_frame_ended();
145
146Q_SIGNALS:
147 void new_annotations();
148
149private:
150 pv::Session &session_;
151
152 pv::util::Timestamp start_time_;
153 double samplerate_;
154
155 /**
156 * This mutex prevents more than one thread from accessing
157 * libsigrokdecode concurrently.
158 * @todo A proper solution should be implemented to allow multiple
159 * decode operations in parallel.
160 */
161 static mutex global_srd_mutex_;
162
163 list< shared_ptr<decode::Decoder> > stack_;
164
165 shared_ptr<pv::data::LogicSegment> segment_;
166
167 mutable mutex input_mutex_;
168 mutable condition_variable input_cond_;
169 int64_t sample_count_, annotation_count_;
170 bool frame_complete_;
171
172 mutable mutex output_mutex_;
173 int64_t samples_decoded_;
174
175 map<const decode::Row, decode::RowData> rows_;
176
177 map<pair<const srd_decoder*, int>, decode::Row> class_rows_;
178
179 QString error_message_;
180
181 std::thread decode_thread_;
182 atomic<bool> interrupt_;
183
184 friend struct DecoderStackTest::TwoDecoderStack;
185};
186
187} // namespace data
188} // namespace pv
189
190#endif // PULSEVIEW_PV_DATA_DECODERSTACK_HPP