]>
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_(""), | |
3f1f6295 | 87 | exprtk_unknown_symbol_table_(nullptr), |
4640a84e SA |
88 | exprtk_symbol_table_(nullptr), |
89 | exprtk_expression_(nullptr), | |
3f1f6295 SA |
90 | exprtk_parser_(nullptr), |
91 | fnc_sig_sample_(nullptr) | |
b0773a8a | 92 | { |
516b0c41 SA |
93 | uint32_t sig_idx = session_.get_next_signal_index(MathChannel); |
94 | set_name(QString(tr("Math%1")).arg(sig_idx)); | |
95 | set_color(AnalogSignalColors[(sig_idx - 1) % countof(AnalogSignalColors)]); | |
4640a84e | 96 | |
5eb9d1c4 | 97 | set_data(std::make_shared<data::Analog>()); |
b0773a8a | 98 | |
4640a84e SA |
99 | connect(&session_, SIGNAL(capture_state_changed(int)), |
100 | this, SLOT(on_capture_state_changed(int))); | |
b0773a8a SA |
101 | } |
102 | ||
103 | MathSignal::~MathSignal() | |
104 | { | |
4640a84e | 105 | reset_generation(); |
3f1f6295 SA |
106 | |
107 | if (fnc_sig_sample_) | |
108 | delete fnc_sig_sample_; | |
b0773a8a SA |
109 | } |
110 | ||
111 | void MathSignal::save_settings(QSettings &settings) const | |
112 | { | |
4640a84e SA |
113 | settings.setValue("expression", expression_); |
114 | ||
115 | settings.setValue("custom_sample_rate", (qulonglong)custom_sample_rate_); | |
116 | settings.setValue("custom_sample_count", (qulonglong)custom_sample_count_); | |
117 | settings.setValue("use_custom_sample_rate", use_custom_sample_rate_); | |
118 | settings.setValue("use_custom_sample_count", use_custom_sample_count_); | |
b0773a8a SA |
119 | } |
120 | ||
121 | void MathSignal::restore_settings(QSettings &settings) | |
122 | { | |
4640a84e SA |
123 | if (settings.contains("expression")) |
124 | expression_ = settings.value("expression").toString(); | |
125 | ||
126 | if (settings.contains("custom_sample_rate")) | |
127 | custom_sample_rate_ = settings.value("custom_sample_rate").toULongLong(); | |
128 | ||
129 | if (settings.contains("custom_sample_count")) | |
130 | custom_sample_count_ = settings.value("custom_sample_count").toULongLong(); | |
131 | ||
132 | if (settings.contains("use_custom_sample_rate")) | |
133 | use_custom_sample_rate_ = settings.value("use_custom_sample_rate").toBool(); | |
134 | ||
135 | if (settings.contains("use_custom_sample_count")) | |
136 | use_custom_sample_count_ = settings.value("use_custom_sample_count").toBool(); | |
137 | } | |
138 | ||
4640a84e SA |
139 | QString MathSignal::get_expression() const |
140 | { | |
141 | return expression_; | |
142 | } | |
143 | ||
144 | void MathSignal::set_expression(QString expression) | |
145 | { | |
146 | expression_ = expression; | |
147 | ||
148 | begin_generation(); | |
149 | } | |
150 | ||
151 | void MathSignal::set_error_message(QString msg) | |
152 | { | |
153 | error_message_ = msg; | |
154 | // TODO Emulate noquote() | |
d203009f | 155 | qDebug().nospace() << name() << ": " << msg << "(Expression: '" << expression_ << "')"; |
f6a93932 SA |
156 | |
157 | error_message_changed(msg); | |
4640a84e SA |
158 | } |
159 | ||
160 | uint64_t MathSignal::get_working_sample_count(uint32_t segment_id) const | |
161 | { | |
162 | // The working sample count is the highest sample number for | |
163 | // which all used signals have data available, so go through all | |
164 | // channels and use the lowest overall sample count of the segment | |
165 | ||
166 | int64_t result = std::numeric_limits<int64_t>::max(); | |
167 | ||
168 | if (use_custom_sample_count_) | |
169 | // A custom sample count implies that only one segment will be created | |
170 | result = (segment_id == 0) ? custom_sample_count_ : 0; | |
171 | else { | |
172 | if (input_signals_.size() > 0) { | |
3f1f6295 SA |
173 | for (auto input_signal : input_signals_) { |
174 | const shared_ptr<SignalBase>& sb = input_signal.second.sb; | |
175 | ||
4640a84e | 176 | shared_ptr<Analog> a = sb->analog_data(); |
bee54d9e SA |
177 | auto analog_segments = a->analog_segments(); |
178 | ||
179 | if (analog_segments.size() == 0) { | |
180 | result = 0; | |
181 | continue; | |
182 | } | |
183 | ||
184 | const uint32_t highest_segment_id = (analog_segments.size() - 1); | |
185 | if (segment_id > highest_segment_id) | |
4640a84e | 186 | continue; |
bee54d9e SA |
187 | |
188 | const shared_ptr<AnalogSegment> segment = analog_segments.at(segment_id); | |
4640a84e SA |
189 | result = min(result, (int64_t)segment->get_sample_count()); |
190 | } | |
191 | } else | |
192 | result = session_.get_segment_sample_count(segment_id); | |
193 | } | |
194 | ||
195 | return result; | |
196 | } | |
197 | ||
144e72c9 | 198 | void MathSignal::update_completeness(uint32_t segment_id, uint64_t output_sample_count) |
bee54d9e SA |
199 | { |
200 | bool output_complete = true; | |
201 | ||
202 | if (input_signals_.size() > 0) { | |
203 | for (auto input_signal : input_signals_) { | |
204 | const shared_ptr<SignalBase>& sb = input_signal.second.sb; | |
205 | ||
206 | shared_ptr<Analog> a = sb->analog_data(); | |
207 | auto analog_segments = a->analog_segments(); | |
208 | ||
209 | if (analog_segments.size() == 0) { | |
210 | output_complete = false; | |
211 | continue; | |
212 | } | |
213 | ||
214 | const uint32_t highest_segment_id = (analog_segments.size() - 1); | |
215 | if (segment_id > highest_segment_id) { | |
216 | output_complete = false; | |
217 | continue; | |
218 | } | |
219 | ||
220 | const shared_ptr<AnalogSegment> segment = analog_segments.at(segment_id); | |
144e72c9 SA |
221 | if (!segment->is_complete()) { |
222 | output_complete = false; | |
223 | continue; | |
224 | } | |
225 | ||
226 | if (output_sample_count < segment->get_sample_count()) | |
bee54d9e SA |
227 | output_complete = false; |
228 | } | |
144e72c9 SA |
229 | } else { |
230 | // We're done when we generated as many samples as the stopped session is long | |
231 | if ((session_.get_capture_state() != Session::Stopped) || | |
232 | (output_sample_count < session_.get_segment_sample_count(segment_id))) | |
233 | output_complete = false; | |
bee54d9e SA |
234 | } |
235 | ||
236 | if (output_complete) | |
237 | analog_data()->analog_segments().at(segment_id)->set_complete(); | |
238 | } | |
239 | ||
4640a84e SA |
240 | void MathSignal::reset_generation() |
241 | { | |
242 | if (gen_thread_.joinable()) { | |
243 | gen_interrupt_ = true; | |
244 | gen_input_cond_.notify_one(); | |
245 | gen_thread_.join(); | |
246 | } | |
247 | ||
248 | data_->clear(); | |
3f1f6295 | 249 | input_signals_.clear(); |
4640a84e | 250 | |
3f1f6295 SA |
251 | if (exprtk_parser_) { |
252 | delete exprtk_parser_; | |
253 | exprtk_parser_ = nullptr; | |
4640a84e SA |
254 | } |
255 | ||
256 | if (exprtk_expression_) { | |
257 | delete exprtk_expression_; | |
258 | exprtk_expression_ = nullptr; | |
259 | } | |
260 | ||
3f1f6295 SA |
261 | if (exprtk_symbol_table_) { |
262 | delete exprtk_symbol_table_; | |
263 | exprtk_symbol_table_ = nullptr; | |
264 | } | |
265 | ||
266 | if (exprtk_unknown_symbol_table_) { | |
267 | delete exprtk_unknown_symbol_table_; | |
268 | exprtk_unknown_symbol_table_ = nullptr; | |
269 | } | |
270 | ||
271 | if (fnc_sig_sample_) { | |
272 | delete fnc_sig_sample_; | |
273 | fnc_sig_sample_ = nullptr; | |
4640a84e SA |
274 | } |
275 | ||
276 | if (!error_message_.isEmpty()) { | |
277 | error_message_ = QString(); | |
278 | // TODO Emulate noquote() | |
279 | qDebug().nospace() << name() << ": Error cleared"; | |
280 | } | |
281 | } | |
282 | ||
283 | void MathSignal::begin_generation() | |
284 | { | |
285 | reset_generation(); | |
286 | ||
287 | if (expression_.isEmpty()) { | |
288 | set_error_message(tr("No expression defined, nothing to do")); | |
289 | return; | |
290 | } | |
291 | ||
144e72c9 SA |
292 | disconnect(this, SLOT(on_data_received())); |
293 | ||
3f1f6295 SA |
294 | fnc_sig_sample_ = new sig_sample<double>(*this); |
295 | ||
296 | exprtk_unknown_symbol_table_ = new exprtk::symbol_table<double>(); | |
297 | ||
4640a84e | 298 | exprtk_symbol_table_ = new exprtk::symbol_table<double>(); |
3f1f6295 | 299 | exprtk_symbol_table_->add_function("sig_sample", *fnc_sig_sample_); |
4640a84e SA |
300 | exprtk_symbol_table_->add_variable("t", exprtk_current_time_); |
301 | exprtk_symbol_table_->add_variable("s", exprtk_current_sample_); | |
302 | exprtk_symbol_table_->add_constants(); | |
303 | ||
304 | exprtk_expression_ = new exprtk::expression<double>(); | |
3f1f6295 | 305 | exprtk_expression_->register_symbol_table(*exprtk_unknown_symbol_table_); |
4640a84e SA |
306 | exprtk_expression_->register_symbol_table(*exprtk_symbol_table_); |
307 | ||
308 | exprtk_parser_ = new exprtk::parser<double>(); | |
3f1f6295 SA |
309 | exprtk_parser_->enable_unknown_symbol_resolver(); |
310 | ||
d203009f | 311 | if (!exprtk_parser_->compile(expression_.toStdString(), *exprtk_expression_)) { |
f6a93932 SA |
312 | QString error_details; |
313 | size_t error_count = exprtk_parser_->error_count(); | |
314 | ||
315 | for (size_t i = 0; i < error_count; i++) { | |
316 | typedef exprtk::parser_error::type error_t; | |
317 | error_t error = exprtk_parser_->get_error(i); | |
318 | exprtk::parser_error::update_error(error, expression_.toStdString()); | |
319 | ||
320 | QString error_detail = tr("%1 at line %2, column %3: %4"); | |
321 | if ((error_count > 1) && (i < (error_count - 1))) | |
322 | error_detail += "\n"; | |
323 | ||
324 | error_details += error_detail \ | |
325 | .arg(exprtk::parser_error::to_str(error.mode).c_str()) \ | |
326 | .arg(error.line_no) \ | |
327 | .arg(error.column_no) \ | |
328 | .arg(error.diagnostic.c_str()); | |
329 | } | |
330 | set_error_message(error_details); | |
d203009f | 331 | } else { |
3f1f6295 SA |
332 | // Resolve unknown scalars to signals and add them to the input signal list |
333 | vector<string> unknowns; | |
334 | exprtk_unknown_symbol_table_->get_variable_list(unknowns); | |
335 | for (string& unknown : unknowns) { | |
336 | signal_data* sig_data = signal_from_name(unknown); | |
337 | const shared_ptr<SignalBase> signal = (sig_data) ? (sig_data->sb) : nullptr; | |
338 | if (!signal || (!signal->analog_data())) { | |
bee54d9e | 339 | set_error_message(QString(tr("%1 isn't a valid analog signal")).arg( |
3f1f6295 SA |
340 | QString::fromStdString(unknown))); |
341 | } else | |
342 | sig_data->ref = &(exprtk_unknown_symbol_table_->variable_ref(unknown)); | |
343 | } | |
344 | } | |
345 | ||
346 | if (error_message_.isEmpty()) { | |
144e72c9 SA |
347 | // Connect to the session data notification if we have no input signals |
348 | if (input_signals_.empty()) | |
349 | connect(&session_, SIGNAL(data_received()), | |
350 | this, SLOT(on_data_received())); | |
351 | ||
d203009f SA |
352 | gen_interrupt_ = false; |
353 | gen_thread_ = std::thread(&MathSignal::generation_proc, this); | |
354 | } | |
4640a84e SA |
355 | } |
356 | ||
357 | void MathSignal::generate_samples(uint32_t segment_id, const uint64_t start_sample, | |
358 | const int64_t sample_count) | |
359 | { | |
360 | shared_ptr<Analog> analog = dynamic_pointer_cast<Analog>(data_); | |
361 | shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
362 | ||
3f1f6295 SA |
363 | // Keep the math functions segment IDs in sync |
364 | fnc_sig_sample_->current_segment = segment_id; | |
365 | ||
4640a84e SA |
366 | const double sample_rate = data_->get_samplerate(); |
367 | ||
368 | exprtk_current_sample_ = start_sample; | |
369 | ||
370 | float *sample_data = new float[sample_count]; | |
371 | ||
372 | for (int64_t i = 0; i < sample_count; i++) { | |
4640a84e | 373 | exprtk_current_time_ = exprtk_current_sample_ / sample_rate; |
3f1f6295 SA |
374 | |
375 | for (auto& entry : input_signals_) { | |
376 | signal_data* sig_data = &(entry.second); | |
377 | update_signal_sample(sig_data, segment_id, exprtk_current_sample_); | |
378 | } | |
379 | ||
4640a84e SA |
380 | double value = exprtk_expression_->value(); |
381 | sample_data[i] = value; | |
3f1f6295 | 382 | exprtk_current_sample_ += 1; |
4640a84e SA |
383 | } |
384 | ||
385 | segment->append_interleaved_samples(sample_data, sample_count, 1); | |
386 | ||
387 | delete[] sample_data; | |
388 | } | |
389 | ||
390 | void MathSignal::generation_proc() | |
391 | { | |
4640a84e SA |
392 | // Don't do anything until we have a valid sample rate |
393 | do { | |
394 | if (use_custom_sample_rate_) | |
395 | data_->set_samplerate(custom_sample_rate_); | |
396 | else | |
397 | data_->set_samplerate(session_.get_samplerate()); | |
398 | ||
399 | if (data_->get_samplerate() == 1) { | |
400 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
401 | gen_input_cond_.wait(gen_input_lock); | |
402 | } | |
403 | } while ((!gen_interrupt_) && (data_->get_samplerate() == 1)); | |
404 | ||
405 | if (gen_interrupt_) | |
406 | return; | |
407 | ||
144e72c9 | 408 | uint32_t segment_id = 0; |
5eb9d1c4 | 409 | shared_ptr<Analog> analog = analog_data(); |
4640a84e SA |
410 | |
411 | // Create initial analog segment | |
412 | shared_ptr<AnalogSegment> output_segment = | |
413 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
414 | analog->push_segment(output_segment); | |
415 | ||
416 | // Create analog samples | |
417 | do { | |
418 | const uint64_t input_sample_count = get_working_sample_count(segment_id); | |
419 | const uint64_t output_sample_count = output_segment->get_sample_count(); | |
420 | ||
421 | const uint64_t samples_to_process = | |
422 | (input_sample_count > output_sample_count) ? | |
423 | (input_sample_count - output_sample_count) : 0; | |
424 | ||
425 | // Process the samples if necessary... | |
426 | if (samples_to_process > 0) { | |
427 | const uint64_t chunk_sample_count = ChunkLength; | |
428 | ||
429 | uint64_t processed_samples = 0; | |
430 | do { | |
431 | const uint64_t start_sample = output_sample_count + processed_samples; | |
432 | const uint64_t sample_count = | |
433 | min(samples_to_process - processed_samples, chunk_sample_count); | |
434 | ||
435 | generate_samples(segment_id, start_sample, sample_count); | |
436 | processed_samples += sample_count; | |
437 | ||
438 | // Notify consumers of this signal's data | |
4640a84e SA |
439 | samples_added(segment_id, start_sample, start_sample + processed_samples); |
440 | } while (!gen_interrupt_ && (processed_samples < samples_to_process)); | |
441 | } | |
442 | ||
144e72c9 | 443 | update_completeness(segment_id, output_sample_count); |
5eb9d1c4 | 444 | |
144e72c9 | 445 | if (output_segment->is_complete() && (segment_id < session_.get_highest_segment_id())) { |
4640a84e SA |
446 | // Process next segment |
447 | segment_id++; | |
448 | ||
449 | output_segment = | |
450 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
451 | analog->push_segment(output_segment); | |
4640a84e SA |
452 | } |
453 | ||
144e72c9 SA |
454 | if (!gen_interrupt_ && (samples_to_process == 0)) { |
455 | // Wait for more input | |
456 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
457 | gen_input_cond_.wait(gen_input_lock); | |
458 | } | |
4640a84e SA |
459 | } while (!gen_interrupt_); |
460 | } | |
461 | ||
3f1f6295 SA |
462 | signal_data* MathSignal::signal_from_name(const std::string& name) |
463 | { | |
464 | // Look up signal in the map and if it doesn't exist yet, add it for future use | |
465 | ||
466 | auto element = input_signals_.find(name); | |
467 | ||
468 | if (element != input_signals_.end()) { | |
469 | return &(element->second); | |
470 | } else { | |
471 | const vector< shared_ptr<SignalBase> > signalbases = session_.signalbases(); | |
472 | const QString sig_name = QString::fromStdString(name); | |
473 | ||
474 | for (const shared_ptr<SignalBase>& sb : signalbases) | |
bee54d9e SA |
475 | if (sb->name() == sig_name) { |
476 | if (!sb->analog_data()) | |
477 | continue; | |
478 | ||
144e72c9 SA |
479 | connect(sb->analog_data().get(), SIGNAL(samples_added(SharedPtrToSegment, uint64_t, uint64_t)), |
480 | this, SLOT(on_data_received())); | |
bee54d9e SA |
481 | connect(sb->analog_data().get(), SIGNAL(segment_completed()), |
482 | this, SLOT(on_data_received())); | |
483 | ||
3f1f6295 | 484 | return &(input_signals_.insert({name, signal_data(sb)}).first->second); |
bee54d9e | 485 | } |
3f1f6295 SA |
486 | } |
487 | ||
488 | return nullptr; | |
489 | } | |
490 | ||
491 | void MathSignal::update_signal_sample(signal_data* sig_data, uint32_t segment_id, uint64_t sample_num) | |
492 | { | |
493 | assert(sig_data); | |
494 | ||
495 | // Update the value only if a different sample is requested | |
496 | if (sig_data->sample_num == sample_num) | |
497 | return; | |
498 | ||
499 | assert(sig_data->sb); | |
500 | const shared_ptr<pv::data::Analog> analog = sig_data->sb->analog_data(); | |
501 | assert(analog); | |
502 | ||
503 | assert(segment_id < analog->analog_segments().size()); | |
504 | ||
505 | const shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
506 | ||
507 | sig_data->sample_num = sample_num; | |
508 | sig_data->sample_value = segment->get_sample(sample_num); | |
509 | ||
144e72c9 | 510 | // We only have a reference if this signal is used as a scalar; |
3f1f6295 SA |
511 | // if it's used by a function, it's null |
512 | if (sig_data->ref) | |
513 | *(sig_data->ref) = sig_data->sample_value; | |
514 | } | |
515 | ||
4640a84e SA |
516 | void MathSignal::on_capture_state_changed(int state) |
517 | { | |
518 | if (state == Session::Running) | |
519 | begin_generation(); | |
5eb9d1c4 | 520 | |
144e72c9 SA |
521 | // Make sure we don't miss any input samples, just in case |
522 | if (state == Session::Stopped) | |
523 | gen_input_cond_.notify_one(); | |
4640a84e SA |
524 | } |
525 | ||
526 | void MathSignal::on_data_received() | |
527 | { | |
528 | gen_input_cond_.notify_one(); | |
b0773a8a SA |
529 | } |
530 | ||
531 | } // namespace data | |
532 | } // namespace pv |