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