]> sigrok.org Git - pulseview.git/blob - pv/data/signalbase.hpp
Use identifiers for fixed conversion preset values
[pulseview.git] / pv / data / signalbase.hpp
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 <condition_variable>
26 #include <thread>
27 #include <vector>
28
29 #include <QColor>
30 #include <QObject>
31 #include <QSettings>
32 #include <QString>
33 #include <QVariant>
34
35 #include <libsigrokcxx/libsigrokcxx.hpp>
36
37 using std::atomic;
38 using std::condition_variable;
39 using std::map;
40 using std::mutex;
41 using std::pair;
42 using std::shared_ptr;
43 using std::vector;
44
45 namespace sigrok {
46 class Channel;
47 }
48
49 namespace pv {
50 namespace data {
51
52 class Analog;
53 class DecoderStack;
54 class Logic;
55 class SignalData;
56
57 class SignalBase : public QObject
58 {
59         Q_OBJECT
60
61 public:
62         enum ChannelType {
63                 AnalogChannel = 1, ///< Analog data
64                 LogicChannel,  ///< Logic data
65                 DecodeChannel, ///< Protocol Decoder channel using libsigrokdecode
66                 A2LChannel,    ///< Analog converted to logic, joint representation
67                 MathChannel    ///< Virtual channel generated by math operations
68         };
69
70         enum ConversionType {
71                 NoConversion = 0,
72                 A2LConversionByThreshold = 1,
73                 A2LConversionBySchmittTrigger = 2
74         };
75
76         /**
77          * Conversion presets range from -1 to n, where 1..n are dependent on
78          * the conversion these presets apply to. -1 and 0 have fixed meanings,
79          * however.
80          */
81         enum ConversionPreset {
82                 NoPreset = -1,     ///< Conversion uses custom values
83                 DynamicPreset = 0  ///< Conversion uses calculated values
84         };
85
86 private:
87         static const int ColourBGAlpha;
88         static const uint64_t ConversionBlockSize;
89
90 public:
91         SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type);
92         virtual ~SignalBase();
93
94 public:
95         /**
96          * Returns the underlying SR channel.
97          */
98         shared_ptr<sigrok::Channel> channel() const;
99
100         /**
101          * Returns enabled status of this channel.
102          */
103         bool enabled() const;
104
105         /**
106          * Sets the enabled status of this channel.
107          * @param value Boolean value to set.
108          */
109         void set_enabled(bool value);
110
111         /**
112          * Gets the type of this channel.
113          */
114         ChannelType type() const;
115
116         /**
117          * Gets the index number of this channel, i.e. a unique ID assigned by
118          * the device driver.
119          */
120         unsigned int index() const;
121
122         /**
123          * Returns which bit of a given sample for this signal represents the
124          * signal itself. This is relevant for compound signals like logic,
125          * rather meaningless for everything else but provided in case there
126          * is a conversion active that provides a digital signal using bit #0.
127          */
128         unsigned int logic_bit_index() const;
129
130         /**
131          * Gets the name of this signal.
132          */
133         QString name() const;
134
135         /**
136          * Gets the internal name of this signal, i.e. how the device calls it.
137          */
138         QString internal_name() const;
139
140         /**
141          * Sets the name of the signal.
142          */
143         virtual void set_name(QString name);
144
145         /**
146          * Get the colour of the signal.
147          */
148         QColor colour() const;
149
150         /**
151          * Set the colour of the signal.
152          */
153         void set_colour(QColor colour);
154
155         /**
156          * Get the background colour of the signal.
157          */
158         QColor bgcolour() const;
159
160         /**
161          * Sets the internal data object.
162          */
163         void set_data(shared_ptr<pv::data::SignalData> data);
164
165         /**
166          * Get the internal data as analog data object in case of analog type.
167          */
168         shared_ptr<pv::data::Analog> analog_data() const;
169
170         /**
171          * Get the internal data as logic data object in case of logic type.
172          */
173         shared_ptr<pv::data::Logic> logic_data() const;
174
175         /**
176          * Queries the kind of conversion performed on this channel.
177          */
178         ConversionType get_conversion_type() const;
179
180         /**
181          * Changes the kind of conversion performed on this channel.
182          *
183          * Restarts the conversion.
184          */
185         void set_conversion_type(ConversionType t);
186
187         /**
188          * Returns all currently known conversion options
189          */
190         map<QString, QVariant> get_conversion_options() const;
191
192         /**
193          * Sets the value of a particular conversion option
194          * Note: it is not checked whether the option is valid for the
195          * currently conversion. If it's not, it will be silently ignored.
196          *
197          * Does not restart the conversion.
198          *
199          * @return true if the value is different from before, false otherwise
200          */
201         bool set_conversion_option(QString key, QVariant value);
202
203         /**
204          * Returns the threshold(s) used for conversions, if applicable.
205          * The resulting thresholds are given for the chosen conversion, so you
206          * can query thresholds also for conversions which aren't currently active.
207          *
208          * If you want the thresholds for the currently active conversion,
209          * call it either with NoConversion or no parameter.
210          *
211          * @param t the type of conversion to obtain the thresholds for, leave
212          *          empty or use NoConversion if you want to query the currently
213          *          used conversion
214          *
215          * @param always_custom ignore the currently selected preset and always
216          *        return the custom values for this conversion, using 0 if those
217          *        aren't set
218          *
219          * @return a list of threshold(s) used by the chosen conversion
220          */
221         vector<double> get_conversion_thresholds(
222                 const ConversionType t = NoConversion, const bool always_custom=false) const;
223
224         /**
225          * Provides all conversion presets available for the currently active
226          * conversion.
227          *
228          * @return a list of description/ID pairs for each preset
229          */
230         vector<pair<QString, int> > get_conversion_presets() const;
231
232         /**
233          * Determines the ID of the currently used conversion preset, which is only
234          * valid for the currently available conversion presets. It is therefore
235          * suggested to call @ref get_conversion_presets right before calling this.
236          *
237          * @return the ID of the currently used conversion preset. -1 if no preset
238          *         is used. In that case, a user setting is used instead.
239          */
240         ConversionPreset get_current_conversion_preset() const;
241
242         /**
243          * Sets the conversion preset to be used.
244          *
245          * Does not restart the conversion.
246          *
247          * @param id the id of the preset to use
248          */
249         void set_conversion_preset(ConversionPreset id);
250
251 #ifdef ENABLE_DECODE
252         bool is_decode_signal() const;
253 #endif
254
255         virtual void save_settings(QSettings &settings) const;
256
257         virtual void restore_settings(QSettings &settings);
258
259         void start_conversion();
260
261 private:
262         bool conversion_is_a2l() const;
263
264         uint8_t convert_a2l_threshold(float threshold, float value);
265         uint8_t convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
266                 float value, uint8_t &state);
267
268         void conversion_thread_proc(QObject* segment);
269
270         void stop_conversion();
271
272 Q_SIGNALS:
273         void enabled_changed(const bool &value);
274
275         void name_changed(const QString &name);
276
277         void colour_changed(const QColor &colour);
278
279         void conversion_type_changed(const ConversionType t);
280
281         void samples_cleared();
282
283         void samples_added(QObject* segment, uint64_t start_sample,
284                 uint64_t end_sample);
285
286 private Q_SLOTS:
287         void on_samples_cleared();
288
289         void on_samples_added(QObject* segment, uint64_t start_sample,
290                 uint64_t end_sample);
291
292         void on_min_max_changed(float min, float max);
293
294         void on_capture_state_changed(int state);
295
296 protected:
297         shared_ptr<sigrok::Channel> channel_;
298         ChannelType channel_type_;
299         shared_ptr<pv::data::SignalData> data_;
300         shared_ptr<pv::data::SignalData> converted_data_;
301         ConversionType conversion_type_;
302         map<QString, QVariant> conversion_options_;
303
304         float min_value_, max_value_;
305
306         std::thread conversion_thread_;
307         atomic<bool> conversion_interrupt_;
308         mutex conversion_input_mutex_;
309         condition_variable conversion_input_cond_;
310
311         QString internal_name_, name_;
312         QColor colour_, bgcolour_;
313 };
314
315 } // namespace data
316 } // namespace pv
317
318 #endif // PULSEVIEW_PV_DATA_SIGNALBASE_HPP