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