]>
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 | ||
cbd2a2de | 21 | #include "analog.hpp" |
12ea3616 | 22 | #include "analogsegment.hpp" |
aca9aa83 | 23 | #include "decode/row.hpp" |
cbd2a2de | 24 | #include "logic.hpp" |
12ea3616 | 25 | #include "logicsegment.hpp" |
bf0edd2b | 26 | #include "signalbase.hpp" |
cbd2a2de | 27 | #include "signaldata.hpp" |
bb7dd726 SA |
28 | |
29 | #include <pv/binding/decoder.hpp> | |
aca9aa83 | 30 | #include <pv/session.hpp> |
bf0edd2b | 31 | |
cbd2a2de | 32 | using std::dynamic_pointer_cast; |
12ea3616 | 33 | using std::make_shared; |
bf0edd2b | 34 | using std::shared_ptr; |
12ea3616 | 35 | using std::tie; |
bf0edd2b SA |
36 | |
37 | namespace pv { | |
38 | namespace data { | |
39 | ||
c063290a | 40 | const int SignalBase::ColourBGAlpha = 8 * 256 / 100; |
bf0edd2b | 41 | |
472a80c5 SA |
42 | SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) : |
43 | channel_(channel), | |
12ea3616 SA |
44 | channel_type_(channel_type), |
45 | conversion_type_(NoConversion) | |
bf0edd2b | 46 | { |
050b5a6c SA |
47 | if (channel_) |
48 | internal_name_ = QString::fromStdString(channel_->name()); | |
bf0edd2b SA |
49 | } |
50 | ||
12ea3616 SA |
51 | SignalBase::~SignalBase() |
52 | { | |
53 | // Wait for the currently ongoing conversion to finish | |
54 | if (conversion_thread_.joinable()) | |
55 | conversion_thread_.join(); | |
56 | } | |
57 | ||
bf0edd2b SA |
58 | shared_ptr<sigrok::Channel> SignalBase::channel() const |
59 | { | |
60 | return channel_; | |
61 | } | |
62 | ||
63 | QString SignalBase::name() const | |
64 | { | |
65 | return (channel_) ? QString::fromStdString(channel_->name()) : name_; | |
66 | } | |
67 | ||
050b5a6c SA |
68 | QString SignalBase::internal_name() const |
69 | { | |
70 | return internal_name_; | |
71 | } | |
72 | ||
bf0edd2b SA |
73 | void SignalBase::set_name(QString name) |
74 | { | |
75 | if (channel_) | |
76 | channel_->set_name(name.toUtf8().constData()); | |
77 | ||
78 | name_ = name; | |
79 | ||
80 | name_changed(name); | |
81 | } | |
82 | ||
83 | bool SignalBase::enabled() const | |
84 | { | |
85 | return (channel_) ? channel_->enabled() : true; | |
86 | } | |
87 | ||
88 | void SignalBase::set_enabled(bool value) | |
89 | { | |
90 | if (channel_) { | |
91 | channel_->set_enabled(value); | |
92 | enabled_changed(value); | |
93 | } | |
94 | } | |
95 | ||
472a80c5 | 96 | SignalBase::ChannelType SignalBase::type() const |
bf0edd2b | 97 | { |
472a80c5 | 98 | return channel_type_; |
bf0edd2b SA |
99 | } |
100 | ||
101 | unsigned int SignalBase::index() const | |
102 | { | |
103 | return (channel_) ? channel_->index() : (unsigned int)-1; | |
104 | } | |
105 | ||
106 | QColor SignalBase::colour() const | |
107 | { | |
108 | return colour_; | |
109 | } | |
110 | ||
111 | void SignalBase::set_colour(QColor colour) | |
112 | { | |
113 | colour_ = colour; | |
114 | ||
115 | bgcolour_ = colour; | |
116 | bgcolour_.setAlpha(ColourBGAlpha); | |
117 | ||
118 | colour_changed(colour); | |
119 | } | |
120 | ||
121 | QColor SignalBase::bgcolour() const | |
122 | { | |
123 | return bgcolour_; | |
124 | } | |
125 | ||
cbd2a2de SA |
126 | void SignalBase::set_data(shared_ptr<pv::data::SignalData> data) |
127 | { | |
12ea3616 SA |
128 | if (data_ && channel_type_ == AnalogChannel) { |
129 | shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_); | |
130 | ||
131 | disconnect(analog_data.get(), SIGNAL(samples_cleared()), | |
132 | this, SLOT(on_samples_cleared())); | |
133 | disconnect(analog_data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)), | |
134 | this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t))); | |
135 | } | |
136 | ||
cbd2a2de | 137 | data_ = data; |
12ea3616 SA |
138 | |
139 | if (data_ && channel_type_ == AnalogChannel) { | |
140 | shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_); | |
141 | ||
142 | connect(analog_data.get(), SIGNAL(samples_cleared()), | |
143 | this, SLOT(on_samples_cleared())); | |
144 | connect(analog_data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)), | |
145 | this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t))); | |
146 | } | |
cbd2a2de SA |
147 | } |
148 | ||
149 | shared_ptr<data::Analog> SignalBase::analog_data() const | |
150 | { | |
12ea3616 SA |
151 | shared_ptr<Analog> result = nullptr; |
152 | ||
472a80c5 | 153 | if (channel_type_ == AnalogChannel) |
12ea3616 SA |
154 | result = dynamic_pointer_cast<Analog>(data_); |
155 | ||
156 | return result; | |
cbd2a2de SA |
157 | } |
158 | ||
159 | shared_ptr<data::Logic> SignalBase::logic_data() const | |
160 | { | |
12ea3616 SA |
161 | shared_ptr<Logic> result = nullptr; |
162 | ||
472a80c5 | 163 | if (channel_type_ == LogicChannel) |
12ea3616 SA |
164 | result = dynamic_pointer_cast<Logic>(data_); |
165 | ||
166 | if (((conversion_type_ == A2LConversionByTreshold) || | |
167 | (conversion_type_ == A2LConversionBySchmittTrigger))) | |
168 | result = dynamic_pointer_cast<Logic>(converted_data_); | |
169 | ||
170 | return result; | |
171 | } | |
172 | ||
173 | void SignalBase::set_conversion_type(ConversionType t) | |
174 | { | |
175 | if (conversion_type_ != NoConversion) { | |
176 | // Wait for the currently ongoing conversion to finish | |
177 | if (conversion_thread_.joinable()) | |
178 | conversion_thread_.join(); | |
179 | ||
180 | // Discard converted data | |
181 | converted_data_.reset(); | |
182 | } | |
183 | ||
184 | conversion_type_ = t; | |
185 | ||
186 | if ((channel_type_ == AnalogChannel) && | |
187 | ((conversion_type_ == A2LConversionByTreshold) || | |
188 | (conversion_type_ == A2LConversionBySchmittTrigger))) { | |
189 | ||
190 | shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_); | |
191 | ||
192 | if (analog_data->analog_segments().size() > 0) { | |
193 | AnalogSegment *asegment = analog_data->analog_segments().front().get(); | |
194 | ||
195 | // Begin conversion of existing sample data | |
196 | // TODO Support for multiple segments is missing | |
197 | on_samples_added(asegment, 0, 0); | |
198 | } | |
199 | } | |
200 | ||
201 | conversion_type_changed(t); | |
cbd2a2de SA |
202 | } |
203 | ||
bb7dd726 SA |
204 | #ifdef ENABLE_DECODE |
205 | bool SignalBase::is_decode_signal() const | |
206 | { | |
207 | return (decoder_stack_ != nullptr); | |
208 | } | |
209 | ||
6f925ba9 | 210 | shared_ptr<pv::data::DecoderStack> SignalBase::decoder_stack() const |
bb7dd726 SA |
211 | { |
212 | return decoder_stack_; | |
213 | } | |
214 | ||
6f925ba9 | 215 | void SignalBase::set_decoder_stack(shared_ptr<pv::data::DecoderStack> |
bb7dd726 SA |
216 | decoder_stack) |
217 | { | |
218 | decoder_stack_ = decoder_stack; | |
219 | } | |
220 | #endif | |
cbd2a2de | 221 | |
6de38b17 SA |
222 | void SignalBase::save_settings(QSettings &settings) const |
223 | { | |
224 | settings.setValue("name", name()); | |
225 | settings.setValue("enabled", enabled()); | |
226 | settings.setValue("colour", colour()); | |
12ea3616 | 227 | settings.setValue("conversion_type", (int)conversion_type_); |
6de38b17 SA |
228 | } |
229 | ||
230 | void SignalBase::restore_settings(QSettings &settings) | |
231 | { | |
232 | set_name(settings.value("name").toString()); | |
233 | set_enabled(settings.value("enabled").toBool()); | |
234 | set_colour(settings.value("colour").value<QColor>()); | |
12ea3616 SA |
235 | set_conversion_type((ConversionType)settings.value("conversion_type").toInt()); |
236 | } | |
237 | ||
238 | uint8_t SignalBase::convert_a2l_threshold(float threshold, float value) | |
239 | { | |
240 | return (value >= threshold) ? 1 : 0; | |
241 | } | |
242 | ||
5d9fe823 SA |
243 | uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr, |
244 | float value, uint8_t &state) | |
12ea3616 | 245 | { |
12ea3616 SA |
246 | if (value < lo_thr) |
247 | state = 0; | |
248 | else if (value > hi_thr) | |
249 | state = 1; | |
250 | ||
251 | return state; | |
252 | } | |
253 | ||
254 | void SignalBase::conversion_thread_proc(QObject* segment, uint64_t start_sample, | |
255 | uint64_t end_sample) | |
256 | { | |
257 | const uint64_t block_size = 4096; | |
258 | ||
259 | // TODO Support for multiple segments is missing | |
260 | ||
261 | if ((channel_type_ == AnalogChannel) && | |
262 | ((conversion_type_ == A2LConversionByTreshold) || | |
263 | (conversion_type_ == A2LConversionBySchmittTrigger))) { | |
264 | ||
265 | AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment); | |
266 | ||
267 | // Create the logic data container if needed | |
268 | shared_ptr<Logic> logic_data; | |
269 | if (!converted_data_) { | |
270 | logic_data = make_shared<Logic>(1); // Contains only one channel | |
271 | converted_data_ = logic_data; | |
272 | } else | |
273 | logic_data = dynamic_pointer_cast<Logic>(converted_data_); | |
274 | ||
275 | // Create the initial logic data segment if needed | |
276 | if (logic_data->segments().size() == 0) { | |
277 | shared_ptr<LogicSegment> lsegment = | |
278 | make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate()); | |
279 | logic_data->push_segment(lsegment); | |
280 | } | |
281 | ||
282 | LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get()); | |
283 | ||
284 | // start_sample=end_sample=0 means we need to figure out the unprocessed range | |
285 | if ((start_sample == 0) && (end_sample == 0)) { | |
286 | start_sample = lsegment->get_sample_count(); | |
287 | end_sample = asegment->get_sample_count(); | |
288 | } | |
289 | ||
290 | if (start_sample == end_sample) | |
291 | return; // Nothing to do | |
292 | ||
293 | float min_v, max_v; | |
294 | tie(min_v, max_v) = asegment->get_min_max(); | |
295 | ||
296 | vector<uint8_t> lsamples; | |
297 | lsamples.reserve(block_size); | |
298 | ||
299 | uint64_t i = start_sample; | |
300 | ||
301 | if (conversion_type_ == A2LConversionByTreshold) { | |
302 | const float threshold = (min_v + max_v) * 0.5; // middle between min and max | |
303 | ||
304 | // Convert as many sample blocks as we can | |
305 | while ((end_sample - i) > block_size) { | |
306 | const float* asamples = asegment->get_samples(i, i + block_size); | |
307 | for (uint32_t j = 0; j < block_size; j++) | |
308 | lsamples.push_back(convert_a2l_threshold(threshold, asamples[j])); | |
309 | lsegment->append_payload(lsamples.data(), lsamples.size()); | |
310 | i += block_size; | |
311 | lsamples.clear(); | |
312 | delete[] asamples; | |
313 | } | |
314 | ||
315 | // Convert remaining samples | |
316 | const float* asamples = asegment->get_samples(i, end_sample); | |
317 | for (uint32_t j = 0; j < (end_sample - i); j++) | |
318 | lsamples.push_back(convert_a2l_threshold(threshold, asamples[j])); | |
319 | lsegment->append_payload(lsamples.data(), lsamples.size()); | |
320 | delete[] asamples; | |
321 | } | |
322 | ||
323 | if (conversion_type_ == A2LConversionBySchmittTrigger) { | |
324 | const float amplitude = max_v - min_v; | |
325 | const float lo_thr = min_v + (amplitude * 0.1); // 10% above min | |
326 | const float hi_thr = max_v - (amplitude * 0.1); // 10% below max | |
5d9fe823 | 327 | uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0 |
12ea3616 SA |
328 | |
329 | // Convert as many sample blocks as we can | |
330 | while ((end_sample - i) > block_size) { | |
331 | const float* asamples = asegment->get_samples(i, i + block_size); | |
332 | for (uint32_t j = 0; j < block_size; j++) | |
5d9fe823 | 333 | lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state)); |
12ea3616 SA |
334 | lsegment->append_payload(lsamples.data(), lsamples.size()); |
335 | i += block_size; | |
336 | lsamples.clear(); | |
337 | delete[] asamples; | |
338 | } | |
339 | ||
340 | // Convert remaining samples | |
341 | const float* asamples = asegment->get_samples(i, end_sample); | |
342 | for (uint32_t j = 0; j < (end_sample - i); j++) | |
5d9fe823 | 343 | lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state)); |
12ea3616 SA |
344 | lsegment->append_payload(lsamples.data(), lsamples.size()); |
345 | delete[] asamples; | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | void SignalBase::on_samples_cleared() | |
351 | { | |
352 | if (converted_data_) | |
353 | converted_data_->clear(); | |
354 | } | |
355 | ||
356 | void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample, | |
357 | uint64_t end_sample) | |
358 | { | |
359 | (void)segment; | |
360 | (void)start_sample; | |
361 | (void)end_sample; | |
362 | ||
363 | if (conversion_type_ != NoConversion) { | |
364 | ||
365 | // Wait for the currently ongoing conversion to finish | |
366 | if (conversion_thread_.joinable()) | |
367 | conversion_thread_.join(); | |
368 | ||
369 | conversion_thread_ = std::thread( | |
370 | &SignalBase::conversion_thread_proc, this, | |
371 | segment, start_sample, end_sample); | |
372 | } | |
373 | } | |
374 | ||
375 | void SignalBase::on_capture_state_changed(int state) | |
376 | { | |
377 | return; | |
378 | if (state == Session::Stopped) { | |
379 | // Make sure that all data is converted | |
380 | ||
381 | if ((channel_type_ == AnalogChannel) && | |
382 | ((conversion_type_ == A2LConversionByTreshold) || | |
383 | (conversion_type_ == A2LConversionBySchmittTrigger))) { | |
384 | ||
385 | shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_); | |
386 | ||
387 | if (analog_data->analog_segments().size() > 0) { | |
388 | // TODO Support for multiple segments is missing | |
389 | AnalogSegment *asegment = analog_data->analog_segments().front().get(); | |
390 | ||
391 | if (conversion_thread_.joinable()) | |
392 | conversion_thread_.join(); | |
393 | ||
394 | conversion_thread_ = std::thread( | |
395 | &SignalBase::conversion_thread_proc, this, asegment, 0, 0); | |
396 | } | |
397 | } | |
398 | } | |
6de38b17 SA |
399 | } |
400 | ||
bf0edd2b SA |
401 | } // namespace data |
402 | } // namespace pv |