2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
21 #ifndef PULSEVIEW_PV_DATA_SIGNALBASE_HPP
22 #define PULSEVIEW_PV_DATA_SIGNALBASE_HPP
26 #include <condition_variable>
37 #include <libsigrokcxx/libsigrokcxx.hpp>
39 #include "segment.hpp"
42 using std::condition_variable;
44 using std::enable_shared_from_this;
48 using std::shared_ptr;
67 class SignalGroup : public QObject
72 SignalGroup(const QString& name);
74 void append_signal(shared_ptr<SignalBase> signal);
75 void remove_signal(shared_ptr<SignalBase> signal);
76 deque<shared_ptr<SignalBase>> signals() const;
79 const QString name() const;
82 deque<shared_ptr<SignalBase>> signals_;
87 class SignalBase : public QObject, public enable_shared_from_this<SignalBase>
90 Q_PROPERTY(QString error_message READ get_error_message)
94 AnalogChannel = 1, ///< Analog data
95 LogicChannel, ///< Logic data
96 DecodeChannel, ///< Protocol Decoder channel using libsigrokdecode
97 MathChannel ///< Virtual channel generated by math operations
100 enum ConversionType {
102 A2LConversionByThreshold = 1,
103 A2LConversionBySchmittTrigger = 2
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,
111 enum ConversionPreset {
112 NoPreset = -1, ///< Conversion uses custom values
113 DynamicPreset = 0 ///< Conversion uses calculated values
116 static const QColor AnalogSignalColors[8];
117 static const QColor LogicSignalColors[10];
120 static const int ColorBGAlpha;
121 static const uint64_t ConversionBlockSize;
122 static const uint32_t ConversionDelay;
125 SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type);
126 virtual ~SignalBase();
130 * Returns the underlying SR channel.
131 * Generated channels don't have a SR channel.
133 shared_ptr<sigrok::Channel> channel() const;
136 * Returns whether this channel is generated or a channel associated with the device.
138 bool is_generated() const;
141 * Returns enabled status of this channel.
143 bool enabled() const;
146 * Sets the enabled status of this channel.
147 * @param value Boolean value to set.
149 void set_enabled(bool value);
152 * Gets the type of this channel.
154 ChannelType type() const;
157 * Gets the index number of this channel, i.e. a unique ID assigned by
160 unsigned int index() const;
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.
167 void set_index(unsigned int index);
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.
175 unsigned int logic_bit_index() const;
178 * Sets the signal group this signal belongs to
180 void set_group(SignalGroup* group);
183 * Returns the signal group this signal belongs to or nullptr if none
185 SignalGroup* group() const;
188 * Gets the name of this signal.
190 QString name() const;
193 * Gets the internal name of this signal, i.e. how the device/generator calls it.
195 QString internal_name() const;
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.
201 void set_internal_name(QString internal_name);
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.
207 QString display_name() const;
210 * Sets the name of the signal.
212 virtual void set_name(QString name);
215 * Get the color of the signal.
217 QColor color() const;
220 * Set the color of the signal.
222 void set_color(QColor color);
225 * Get the background color of the signal.
227 QColor bgcolor() const;
230 * Returns the current error message text.
232 virtual QString get_error_message() const;
235 * Sets the internal data object.
237 void set_data(shared_ptr<pv::data::SignalData> data);
240 * Clears all sample data and removes all associated segments.
242 void clear_sample_data();
245 * Get the internal data as analog data object in case of analog type.
247 shared_ptr<pv::data::Analog> analog_data() const;
250 * Get the internal data as logic data object in case of logic type.
252 shared_ptr<pv::data::Logic> logic_data() const;
255 * Determines whether a given segment is complete (i.e. end-of-frame has
256 * been seen). It only considers the original data, not the converted data.
258 bool segment_is_complete(uint32_t segment_id) const;
261 * Determines whether this signal has any sample data at all.
263 bool has_samples() const;
266 * Returns the sample rate for this signal.
268 virtual double get_samplerate() const;
271 * Queries the kind of conversion performed on this channel.
273 ConversionType get_conversion_type() const;
276 * Changes the kind of conversion performed on this channel.
278 * Restarts the conversion.
280 void set_conversion_type(ConversionType t);
283 * Returns all currently known conversion options
285 map<QString, QVariant> get_conversion_options() const;
288 * Sets the value of a particular conversion option
289 * Note: it is not checked whether the option is valid for the
290 * currently conversion. If it's not, it will be silently ignored.
292 * Does not restart the conversion.
294 * @return true if the value is different from before, false otherwise
296 bool set_conversion_option(QString key, QVariant value);
299 * Returns the threshold(s) used for conversions, if applicable.
300 * The resulting thresholds are given for the chosen conversion, so you
301 * can query thresholds also for conversions which aren't currently active.
303 * If you want the thresholds for the currently active conversion,
304 * call it either with NoConversion or no parameter.
306 * @param t the type of conversion to obtain the thresholds for, leave
307 * empty or use NoConversion if you want to query the currently
310 * @param always_custom ignore the currently selected preset and always
311 * return the custom values for this conversion, using 0 if those
314 * @return a list of threshold(s) used by the chosen conversion
316 vector<double> get_conversion_thresholds(
317 const ConversionType t = NoConversion, const bool always_custom=false) const;
320 * Provides all conversion presets available for the currently active
323 * @return a list of description/ID pairs for each preset
325 vector<pair<QString, int> > get_conversion_presets() const;
328 * Determines the ID of the currently used conversion preset, which is only
329 * valid for the currently available conversion presets. It is therefore
330 * suggested to call @ref get_conversion_presets right before calling this.
332 * @return the ID of the currently used conversion preset. -1 if no preset
333 * is used. In that case, a user setting is used instead.
335 ConversionPreset get_current_conversion_preset() const;
338 * Sets the conversion preset to be used.
340 * Does not restart the conversion.
342 * @param id the id of the preset to use
344 void set_conversion_preset(ConversionPreset id);
347 bool is_decode_signal() const;
350 virtual void save_settings(QSettings &settings) const;
351 virtual void restore_settings(QSettings &settings);
353 void start_conversion(bool delayed_start=false);
356 virtual void set_error_message(QString msg);
359 void stop_conversion();
361 bool conversion_is_a2l() const;
363 uint8_t convert_a2l_threshold(float threshold, float value);
364 uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
365 float value, uint8_t &state);
367 void convert_single_segment_range(shared_ptr<AnalogSegment> asegment,
368 shared_ptr<LogicSegment> lsegment, uint64_t start_sample, uint64_t end_sample);
369 void convert_single_segment(shared_ptr<AnalogSegment> asegment,
370 shared_ptr<LogicSegment> lsegment);
371 void conversion_thread_proc();
374 void enabled_changed(const bool &value);
375 void name_changed(const QString &name);
376 void color_changed(const QColor &color);
377 void error_message_changed(const QString &msg);
378 void conversion_type_changed(const ConversionType t);
380 void samples_cleared();
381 void samples_added(uint64_t segment_id, uint64_t start_sample,
382 uint64_t end_sample);
384 void min_max_changed(float min, float max);
387 void on_samples_cleared();
389 void on_samples_added(SharedPtrToSegment segment, uint64_t start_sample,
390 uint64_t end_sample);
392 void on_input_segment_completed();
394 void on_min_max_changed(float min, float max);
396 void on_capture_state_changed(int state);
398 void on_delayed_conversion_start();
401 shared_ptr<sigrok::Channel> channel_;
402 ChannelType channel_type_;
404 shared_ptr<pv::data::SignalData> data_;
405 shared_ptr<pv::data::SignalData> converted_data_;
406 ConversionType conversion_type_;
407 map<QString, QVariant> conversion_options_;
409 float min_value_, max_value_;
411 std::thread conversion_thread_;
412 atomic<bool> conversion_interrupt_;
413 mutex conversion_input_mutex_;
414 condition_variable conversion_input_cond_;
415 QTimer delayed_conversion_starter_;
417 QString internal_name_, name_;
418 QColor color_, bgcolor_;
421 QString error_message_;
427 Q_DECLARE_METATYPE(shared_ptr<pv::data::SignalBase>);
429 #endif // PULSEVIEW_PV_DATA_SIGNALBASE_HPP