]> sigrok.org Git - pulseview.git/blame - pv/data/signalbase.hpp
Use shared_ptr for async samples_added() notification
[pulseview.git] / pv / data / signalbase.hpp
CommitLineData
bf0edd2b
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
efdec55a 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bf0edd2b
SA
19 */
20
21#ifndef PULSEVIEW_PV_DATA_SIGNALBASE_HPP
22#define PULSEVIEW_PV_DATA_SIGNALBASE_HPP
23
27a3f09b
SA
24#include <atomic>
25#include <condition_variable>
12ea3616 26#include <thread>
52c900ac 27#include <vector>
12ea3616 28
bf0edd2b
SA
29#include <QColor>
30#include <QObject>
6de38b17 31#include <QSettings>
bf0edd2b 32#include <QString>
932bc246 33#include <QTimer>
52c900ac 34#include <QVariant>
bf0edd2b
SA
35
36#include <libsigrokcxx/libsigrokcxx.hpp>
37
720f4762
SA
38#include "segment.hpp"
39
27a3f09b
SA
40using std::atomic;
41using std::condition_variable;
52c900ac 42using std::map;
27a3f09b 43using std::mutex;
52c900ac 44using std::pair;
6f925ba9 45using std::shared_ptr;
52c900ac 46using std::vector;
bf0edd2b
SA
47
48namespace sigrok {
49class Channel;
bf0edd2b
SA
50}
51
52namespace pv {
53namespace data {
54
cbd2a2de 55class Analog;
63253d72 56class AnalogSegment;
bb7dd726 57class DecoderStack;
cbd2a2de 58class Logic;
63253d72 59class LogicSegment;
1f3033cb 60class Segment;
cbd2a2de
SA
61class SignalData;
62
bf0edd2b
SA
63class SignalBase : public QObject
64{
65 Q_OBJECT
66
472a80c5
SA
67public:
68 enum ChannelType {
64845ac2
SA
69 AnalogChannel = 1, ///< Analog data
70 LogicChannel, ///< Logic data
71 DecodeChannel, ///< Protocol Decoder channel using libsigrokdecode
64845ac2 72 MathChannel ///< Virtual channel generated by math operations
472a80c5
SA
73 };
74
12ea3616
SA
75 enum ConversionType {
76 NoConversion = 0,
b9cdbe03 77 A2LConversionByThreshold = 1,
12ea3616
SA
78 A2LConversionBySchmittTrigger = 2
79 };
80
f0f9c856
SA
81 /**
82 * Conversion presets range from -1 to n, where 1..n are dependent on
83 * the conversion these presets apply to. -1 and 0 have fixed meanings,
84 * however.
85 */
86 enum ConversionPreset {
87 NoPreset = -1, ///< Conversion uses custom values
88 DynamicPreset = 0 ///< Conversion uses calculated values
89 };
90
bf0edd2b 91private:
641574bc 92 static const int ColorBGAlpha;
bcaf0334 93 static const uint64_t ConversionBlockSize;
932bc246 94 static const uint32_t ConversionDelay;
bf0edd2b
SA
95
96public:
472a80c5 97 SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type);
12ea3616 98 virtual ~SignalBase();
bf0edd2b
SA
99
100public:
101 /**
102 * Returns the underlying SR channel.
103 */
6f925ba9 104 shared_ptr<sigrok::Channel> channel() const;
bf0edd2b
SA
105
106 /**
107 * Returns enabled status of this channel.
108 */
109 bool enabled() const;
110
111 /**
112 * Sets the enabled status of this channel.
113 * @param value Boolean value to set.
114 */
115 void set_enabled(bool value);
116
117 /**
118 * Gets the type of this channel.
119 */
472a80c5 120 ChannelType type() const;
bf0edd2b
SA
121
122 /**
27a3f09b
SA
123 * Gets the index number of this channel, i.e. a unique ID assigned by
124 * the device driver.
bf0edd2b
SA
125 */
126 unsigned int index() const;
127
27a3f09b
SA
128 /**
129 * Returns which bit of a given sample for this signal represents the
130 * signal itself. This is relevant for compound signals like logic,
131 * rather meaningless for everything else but provided in case there
132 * is a conversion active that provides a digital signal using bit #0.
133 */
134 unsigned int logic_bit_index() const;
135
bf0edd2b
SA
136 /**
137 * Gets the name of this signal.
138 */
139 QString name() const;
140
050b5a6c
SA
141 /**
142 * Gets the internal name of this signal, i.e. how the device calls it.
143 */
144 QString internal_name() const;
145
b5d20c6d
SA
146 /**
147 * Produces a string for this signal that can be used for display,
148 * i.e. it contains one or both of the signal/internal names.
149 */
150 QString display_name() const;
151
bf0edd2b
SA
152 /**
153 * Sets the name of the signal.
154 */
155 virtual void set_name(QString name);
156
157 /**
641574bc 158 * Get the color of the signal.
bf0edd2b 159 */
641574bc 160 QColor color() const;
bf0edd2b
SA
161
162 /**
641574bc 163 * Set the color of the signal.
bf0edd2b 164 */
641574bc 165 void set_color(QColor color);
bf0edd2b
SA
166
167 /**
641574bc 168 * Get the background color of the signal.
bf0edd2b 169 */
641574bc 170 QColor bgcolor() const;
bf0edd2b 171
cbd2a2de
SA
172 /**
173 * Sets the internal data object.
174 */
6f925ba9 175 void set_data(shared_ptr<pv::data::SignalData> data);
cbd2a2de 176
464f05d5
SA
177 /**
178 * Clears all sample data and removes all associated segments.
179 */
180 void clear_sample_data();
181
cbd2a2de
SA
182 /**
183 * Get the internal data as analog data object in case of analog type.
184 */
6f925ba9 185 shared_ptr<pv::data::Analog> analog_data() const;
cbd2a2de
SA
186
187 /**
188 * Get the internal data as logic data object in case of logic type.
189 */
6f925ba9 190 shared_ptr<pv::data::Logic> logic_data() const;
cbd2a2de 191
558ad6ce
SA
192 /**
193 * Determines whether a given segment is complete (i.e. end-of-frame has
194 * been seen). It only considers the original data, not the converted data.
195 */
196 bool segment_is_complete(uint32_t segment_id) const;
197
d13d95b3
SA
198 /**
199 * Determines whether this signal has any sample data at all.
200 */
201 bool has_samples() const;
202
eeceee99
SA
203 /**
204 * Returns the sample rate for this signal.
205 */
85125b0f 206 virtual double get_samplerate() const;
eeceee99 207
06b6ce26
SA
208 /**
209 * Queries the kind of conversion performed on this channel.
210 */
211 ConversionType get_conversion_type() const;
212
12ea3616
SA
213 /**
214 * Changes the kind of conversion performed on this channel.
52c900ac
SA
215 *
216 * Restarts the conversion.
12ea3616
SA
217 */
218 void set_conversion_type(ConversionType t);
219
52c900ac
SA
220 /**
221 * Returns all currently known conversion options
222 */
223 map<QString, QVariant> get_conversion_options() const;
224
225 /**
226 * Sets the value of a particular conversion option
227 * Note: it is not checked whether the option is valid for the
228 * currently conversion. If it's not, it will be silently ignored.
229 *
230 * Does not restart the conversion.
231 *
232 * @return true if the value is different from before, false otherwise
233 */
234 bool set_conversion_option(QString key, QVariant value);
235
236 /**
237 * Returns the threshold(s) used for conversions, if applicable.
238 * The resulting thresholds are given for the chosen conversion, so you
239 * can query thresholds also for conversions which aren't currently active.
240 *
241 * If you want the thresholds for the currently active conversion,
242 * call it either with NoConversion or no parameter.
243 *
244 * @param t the type of conversion to obtain the thresholds for, leave
245 * empty or use NoConversion if you want to query the currently
246 * used conversion
247 *
248 * @param always_custom ignore the currently selected preset and always
249 * return the custom values for this conversion, using 0 if those
250 * aren't set
251 *
252 * @return a list of threshold(s) used by the chosen conversion
253 */
254 vector<double> get_conversion_thresholds(
255 const ConversionType t = NoConversion, const bool always_custom=false) const;
256
257 /**
258 * Provides all conversion presets available for the currently active
259 * conversion.
260 *
261 * @return a list of description/ID pairs for each preset
262 */
263 vector<pair<QString, int> > get_conversion_presets() const;
264
265 /**
266 * Determines the ID of the currently used conversion preset, which is only
267 * valid for the currently available conversion presets. It is therefore
268 * suggested to call @ref get_conversion_presets right before calling this.
269 *
270 * @return the ID of the currently used conversion preset. -1 if no preset
271 * is used. In that case, a user setting is used instead.
272 */
f0f9c856 273 ConversionPreset get_current_conversion_preset() const;
52c900ac
SA
274
275 /**
276 * Sets the conversion preset to be used.
277 *
278 * Does not restart the conversion.
279 *
280 * @param id the id of the preset to use
281 */
f0f9c856 282 void set_conversion_preset(ConversionPreset id);
52c900ac 283
bb7dd726 284#ifdef ENABLE_DECODE
47747218 285 bool is_decode_signal() const;
bb7dd726 286#endif
cbd2a2de 287
47747218 288 virtual void save_settings(QSettings &settings) const;
6de38b17 289
47747218 290 virtual void restore_settings(QSettings &settings);
6de38b17 291
932bc246 292 void start_conversion(bool delayed_start=false);
52c900ac 293
12ea3616 294private:
ccccb914
SA
295 bool conversion_is_a2l() const;
296
12ea3616 297 uint8_t convert_a2l_threshold(float threshold, float value);
5d9fe823
SA
298 uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
299 float value, uint8_t &state);
12ea3616 300
784f6c75
SA
301 void convert_single_segment_range(AnalogSegment *asegment,
302 LogicSegment *lsegment, uint64_t start_sample, uint64_t end_sample);
63253d72
SA
303 void convert_single_segment(pv::data::AnalogSegment *asegment,
304 pv::data::LogicSegment *lsegment);
305 void conversion_thread_proc();
27a3f09b 306
27a3f09b 307 void stop_conversion();
12ea3616 308
bf0edd2b
SA
309Q_SIGNALS:
310 void enabled_changed(const bool &value);
311
312 void name_changed(const QString &name);
313
641574bc 314 void color_changed(const QColor &color);
bf0edd2b 315
12ea3616
SA
316 void conversion_type_changed(const ConversionType t);
317
eae3bbbb
SA
318 void samples_cleared();
319
7f894d95 320 void samples_added(uint64_t segment_id, uint64_t start_sample,
eae3bbbb
SA
321 uint64_t end_sample);
322
c6d9cf65
SA
323 void min_max_changed(float min, float max);
324
12ea3616
SA
325private Q_SLOTS:
326 void on_samples_cleared();
327
720f4762 328 void on_samples_added(SharedPtrToSegment segment, uint64_t start_sample,
12ea3616
SA
329 uint64_t end_sample);
330
8e15445c
SA
331 void on_min_max_changed(float min, float max);
332
12ea3616
SA
333 void on_capture_state_changed(int state);
334
932bc246
SA
335 void on_delayed_conversion_start();
336
ad908057 337protected:
6f925ba9 338 shared_ptr<sigrok::Channel> channel_;
472a80c5 339 ChannelType channel_type_;
6f925ba9 340 shared_ptr<pv::data::SignalData> data_;
12ea3616 341 shared_ptr<pv::data::SignalData> converted_data_;
06b6ce26 342 ConversionType conversion_type_;
52c900ac
SA
343 map<QString, QVariant> conversion_options_;
344
345 float min_value_, max_value_;
cbd2a2de 346
12ea3616 347 std::thread conversion_thread_;
27a3f09b
SA
348 atomic<bool> conversion_interrupt_;
349 mutex conversion_input_mutex_;
350 condition_variable conversion_input_cond_;
932bc246 351 QTimer delayed_conversion_starter_;
12ea3616 352
050b5a6c 353 QString internal_name_, name_;
641574bc 354 QColor color_, bgcolor_;
bf0edd2b
SA
355};
356
357} // namespace data
358} // namespace pv
359
360#endif // PULSEVIEW_PV_DATA_SIGNALBASE_HPP