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