]>
Commit | Line | Data |
---|---|---|
b0773a8a SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <limits> | |
21 | ||
22 | #include <QDebug> | |
23 | ||
24 | #include "mathsignal.hpp" | |
25 | ||
516b0c41 | 26 | #include <extdef.h> |
b0773a8a SA |
27 | #include <pv/globalsettings.hpp> |
28 | #include <pv/session.hpp> | |
29 | #include <pv/data/analogsegment.hpp> | |
30 | #include <pv/data/signalbase.hpp> | |
31 | ||
4640a84e | 32 | using std::dynamic_pointer_cast; |
b0773a8a | 33 | using std::make_shared; |
4640a84e SA |
34 | using std::min; |
35 | using std::unique_lock; | |
b0773a8a SA |
36 | |
37 | namespace pv { | |
38 | namespace data { | |
39 | ||
40 | const int64_t MathSignal::ChunkLength = 256 * 1024; | |
41 | ||
42 | ||
3f1f6295 SA |
43 | template<typename T> |
44 | struct sig_sample : public exprtk::igeneric_function<T> | |
45 | { | |
46 | typedef typename exprtk::igeneric_function<T>::parameter_list_t parameter_list_t; | |
47 | typedef typename exprtk::igeneric_function<T>::generic_type generic_type; | |
48 | typedef typename generic_type::scalar_view scalar_t; | |
49 | typedef typename generic_type::string_view string_t; | |
50 | ||
51 | sig_sample(MathSignal& owner) : | |
52 | exprtk::igeneric_function<T>("ST"), // Require channel name and sample number | |
53 | owner_(owner), | |
54 | sig_data(nullptr) | |
55 | { | |
56 | } | |
57 | ||
58 | T operator()(parameter_list_t parameters) | |
59 | { | |
60 | const string_t exprtk_sig_name = string_t(parameters[0]); | |
61 | const scalar_t exprtk_sample_num = scalar_t(parameters[1]); | |
62 | ||
63 | const std::string str_sig_name = to_str(exprtk_sig_name); | |
64 | const double sample_num = exprtk_sample_num(); | |
65 | ||
66 | if (!sig_data) | |
67 | sig_data = owner_.signal_from_name(str_sig_name); | |
68 | ||
69 | assert(sig_data); | |
70 | owner_.update_signal_sample(sig_data, current_segment, sample_num); | |
71 | ||
72 | return T(sig_data->sample_value); | |
73 | } | |
74 | ||
75 | MathSignal& owner_; | |
76 | uint32_t current_segment; | |
77 | signal_data* sig_data; | |
78 | }; | |
79 | ||
80 | ||
b0773a8a SA |
81 | MathSignal::MathSignal(pv::Session &session) : |
82 | SignalBase(nullptr, SignalBase::MathChannel), | |
4640a84e SA |
83 | session_(session), |
84 | use_custom_sample_rate_(false), | |
85 | use_custom_sample_count_(false), | |
86 | expression_(""), | |
87 | error_message_(""), | |
3f1f6295 | 88 | exprtk_unknown_symbol_table_(nullptr), |
4640a84e SA |
89 | exprtk_symbol_table_(nullptr), |
90 | exprtk_expression_(nullptr), | |
3f1f6295 SA |
91 | exprtk_parser_(nullptr), |
92 | fnc_sig_sample_(nullptr) | |
b0773a8a | 93 | { |
516b0c41 SA |
94 | uint32_t sig_idx = session_.get_next_signal_index(MathChannel); |
95 | set_name(QString(tr("Math%1")).arg(sig_idx)); | |
96 | set_color(AnalogSignalColors[(sig_idx - 1) % countof(AnalogSignalColors)]); | |
4640a84e | 97 | |
5eb9d1c4 | 98 | set_data(std::make_shared<data::Analog>()); |
b0773a8a | 99 | |
4640a84e SA |
100 | connect(&session_, SIGNAL(capture_state_changed(int)), |
101 | this, SLOT(on_capture_state_changed(int))); | |
b0773a8a SA |
102 | } |
103 | ||
104 | MathSignal::~MathSignal() | |
105 | { | |
4640a84e | 106 | reset_generation(); |
3f1f6295 SA |
107 | |
108 | if (fnc_sig_sample_) | |
109 | delete fnc_sig_sample_; | |
b0773a8a SA |
110 | } |
111 | ||
112 | void MathSignal::save_settings(QSettings &settings) const | |
113 | { | |
4640a84e SA |
114 | settings.setValue("expression", expression_); |
115 | ||
116 | settings.setValue("custom_sample_rate", (qulonglong)custom_sample_rate_); | |
117 | settings.setValue("custom_sample_count", (qulonglong)custom_sample_count_); | |
118 | settings.setValue("use_custom_sample_rate", use_custom_sample_rate_); | |
119 | settings.setValue("use_custom_sample_count", use_custom_sample_count_); | |
b0773a8a SA |
120 | } |
121 | ||
122 | void MathSignal::restore_settings(QSettings &settings) | |
123 | { | |
4640a84e SA |
124 | if (settings.contains("expression")) |
125 | expression_ = settings.value("expression").toString(); | |
126 | ||
127 | if (settings.contains("custom_sample_rate")) | |
128 | custom_sample_rate_ = settings.value("custom_sample_rate").toULongLong(); | |
129 | ||
130 | if (settings.contains("custom_sample_count")) | |
131 | custom_sample_count_ = settings.value("custom_sample_count").toULongLong(); | |
132 | ||
133 | if (settings.contains("use_custom_sample_rate")) | |
134 | use_custom_sample_rate_ = settings.value("use_custom_sample_rate").toBool(); | |
135 | ||
136 | if (settings.contains("use_custom_sample_count")) | |
137 | use_custom_sample_count_ = settings.value("use_custom_sample_count").toBool(); | |
138 | } | |
139 | ||
140 | QString MathSignal::error_message() const | |
141 | { | |
142 | return error_message_; | |
143 | } | |
144 | ||
145 | QString MathSignal::get_expression() const | |
146 | { | |
147 | return expression_; | |
148 | } | |
149 | ||
150 | void MathSignal::set_expression(QString expression) | |
151 | { | |
152 | expression_ = expression; | |
153 | ||
154 | begin_generation(); | |
155 | } | |
156 | ||
157 | void MathSignal::set_error_message(QString msg) | |
158 | { | |
159 | error_message_ = msg; | |
160 | // TODO Emulate noquote() | |
d203009f | 161 | qDebug().nospace() << name() << ": " << msg << "(Expression: '" << expression_ << "')"; |
4640a84e SA |
162 | } |
163 | ||
164 | uint64_t MathSignal::get_working_sample_count(uint32_t segment_id) const | |
165 | { | |
166 | // The working sample count is the highest sample number for | |
167 | // which all used signals have data available, so go through all | |
168 | // channels and use the lowest overall sample count of the segment | |
169 | ||
170 | int64_t result = std::numeric_limits<int64_t>::max(); | |
171 | ||
172 | if (use_custom_sample_count_) | |
173 | // A custom sample count implies that only one segment will be created | |
174 | result = (segment_id == 0) ? custom_sample_count_ : 0; | |
175 | else { | |
176 | if (input_signals_.size() > 0) { | |
3f1f6295 SA |
177 | for (auto input_signal : input_signals_) { |
178 | const shared_ptr<SignalBase>& sb = input_signal.second.sb; | |
179 | ||
4640a84e | 180 | shared_ptr<Analog> a = sb->analog_data(); |
bee54d9e SA |
181 | auto analog_segments = a->analog_segments(); |
182 | ||
183 | if (analog_segments.size() == 0) { | |
184 | result = 0; | |
185 | continue; | |
186 | } | |
187 | ||
188 | const uint32_t highest_segment_id = (analog_segments.size() - 1); | |
189 | if (segment_id > highest_segment_id) | |
4640a84e | 190 | continue; |
bee54d9e SA |
191 | |
192 | const shared_ptr<AnalogSegment> segment = analog_segments.at(segment_id); | |
4640a84e SA |
193 | result = min(result, (int64_t)segment->get_sample_count()); |
194 | } | |
195 | } else | |
196 | result = session_.get_segment_sample_count(segment_id); | |
197 | } | |
198 | ||
199 | return result; | |
200 | } | |
201 | ||
144e72c9 | 202 | void MathSignal::update_completeness(uint32_t segment_id, uint64_t output_sample_count) |
bee54d9e SA |
203 | { |
204 | bool output_complete = true; | |
205 | ||
206 | if (input_signals_.size() > 0) { | |
207 | for (auto input_signal : input_signals_) { | |
208 | const shared_ptr<SignalBase>& sb = input_signal.second.sb; | |
209 | ||
210 | shared_ptr<Analog> a = sb->analog_data(); | |
211 | auto analog_segments = a->analog_segments(); | |
212 | ||
213 | if (analog_segments.size() == 0) { | |
214 | output_complete = false; | |
215 | continue; | |
216 | } | |
217 | ||
218 | const uint32_t highest_segment_id = (analog_segments.size() - 1); | |
219 | if (segment_id > highest_segment_id) { | |
220 | output_complete = false; | |
221 | continue; | |
222 | } | |
223 | ||
224 | const shared_ptr<AnalogSegment> segment = analog_segments.at(segment_id); | |
144e72c9 SA |
225 | if (!segment->is_complete()) { |
226 | output_complete = false; | |
227 | continue; | |
228 | } | |
229 | ||
230 | if (output_sample_count < segment->get_sample_count()) | |
bee54d9e SA |
231 | output_complete = false; |
232 | } | |
144e72c9 SA |
233 | } else { |
234 | // We're done when we generated as many samples as the stopped session is long | |
235 | if ((session_.get_capture_state() != Session::Stopped) || | |
236 | (output_sample_count < session_.get_segment_sample_count(segment_id))) | |
237 | output_complete = false; | |
bee54d9e SA |
238 | } |
239 | ||
240 | if (output_complete) | |
241 | analog_data()->analog_segments().at(segment_id)->set_complete(); | |
242 | } | |
243 | ||
4640a84e SA |
244 | void MathSignal::reset_generation() |
245 | { | |
246 | if (gen_thread_.joinable()) { | |
247 | gen_interrupt_ = true; | |
248 | gen_input_cond_.notify_one(); | |
249 | gen_thread_.join(); | |
250 | } | |
251 | ||
252 | data_->clear(); | |
3f1f6295 | 253 | input_signals_.clear(); |
4640a84e | 254 | |
3f1f6295 SA |
255 | if (exprtk_parser_) { |
256 | delete exprtk_parser_; | |
257 | exprtk_parser_ = nullptr; | |
4640a84e SA |
258 | } |
259 | ||
260 | if (exprtk_expression_) { | |
261 | delete exprtk_expression_; | |
262 | exprtk_expression_ = nullptr; | |
263 | } | |
264 | ||
3f1f6295 SA |
265 | if (exprtk_symbol_table_) { |
266 | delete exprtk_symbol_table_; | |
267 | exprtk_symbol_table_ = nullptr; | |
268 | } | |
269 | ||
270 | if (exprtk_unknown_symbol_table_) { | |
271 | delete exprtk_unknown_symbol_table_; | |
272 | exprtk_unknown_symbol_table_ = nullptr; | |
273 | } | |
274 | ||
275 | if (fnc_sig_sample_) { | |
276 | delete fnc_sig_sample_; | |
277 | fnc_sig_sample_ = nullptr; | |
4640a84e SA |
278 | } |
279 | ||
280 | if (!error_message_.isEmpty()) { | |
281 | error_message_ = QString(); | |
282 | // TODO Emulate noquote() | |
283 | qDebug().nospace() << name() << ": Error cleared"; | |
284 | } | |
285 | } | |
286 | ||
287 | void MathSignal::begin_generation() | |
288 | { | |
289 | reset_generation(); | |
290 | ||
291 | if (expression_.isEmpty()) { | |
292 | set_error_message(tr("No expression defined, nothing to do")); | |
293 | return; | |
294 | } | |
295 | ||
144e72c9 SA |
296 | disconnect(this, SLOT(on_data_received())); |
297 | ||
3f1f6295 SA |
298 | fnc_sig_sample_ = new sig_sample<double>(*this); |
299 | ||
300 | exprtk_unknown_symbol_table_ = new exprtk::symbol_table<double>(); | |
301 | ||
4640a84e | 302 | exprtk_symbol_table_ = new exprtk::symbol_table<double>(); |
3f1f6295 | 303 | exprtk_symbol_table_->add_function("sig_sample", *fnc_sig_sample_); |
4640a84e SA |
304 | exprtk_symbol_table_->add_variable("t", exprtk_current_time_); |
305 | exprtk_symbol_table_->add_variable("s", exprtk_current_sample_); | |
306 | exprtk_symbol_table_->add_constants(); | |
307 | ||
308 | exprtk_expression_ = new exprtk::expression<double>(); | |
3f1f6295 | 309 | exprtk_expression_->register_symbol_table(*exprtk_unknown_symbol_table_); |
4640a84e SA |
310 | exprtk_expression_->register_symbol_table(*exprtk_symbol_table_); |
311 | ||
312 | exprtk_parser_ = new exprtk::parser<double>(); | |
3f1f6295 SA |
313 | exprtk_parser_->enable_unknown_symbol_resolver(); |
314 | ||
d203009f | 315 | if (!exprtk_parser_->compile(expression_.toStdString(), *exprtk_expression_)) { |
3f1f6295 | 316 | set_error_message(tr("Error in expression")); |
d203009f | 317 | } else { |
3f1f6295 SA |
318 | // Resolve unknown scalars to signals and add them to the input signal list |
319 | vector<string> unknowns; | |
320 | exprtk_unknown_symbol_table_->get_variable_list(unknowns); | |
321 | for (string& unknown : unknowns) { | |
322 | signal_data* sig_data = signal_from_name(unknown); | |
323 | const shared_ptr<SignalBase> signal = (sig_data) ? (sig_data->sb) : nullptr; | |
324 | if (!signal || (!signal->analog_data())) { | |
bee54d9e | 325 | set_error_message(QString(tr("%1 isn't a valid analog signal")).arg( |
3f1f6295 SA |
326 | QString::fromStdString(unknown))); |
327 | } else | |
328 | sig_data->ref = &(exprtk_unknown_symbol_table_->variable_ref(unknown)); | |
329 | } | |
330 | } | |
331 | ||
332 | if (error_message_.isEmpty()) { | |
144e72c9 SA |
333 | // Connect to the session data notification if we have no input signals |
334 | if (input_signals_.empty()) | |
335 | connect(&session_, SIGNAL(data_received()), | |
336 | this, SLOT(on_data_received())); | |
337 | ||
d203009f SA |
338 | gen_interrupt_ = false; |
339 | gen_thread_ = std::thread(&MathSignal::generation_proc, this); | |
340 | } | |
4640a84e SA |
341 | } |
342 | ||
343 | void MathSignal::generate_samples(uint32_t segment_id, const uint64_t start_sample, | |
344 | const int64_t sample_count) | |
345 | { | |
346 | shared_ptr<Analog> analog = dynamic_pointer_cast<Analog>(data_); | |
347 | shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
348 | ||
3f1f6295 SA |
349 | // Keep the math functions segment IDs in sync |
350 | fnc_sig_sample_->current_segment = segment_id; | |
351 | ||
4640a84e SA |
352 | const double sample_rate = data_->get_samplerate(); |
353 | ||
354 | exprtk_current_sample_ = start_sample; | |
355 | ||
356 | float *sample_data = new float[sample_count]; | |
357 | ||
358 | for (int64_t i = 0; i < sample_count; i++) { | |
4640a84e | 359 | exprtk_current_time_ = exprtk_current_sample_ / sample_rate; |
3f1f6295 SA |
360 | |
361 | for (auto& entry : input_signals_) { | |
362 | signal_data* sig_data = &(entry.second); | |
363 | update_signal_sample(sig_data, segment_id, exprtk_current_sample_); | |
364 | } | |
365 | ||
4640a84e SA |
366 | double value = exprtk_expression_->value(); |
367 | sample_data[i] = value; | |
3f1f6295 | 368 | exprtk_current_sample_ += 1; |
4640a84e SA |
369 | } |
370 | ||
371 | segment->append_interleaved_samples(sample_data, sample_count, 1); | |
372 | ||
373 | delete[] sample_data; | |
374 | } | |
375 | ||
376 | void MathSignal::generation_proc() | |
377 | { | |
4640a84e SA |
378 | // Don't do anything until we have a valid sample rate |
379 | do { | |
380 | if (use_custom_sample_rate_) | |
381 | data_->set_samplerate(custom_sample_rate_); | |
382 | else | |
383 | data_->set_samplerate(session_.get_samplerate()); | |
384 | ||
385 | if (data_->get_samplerate() == 1) { | |
386 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
387 | gen_input_cond_.wait(gen_input_lock); | |
388 | } | |
389 | } while ((!gen_interrupt_) && (data_->get_samplerate() == 1)); | |
390 | ||
391 | if (gen_interrupt_) | |
392 | return; | |
393 | ||
144e72c9 | 394 | uint32_t segment_id = 0; |
5eb9d1c4 | 395 | shared_ptr<Analog> analog = analog_data(); |
4640a84e SA |
396 | |
397 | // Create initial analog segment | |
398 | shared_ptr<AnalogSegment> output_segment = | |
399 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
400 | analog->push_segment(output_segment); | |
401 | ||
402 | // Create analog samples | |
403 | do { | |
404 | const uint64_t input_sample_count = get_working_sample_count(segment_id); | |
405 | const uint64_t output_sample_count = output_segment->get_sample_count(); | |
406 | ||
407 | const uint64_t samples_to_process = | |
408 | (input_sample_count > output_sample_count) ? | |
409 | (input_sample_count - output_sample_count) : 0; | |
410 | ||
411 | // Process the samples if necessary... | |
412 | if (samples_to_process > 0) { | |
413 | const uint64_t chunk_sample_count = ChunkLength; | |
414 | ||
415 | uint64_t processed_samples = 0; | |
416 | do { | |
417 | const uint64_t start_sample = output_sample_count + processed_samples; | |
418 | const uint64_t sample_count = | |
419 | min(samples_to_process - processed_samples, chunk_sample_count); | |
420 | ||
421 | generate_samples(segment_id, start_sample, sample_count); | |
422 | processed_samples += sample_count; | |
423 | ||
424 | // Notify consumers of this signal's data | |
4640a84e SA |
425 | samples_added(segment_id, start_sample, start_sample + processed_samples); |
426 | } while (!gen_interrupt_ && (processed_samples < samples_to_process)); | |
427 | } | |
428 | ||
144e72c9 | 429 | update_completeness(segment_id, output_sample_count); |
5eb9d1c4 | 430 | |
144e72c9 | 431 | if (output_segment->is_complete() && (segment_id < session_.get_highest_segment_id())) { |
4640a84e SA |
432 | // Process next segment |
433 | segment_id++; | |
434 | ||
435 | output_segment = | |
436 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
437 | analog->push_segment(output_segment); | |
4640a84e SA |
438 | } |
439 | ||
144e72c9 SA |
440 | if (!gen_interrupt_ && (samples_to_process == 0)) { |
441 | // Wait for more input | |
442 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
443 | gen_input_cond_.wait(gen_input_lock); | |
444 | } | |
4640a84e SA |
445 | } while (!gen_interrupt_); |
446 | } | |
447 | ||
3f1f6295 SA |
448 | signal_data* MathSignal::signal_from_name(const std::string& name) |
449 | { | |
450 | // Look up signal in the map and if it doesn't exist yet, add it for future use | |
451 | ||
452 | auto element = input_signals_.find(name); | |
453 | ||
454 | if (element != input_signals_.end()) { | |
455 | return &(element->second); | |
456 | } else { | |
457 | const vector< shared_ptr<SignalBase> > signalbases = session_.signalbases(); | |
458 | const QString sig_name = QString::fromStdString(name); | |
459 | ||
460 | for (const shared_ptr<SignalBase>& sb : signalbases) | |
bee54d9e SA |
461 | if (sb->name() == sig_name) { |
462 | if (!sb->analog_data()) | |
463 | continue; | |
464 | ||
144e72c9 SA |
465 | connect(sb->analog_data().get(), SIGNAL(samples_added(SharedPtrToSegment, uint64_t, uint64_t)), |
466 | this, SLOT(on_data_received())); | |
bee54d9e SA |
467 | connect(sb->analog_data().get(), SIGNAL(segment_completed()), |
468 | this, SLOT(on_data_received())); | |
469 | ||
3f1f6295 | 470 | return &(input_signals_.insert({name, signal_data(sb)}).first->second); |
bee54d9e | 471 | } |
3f1f6295 SA |
472 | } |
473 | ||
474 | return nullptr; | |
475 | } | |
476 | ||
477 | void MathSignal::update_signal_sample(signal_data* sig_data, uint32_t segment_id, uint64_t sample_num) | |
478 | { | |
479 | assert(sig_data); | |
480 | ||
481 | // Update the value only if a different sample is requested | |
482 | if (sig_data->sample_num == sample_num) | |
483 | return; | |
484 | ||
485 | assert(sig_data->sb); | |
486 | const shared_ptr<pv::data::Analog> analog = sig_data->sb->analog_data(); | |
487 | assert(analog); | |
488 | ||
489 | assert(segment_id < analog->analog_segments().size()); | |
490 | ||
491 | const shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
492 | ||
493 | sig_data->sample_num = sample_num; | |
494 | sig_data->sample_value = segment->get_sample(sample_num); | |
495 | ||
144e72c9 | 496 | // We only have a reference if this signal is used as a scalar; |
3f1f6295 SA |
497 | // if it's used by a function, it's null |
498 | if (sig_data->ref) | |
499 | *(sig_data->ref) = sig_data->sample_value; | |
500 | } | |
501 | ||
4640a84e SA |
502 | void MathSignal::on_capture_state_changed(int state) |
503 | { | |
504 | if (state == Session::Running) | |
505 | begin_generation(); | |
5eb9d1c4 | 506 | |
144e72c9 SA |
507 | // Make sure we don't miss any input samples, just in case |
508 | if (state == Session::Stopped) | |
509 | gen_input_cond_.notify_one(); | |
4640a84e SA |
510 | } |
511 | ||
512 | void MathSignal::on_data_received() | |
513 | { | |
514 | gen_input_cond_.notify_one(); | |
b0773a8a SA |
515 | } |
516 | ||
517 | } // namespace data | |
518 | } // namespace pv |