]>
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; |
27a3f09b | 36 | using std::unique_lock; |
bf0edd2b SA |
37 | |
38 | namespace pv { | |
39 | namespace data { | |
40 | ||
c063290a | 41 | const int SignalBase::ColourBGAlpha = 8 * 256 / 100; |
bcaf0334 | 42 | const uint64_t SignalBase::ConversionBlockSize = 4096; |
bf0edd2b | 43 | |
472a80c5 SA |
44 | SignalBase::SignalBase(shared_ptr<sigrok::Channel> channel, ChannelType channel_type) : |
45 | channel_(channel), | |
12ea3616 SA |
46 | channel_type_(channel_type), |
47 | conversion_type_(NoConversion) | |
bf0edd2b | 48 | { |
050b5a6c SA |
49 | if (channel_) |
50 | internal_name_ = QString::fromStdString(channel_->name()); | |
bf0edd2b SA |
51 | } |
52 | ||
12ea3616 SA |
53 | SignalBase::~SignalBase() |
54 | { | |
27a3f09b | 55 | stop_conversion(); |
12ea3616 SA |
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 | { | |
27a3f09b SA |
103 | return (channel_) ? channel_->index() : 0; |
104 | } | |
105 | ||
106 | unsigned int SignalBase::logic_bit_index() const | |
107 | { | |
108 | if (channel_type_ == LogicChannel) | |
109 | return channel_->index(); | |
110 | else | |
111 | return 0; | |
bf0edd2b SA |
112 | } |
113 | ||
114 | QColor SignalBase::colour() const | |
115 | { | |
116 | return colour_; | |
117 | } | |
118 | ||
119 | void SignalBase::set_colour(QColor colour) | |
120 | { | |
121 | colour_ = colour; | |
122 | ||
123 | bgcolour_ = colour; | |
124 | bgcolour_.setAlpha(ColourBGAlpha); | |
125 | ||
126 | colour_changed(colour); | |
127 | } | |
128 | ||
129 | QColor SignalBase::bgcolour() const | |
130 | { | |
131 | return bgcolour_; | |
132 | } | |
133 | ||
cbd2a2de SA |
134 | void SignalBase::set_data(shared_ptr<pv::data::SignalData> data) |
135 | { | |
8ce8ebb9 SA |
136 | if (data_) { |
137 | disconnect(data.get(), SIGNAL(samples_cleared()), | |
12ea3616 | 138 | this, SLOT(on_samples_cleared())); |
8ce8ebb9 | 139 | disconnect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)), |
12ea3616 SA |
140 | this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t))); |
141 | } | |
142 | ||
cbd2a2de | 143 | data_ = data; |
12ea3616 | 144 | |
8ce8ebb9 SA |
145 | if (data_) { |
146 | connect(data.get(), SIGNAL(samples_cleared()), | |
12ea3616 | 147 | this, SLOT(on_samples_cleared())); |
8ce8ebb9 | 148 | connect(data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)), |
12ea3616 SA |
149 | this, SLOT(on_samples_added(QObject*, uint64_t, uint64_t))); |
150 | } | |
cbd2a2de SA |
151 | } |
152 | ||
153 | shared_ptr<data::Analog> SignalBase::analog_data() const | |
154 | { | |
12ea3616 SA |
155 | shared_ptr<Analog> result = nullptr; |
156 | ||
472a80c5 | 157 | if (channel_type_ == AnalogChannel) |
12ea3616 SA |
158 | result = dynamic_pointer_cast<Analog>(data_); |
159 | ||
160 | return result; | |
cbd2a2de SA |
161 | } |
162 | ||
163 | shared_ptr<data::Logic> SignalBase::logic_data() const | |
164 | { | |
12ea3616 SA |
165 | shared_ptr<Logic> result = nullptr; |
166 | ||
472a80c5 | 167 | if (channel_type_ == LogicChannel) |
12ea3616 SA |
168 | result = dynamic_pointer_cast<Logic>(data_); |
169 | ||
170 | if (((conversion_type_ == A2LConversionByTreshold) || | |
171 | (conversion_type_ == A2LConversionBySchmittTrigger))) | |
172 | result = dynamic_pointer_cast<Logic>(converted_data_); | |
173 | ||
174 | return result; | |
175 | } | |
176 | ||
177 | void SignalBase::set_conversion_type(ConversionType t) | |
178 | { | |
179 | if (conversion_type_ != NoConversion) { | |
27a3f09b | 180 | stop_conversion(); |
12ea3616 SA |
181 | |
182 | // Discard converted data | |
183 | converted_data_.reset(); | |
184 | } | |
185 | ||
186 | conversion_type_ = t; | |
187 | ||
27a3f09b | 188 | start_conversion(); |
12ea3616 SA |
189 | |
190 | conversion_type_changed(t); | |
cbd2a2de SA |
191 | } |
192 | ||
bb7dd726 SA |
193 | #ifdef ENABLE_DECODE |
194 | bool SignalBase::is_decode_signal() const | |
195 | { | |
47747218 | 196 | return (channel_type_ == DecodeChannel); |
bb7dd726 SA |
197 | } |
198 | #endif | |
cbd2a2de | 199 | |
6de38b17 SA |
200 | void SignalBase::save_settings(QSettings &settings) const |
201 | { | |
202 | settings.setValue("name", name()); | |
203 | settings.setValue("enabled", enabled()); | |
204 | settings.setValue("colour", colour()); | |
12ea3616 | 205 | settings.setValue("conversion_type", (int)conversion_type_); |
6de38b17 SA |
206 | } |
207 | ||
208 | void SignalBase::restore_settings(QSettings &settings) | |
209 | { | |
210 | set_name(settings.value("name").toString()); | |
211 | set_enabled(settings.value("enabled").toBool()); | |
212 | set_colour(settings.value("colour").value<QColor>()); | |
12ea3616 SA |
213 | set_conversion_type((ConversionType)settings.value("conversion_type").toInt()); |
214 | } | |
215 | ||
216 | uint8_t SignalBase::convert_a2l_threshold(float threshold, float value) | |
217 | { | |
218 | return (value >= threshold) ? 1 : 0; | |
219 | } | |
220 | ||
5d9fe823 SA |
221 | uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr, |
222 | float value, uint8_t &state) | |
12ea3616 | 223 | { |
12ea3616 SA |
224 | if (value < lo_thr) |
225 | state = 0; | |
226 | else if (value > hi_thr) | |
227 | state = 1; | |
228 | ||
229 | return state; | |
230 | } | |
231 | ||
27a3f09b | 232 | void SignalBase::conversion_thread_proc(QObject* segment) |
12ea3616 | 233 | { |
12ea3616 SA |
234 | // TODO Support for multiple segments is missing |
235 | ||
27a3f09b SA |
236 | uint64_t start_sample, end_sample; |
237 | start_sample = end_sample = 0; | |
12ea3616 | 238 | |
27a3f09b SA |
239 | do { |
240 | if ((channel_type_ == AnalogChannel) && | |
241 | ((conversion_type_ == A2LConversionByTreshold) || | |
242 | (conversion_type_ == A2LConversionBySchmittTrigger))) { | |
243 | ||
244 | AnalogSegment *asegment = qobject_cast<AnalogSegment*>(segment); | |
245 | ||
246 | // Create the logic data container if needed | |
247 | shared_ptr<Logic> logic_data; | |
248 | if (!converted_data_) { | |
249 | logic_data = make_shared<Logic>(1); // Contains only one channel | |
250 | converted_data_ = logic_data; | |
251 | } else | |
252 | logic_data = dynamic_pointer_cast<Logic>(converted_data_); | |
253 | ||
254 | // Create the initial logic data segment if needed | |
255 | if (logic_data->segments().size() == 0) { | |
256 | shared_ptr<LogicSegment> lsegment = | |
257 | make_shared<LogicSegment>(*logic_data.get(), 1, asegment->samplerate()); | |
258 | logic_data->push_segment(lsegment); | |
259 | } | |
12ea3616 | 260 | |
27a3f09b | 261 | LogicSegment *lsegment = dynamic_cast<LogicSegment*>(logic_data->segments().front().get()); |
12ea3616 | 262 | |
12ea3616 SA |
263 | start_sample = lsegment->get_sample_count(); |
264 | end_sample = asegment->get_sample_count(); | |
12ea3616 | 265 | |
27a3f09b SA |
266 | if (end_sample > start_sample) { |
267 | float min_v, max_v; | |
268 | tie(min_v, max_v) = asegment->get_min_max(); | |
269 | ||
270 | vector<uint8_t> lsamples; | |
271 | lsamples.reserve(ConversionBlockSize); | |
272 | ||
273 | uint64_t i = start_sample; | |
274 | ||
275 | if (conversion_type_ == A2LConversionByTreshold) { | |
276 | const float threshold = (min_v + max_v) * 0.5; // middle between min and max | |
277 | ||
278 | // Convert as many sample blocks as we can | |
279 | while ((end_sample - i) > ConversionBlockSize) { | |
280 | const float* asamples = asegment->get_samples(i, i + ConversionBlockSize); | |
281 | for (uint32_t j = 0; j < ConversionBlockSize; j++) | |
282 | lsamples.push_back(convert_a2l_threshold(threshold, asamples[j])); | |
283 | lsegment->append_payload(lsamples.data(), lsamples.size()); | |
284 | samples_added(lsegment, i, i + ConversionBlockSize); | |
285 | i += ConversionBlockSize; | |
286 | lsamples.clear(); | |
287 | delete[] asamples; | |
288 | } | |
289 | ||
290 | // Convert remaining samples | |
291 | const float* asamples = asegment->get_samples(i, end_sample); | |
292 | for (uint32_t j = 0; j < (end_sample - i); j++) | |
293 | lsamples.push_back(convert_a2l_threshold(threshold, asamples[j])); | |
294 | lsegment->append_payload(lsamples.data(), lsamples.size()); | |
295 | samples_added(lsegment, i, end_sample); | |
296 | delete[] asamples; | |
297 | } | |
298 | ||
299 | if (conversion_type_ == A2LConversionBySchmittTrigger) { | |
300 | const float amplitude = max_v - min_v; | |
301 | const float lo_thr = min_v + (amplitude * 0.1); // 10% above min | |
302 | const float hi_thr = max_v - (amplitude * 0.1); // 10% below max | |
303 | uint8_t state = 0; // TODO Use value of logic sample n-1 instead of 0 | |
304 | ||
305 | // Convert as many sample blocks as we can | |
306 | while ((end_sample - i) > ConversionBlockSize) { | |
307 | const float* asamples = asegment->get_samples(i, i + ConversionBlockSize); | |
308 | for (uint32_t j = 0; j < ConversionBlockSize; j++) | |
309 | lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state)); | |
310 | lsegment->append_payload(lsamples.data(), lsamples.size()); | |
311 | samples_added(lsegment, i, i + ConversionBlockSize); | |
312 | i += ConversionBlockSize; | |
313 | lsamples.clear(); | |
314 | delete[] asamples; | |
315 | } | |
316 | ||
317 | // Convert remaining samples | |
318 | const float* asamples = asegment->get_samples(i, end_sample); | |
319 | for (uint32_t j = 0; j < (end_sample - i); j++) | |
320 | lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state)); | |
321 | lsegment->append_payload(lsamples.data(), lsamples.size()); | |
322 | samples_added(lsegment, i, end_sample); | |
323 | delete[] asamples; | |
324 | } | |
325 | ||
326 | // If acquisition is ongoing, start-/endsample may have changed | |
327 | end_sample = asegment->get_sample_count(); | |
12ea3616 | 328 | } |
27a3f09b | 329 | } |
12ea3616 | 330 | |
27a3f09b SA |
331 | if (!conversion_interrupt_ && (start_sample == end_sample)) { |
332 | unique_lock<mutex> input_lock(conversion_input_mutex_); | |
333 | conversion_input_cond_.wait(input_lock); | |
12ea3616 | 334 | } |
27a3f09b SA |
335 | } while (!conversion_interrupt_); |
336 | } | |
12ea3616 | 337 | |
27a3f09b SA |
338 | void SignalBase::start_conversion() |
339 | { | |
340 | stop_conversion(); | |
341 | ||
342 | if ((channel_type_ == AnalogChannel) && | |
343 | ((conversion_type_ == A2LConversionByTreshold) || | |
344 | (conversion_type_ == A2LConversionBySchmittTrigger))) { | |
345 | ||
346 | shared_ptr<Analog> analog_data = dynamic_pointer_cast<Analog>(data_); | |
12ea3616 | 347 | |
27a3f09b SA |
348 | if (analog_data->analog_segments().size() > 0) { |
349 | // TODO Support for multiple segments is missing | |
350 | AnalogSegment *asegment = analog_data->analog_segments().front().get(); | |
eae3bbbb | 351 | |
27a3f09b SA |
352 | conversion_interrupt_ = false; |
353 | conversion_thread_ = std::thread( | |
354 | &SignalBase::conversion_thread_proc, this, asegment); | |
12ea3616 SA |
355 | } |
356 | } | |
357 | } | |
358 | ||
27a3f09b SA |
359 | void SignalBase::stop_conversion() |
360 | { | |
361 | // Stop conversion so we can restart it from the beginning | |
362 | conversion_interrupt_ = true; | |
363 | conversion_input_cond_.notify_one(); | |
364 | if (conversion_thread_.joinable()) | |
365 | conversion_thread_.join(); | |
366 | } | |
367 | ||
12ea3616 SA |
368 | void SignalBase::on_samples_cleared() |
369 | { | |
370 | if (converted_data_) | |
371 | converted_data_->clear(); | |
eae3bbbb SA |
372 | |
373 | samples_cleared(); | |
12ea3616 SA |
374 | } |
375 | ||
376 | void SignalBase::on_samples_added(QObject* segment, uint64_t start_sample, | |
377 | uint64_t end_sample) | |
378 | { | |
12ea3616 | 379 | if (conversion_type_ != NoConversion) { |
27a3f09b SA |
380 | if (conversion_thread_.joinable()) { |
381 | // Notify the conversion thread since it's running | |
382 | conversion_input_cond_.notify_one(); | |
383 | } else { | |
384 | // Start the conversion thread | |
385 | start_conversion(); | |
386 | } | |
12ea3616 | 387 | } |
eae3bbbb SA |
388 | |
389 | samples_added(segment, start_sample, end_sample); | |
12ea3616 SA |
390 | } |
391 | ||
392 | void SignalBase::on_capture_state_changed(int state) | |
393 | { | |
27a3f09b SA |
394 | if (state == Session::Running) { |
395 | if (conversion_type_ != NoConversion) { | |
396 | // Restart conversion | |
397 | stop_conversion(); | |
398 | start_conversion(); | |
12ea3616 SA |
399 | } |
400 | } | |
6de38b17 SA |
401 | } |
402 | ||
bf0edd2b SA |
403 | } // namespace data |
404 | } // namespace pv |