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