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>
39 using std::condition_variable;
43 using std::shared_ptr;
60 class SignalBase : public QObject
66 AnalogChannel = 1, ///< Analog data
67 LogicChannel, ///< Logic data
68 DecodeChannel, ///< Protocol Decoder channel using libsigrokdecode
69 MathChannel ///< Virtual channel generated by math operations
74 A2LConversionByThreshold = 1,
75 A2LConversionBySchmittTrigger = 2
79 * Conversion presets range from -1 to n, where 1..n are dependent on
80 * the conversion these presets apply to. -1 and 0 have fixed meanings,
83 enum ConversionPreset {
84 NoPreset = -1, ///< Conversion uses custom values
85 DynamicPreset = 0 ///< Conversion uses calculated values
89 static const int ColourBGAlpha;
90 static const uint64_t ConversionBlockSize;
91 static const uint32_t ConversionDelay;
94 SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type);
95 virtual ~SignalBase();
99 * Returns the underlying SR channel.
101 shared_ptr<sigrok::Channel> channel() const;
104 * Returns enabled status of this channel.
106 bool enabled() const;
109 * Sets the enabled status of this channel.
110 * @param value Boolean value to set.
112 void set_enabled(bool value);
115 * Gets the type of this channel.
117 ChannelType type() const;
120 * Gets the index number of this channel, i.e. a unique ID assigned by
123 unsigned int index() const;
126 * Returns which bit of a given sample for this signal represents the
127 * signal itself. This is relevant for compound signals like logic,
128 * rather meaningless for everything else but provided in case there
129 * is a conversion active that provides a digital signal using bit #0.
131 unsigned int logic_bit_index() const;
134 * Gets the name of this signal.
136 QString name() const;
139 * Gets the internal name of this signal, i.e. how the device calls it.
141 QString internal_name() const;
144 * Produces a string for this signal that can be used for display,
145 * i.e. it contains one or both of the signal/internal names.
147 QString display_name() const;
150 * Sets the name of the signal.
152 virtual void set_name(QString name);
155 * Get the colour of the signal.
157 QColor colour() const;
160 * Set the colour of the signal.
162 void set_colour(QColor colour);
165 * Get the background colour of the signal.
167 QColor bgcolour() const;
170 * Sets the internal data object.
172 void set_data(shared_ptr<pv::data::SignalData> data);
175 * Get the internal data as analog data object in case of analog type.
177 shared_ptr<pv::data::Analog> analog_data() const;
180 * Get the internal data as logic data object in case of logic type.
182 shared_ptr<pv::data::Logic> logic_data() const;
185 * Determines whether a given segment is complete (i.e. end-of-frame has
186 * been seen). It only considers the original data, not the converted data.
188 bool segment_is_complete(uint32_t segment_id) const;
191 * Queries the kind of conversion performed on this channel.
193 ConversionType get_conversion_type() const;
196 * Changes the kind of conversion performed on this channel.
198 * Restarts the conversion.
200 void set_conversion_type(ConversionType t);
203 * Returns all currently known conversion options
205 map<QString, QVariant> get_conversion_options() const;
208 * Sets the value of a particular conversion option
209 * Note: it is not checked whether the option is valid for the
210 * currently conversion. If it's not, it will be silently ignored.
212 * Does not restart the conversion.
214 * @return true if the value is different from before, false otherwise
216 bool set_conversion_option(QString key, QVariant value);
219 * Returns the threshold(s) used for conversions, if applicable.
220 * The resulting thresholds are given for the chosen conversion, so you
221 * can query thresholds also for conversions which aren't currently active.
223 * If you want the thresholds for the currently active conversion,
224 * call it either with NoConversion or no parameter.
226 * @param t the type of conversion to obtain the thresholds for, leave
227 * empty or use NoConversion if you want to query the currently
230 * @param always_custom ignore the currently selected preset and always
231 * return the custom values for this conversion, using 0 if those
234 * @return a list of threshold(s) used by the chosen conversion
236 vector<double> get_conversion_thresholds(
237 const ConversionType t = NoConversion, const bool always_custom=false) const;
240 * Provides all conversion presets available for the currently active
243 * @return a list of description/ID pairs for each preset
245 vector<pair<QString, int> > get_conversion_presets() const;
248 * Determines the ID of the currently used conversion preset, which is only
249 * valid for the currently available conversion presets. It is therefore
250 * suggested to call @ref get_conversion_presets right before calling this.
252 * @return the ID of the currently used conversion preset. -1 if no preset
253 * is used. In that case, a user setting is used instead.
255 ConversionPreset get_current_conversion_preset() const;
258 * Sets the conversion preset to be used.
260 * Does not restart the conversion.
262 * @param id the id of the preset to use
264 void set_conversion_preset(ConversionPreset id);
267 bool is_decode_signal() const;
270 virtual void save_settings(QSettings &settings) const;
272 virtual void restore_settings(QSettings &settings);
274 void start_conversion(bool delayed_start=false);
277 bool conversion_is_a2l() const;
279 uint8_t convert_a2l_threshold(float threshold, float value);
280 uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
281 float value, uint8_t &state);
283 void convert_single_segment_range(AnalogSegment *asegment,
284 LogicSegment *lsegment, uint64_t start_sample, uint64_t end_sample);
285 void convert_single_segment(pv::data::AnalogSegment *asegment,
286 pv::data::LogicSegment *lsegment);
287 void conversion_thread_proc();
289 void stop_conversion();
292 void enabled_changed(const bool &value);
294 void name_changed(const QString &name);
296 void colour_changed(const QColor &colour);
298 void conversion_type_changed(const ConversionType t);
300 void samples_cleared();
302 void samples_added(uint64_t segment_id, uint64_t start_sample,
303 uint64_t end_sample);
305 void min_max_changed(float min, float max);
308 void on_samples_cleared();
310 void on_samples_added(QObject* segment, uint64_t start_sample,
311 uint64_t end_sample);
313 void on_min_max_changed(float min, float max);
315 void on_capture_state_changed(int state);
317 void on_delayed_conversion_start();
320 shared_ptr<sigrok::Channel> channel_;
321 ChannelType channel_type_;
322 shared_ptr<pv::data::SignalData> data_;
323 shared_ptr<pv::data::SignalData> converted_data_;
324 ConversionType conversion_type_;
325 map<QString, QVariant> conversion_options_;
327 float min_value_, max_value_;
329 std::thread conversion_thread_;
330 atomic<bool> conversion_interrupt_;
331 mutex conversion_input_mutex_;
332 condition_variable conversion_input_cond_;
333 QTimer delayed_conversion_starter_;
335 QString internal_name_, name_;
336 QColor colour_, bgcolour_;
342 #endif // PULSEVIEW_PV_DATA_SIGNALBASE_HPP