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