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