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
25 #include <condition_variable>
36 #include <libsigrokcxx/libsigrokcxx.hpp>
38 #include "segment.hpp"
41 using std::condition_variable;
45 using std::shared_ptr;
63 class SignalBase : public QObject
69 AnalogChannel = 1, ///< Analog data
70 LogicChannel, ///< Logic data
71 DecodeChannel, ///< Protocol Decoder channel using libsigrokdecode
72 MathChannel ///< Virtual channel generated by math operations
77 A2LConversionByThreshold = 1,
78 A2LConversionBySchmittTrigger = 2
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,
86 enum ConversionPreset {
87 NoPreset = -1, ///< Conversion uses custom values
88 DynamicPreset = 0 ///< Conversion uses calculated values
92 static const int ColorBGAlpha;
93 static const uint64_t ConversionBlockSize;
94 static const uint32_t ConversionDelay;
97 SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type);
98 virtual ~SignalBase();
102 * Returns the underlying SR channel.
103 * Generated channels don't have a SR channel.
105 shared_ptr<sigrok::Channel> channel() const;
108 * Returns enabled status of this channel.
110 bool enabled() const;
113 * Sets the enabled status of this channel.
114 * @param value Boolean value to set.
116 void set_enabled(bool value);
119 * Gets the type of this channel.
121 ChannelType type() const;
124 * Gets the index number of this channel, i.e. a unique ID assigned by
127 unsigned int index() const;
130 * Sets the index number of this channel, i.e. a unique ID assigned by
131 * the device driver or the logic bit index (see below).
132 * Only use immediately after creating the signal and leave it untouched after.
134 void set_index(unsigned int index);
137 * Returns which bit of a given sample for this signal represents the
138 * signal itself. This is relevant for compound signals like logic,
139 * rather meaningless for everything else but provided in case there
140 * is a conversion active that provides a digital signal using bit #0.
142 unsigned int logic_bit_index() const;
145 * Gets the name of this signal.
147 QString name() const;
150 * Gets the internal name of this signal, i.e. how the device/generator calls it.
152 QString internal_name() const;
155 * Sets the internal name of this signal, i.e. how the device/generator calls it.
156 * Only use immediately after creating the signal and leave it untouched after.
158 void set_internal_name(QString internal_name);
161 * Produces a string for this signal that can be used for display,
162 * i.e. it contains one or both of the signal/internal names.
164 QString display_name() const;
167 * Sets the name of the signal.
169 virtual void set_name(QString name);
172 * Get the color of the signal.
174 QColor color() const;
177 * Set the color of the signal.
179 void set_color(QColor color);
182 * Get the background color of the signal.
184 QColor bgcolor() const;
187 * Sets the internal data object.
189 void set_data(shared_ptr<pv::data::SignalData> data);
192 * Clears all sample data and removes all associated segments.
194 void clear_sample_data();
197 * Get the internal data as analog data object in case of analog type.
199 shared_ptr<pv::data::Analog> analog_data() const;
202 * Get the internal data as logic data object in case of logic type.
204 shared_ptr<pv::data::Logic> logic_data() const;
207 * Determines whether a given segment is complete (i.e. end-of-frame has
208 * been seen). It only considers the original data, not the converted data.
210 bool segment_is_complete(uint32_t segment_id) const;
213 * Determines whether this signal has any sample data at all.
215 bool has_samples() const;
218 * Returns the sample rate for this signal.
220 virtual double get_samplerate() const;
223 * Queries the kind of conversion performed on this channel.
225 ConversionType get_conversion_type() const;
228 * Changes the kind of conversion performed on this channel.
230 * Restarts the conversion.
232 void set_conversion_type(ConversionType t);
235 * Returns all currently known conversion options
237 map<QString, QVariant> get_conversion_options() const;
240 * Sets the value of a particular conversion option
241 * Note: it is not checked whether the option is valid for the
242 * currently conversion. If it's not, it will be silently ignored.
244 * Does not restart the conversion.
246 * @return true if the value is different from before, false otherwise
248 bool set_conversion_option(QString key, QVariant value);
251 * Returns the threshold(s) used for conversions, if applicable.
252 * The resulting thresholds are given for the chosen conversion, so you
253 * can query thresholds also for conversions which aren't currently active.
255 * If you want the thresholds for the currently active conversion,
256 * call it either with NoConversion or no parameter.
258 * @param t the type of conversion to obtain the thresholds for, leave
259 * empty or use NoConversion if you want to query the currently
262 * @param always_custom ignore the currently selected preset and always
263 * return the custom values for this conversion, using 0 if those
266 * @return a list of threshold(s) used by the chosen conversion
268 vector<double> get_conversion_thresholds(
269 const ConversionType t = NoConversion, const bool always_custom=false) const;
272 * Provides all conversion presets available for the currently active
275 * @return a list of description/ID pairs for each preset
277 vector<pair<QString, int> > get_conversion_presets() const;
280 * Determines the ID of the currently used conversion preset, which is only
281 * valid for the currently available conversion presets. It is therefore
282 * suggested to call @ref get_conversion_presets right before calling this.
284 * @return the ID of the currently used conversion preset. -1 if no preset
285 * is used. In that case, a user setting is used instead.
287 ConversionPreset get_current_conversion_preset() const;
290 * Sets the conversion preset to be used.
292 * Does not restart the conversion.
294 * @param id the id of the preset to use
296 void set_conversion_preset(ConversionPreset id);
299 bool is_decode_signal() const;
302 virtual void save_settings(QSettings &settings) const;
304 virtual void restore_settings(QSettings &settings);
306 void start_conversion(bool delayed_start=false);
309 bool conversion_is_a2l() const;
311 uint8_t convert_a2l_threshold(float threshold, float value);
312 uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
313 float value, uint8_t &state);
315 void convert_single_segment_range(AnalogSegment *asegment,
316 LogicSegment *lsegment, uint64_t start_sample, uint64_t end_sample);
317 void convert_single_segment(pv::data::AnalogSegment *asegment,
318 pv::data::LogicSegment *lsegment);
319 void conversion_thread_proc();
321 void stop_conversion();
324 void enabled_changed(const bool &value);
326 void name_changed(const QString &name);
328 void color_changed(const QColor &color);
330 void conversion_type_changed(const ConversionType t);
332 void samples_cleared();
334 void samples_added(uint64_t segment_id, uint64_t start_sample,
335 uint64_t end_sample);
337 void min_max_changed(float min, float max);
340 void on_samples_cleared();
342 void on_samples_added(SharedPtrToSegment segment, uint64_t start_sample,
343 uint64_t end_sample);
345 void on_min_max_changed(float min, float max);
347 void on_capture_state_changed(int state);
349 void on_delayed_conversion_start();
352 shared_ptr<sigrok::Channel> channel_;
353 ChannelType channel_type_;
354 shared_ptr<pv::data::SignalData> data_;
355 shared_ptr<pv::data::SignalData> converted_data_;
356 ConversionType conversion_type_;
357 map<QString, QVariant> conversion_options_;
359 float min_value_, max_value_;
361 std::thread conversion_thread_;
362 atomic<bool> conversion_interrupt_;
363 mutex conversion_input_mutex_;
364 condition_variable conversion_input_cond_;
365 QTimer delayed_conversion_starter_;
367 QString internal_name_, name_;
368 QColor color_, bgcolor_;
375 #endif // PULSEVIEW_PV_DATA_SIGNALBASE_HPP