]>
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))); | |
102 | connect(&session_, SIGNAL(data_received()), | |
103 | this, SLOT(on_data_received())); | |
b0773a8a | 104 | |
4640a84e | 105 | expression_ = "sin(2 * pi * t) + cos(t / 2 * pi)"; |
b0773a8a SA |
106 | } |
107 | ||
108 | MathSignal::~MathSignal() | |
109 | { | |
4640a84e | 110 | reset_generation(); |
3f1f6295 SA |
111 | |
112 | if (fnc_sig_sample_) | |
113 | delete fnc_sig_sample_; | |
b0773a8a SA |
114 | } |
115 | ||
116 | void MathSignal::save_settings(QSettings &settings) const | |
117 | { | |
4640a84e SA |
118 | settings.setValue("expression", expression_); |
119 | ||
120 | settings.setValue("custom_sample_rate", (qulonglong)custom_sample_rate_); | |
121 | settings.setValue("custom_sample_count", (qulonglong)custom_sample_count_); | |
122 | settings.setValue("use_custom_sample_rate", use_custom_sample_rate_); | |
123 | settings.setValue("use_custom_sample_count", use_custom_sample_count_); | |
b0773a8a SA |
124 | } |
125 | ||
126 | void MathSignal::restore_settings(QSettings &settings) | |
127 | { | |
4640a84e SA |
128 | if (settings.contains("expression")) |
129 | expression_ = settings.value("expression").toString(); | |
130 | ||
131 | if (settings.contains("custom_sample_rate")) | |
132 | custom_sample_rate_ = settings.value("custom_sample_rate").toULongLong(); | |
133 | ||
134 | if (settings.contains("custom_sample_count")) | |
135 | custom_sample_count_ = settings.value("custom_sample_count").toULongLong(); | |
136 | ||
137 | if (settings.contains("use_custom_sample_rate")) | |
138 | use_custom_sample_rate_ = settings.value("use_custom_sample_rate").toBool(); | |
139 | ||
140 | if (settings.contains("use_custom_sample_count")) | |
141 | use_custom_sample_count_ = settings.value("use_custom_sample_count").toBool(); | |
142 | } | |
143 | ||
144 | QString MathSignal::error_message() const | |
145 | { | |
146 | return error_message_; | |
147 | } | |
148 | ||
149 | QString MathSignal::get_expression() const | |
150 | { | |
151 | return expression_; | |
152 | } | |
153 | ||
154 | void MathSignal::set_expression(QString expression) | |
155 | { | |
156 | expression_ = expression; | |
157 | ||
158 | begin_generation(); | |
159 | } | |
160 | ||
161 | void MathSignal::set_error_message(QString msg) | |
162 | { | |
163 | error_message_ = msg; | |
164 | // TODO Emulate noquote() | |
d203009f | 165 | qDebug().nospace() << name() << ": " << msg << "(Expression: '" << expression_ << "')"; |
4640a84e SA |
166 | } |
167 | ||
168 | uint64_t MathSignal::get_working_sample_count(uint32_t segment_id) const | |
169 | { | |
170 | // The working sample count is the highest sample number for | |
171 | // which all used signals have data available, so go through all | |
172 | // channels and use the lowest overall sample count of the segment | |
173 | ||
174 | int64_t result = std::numeric_limits<int64_t>::max(); | |
175 | ||
176 | if (use_custom_sample_count_) | |
177 | // A custom sample count implies that only one segment will be created | |
178 | result = (segment_id == 0) ? custom_sample_count_ : 0; | |
179 | else { | |
180 | if (input_signals_.size() > 0) { | |
3f1f6295 SA |
181 | for (auto input_signal : input_signals_) { |
182 | const shared_ptr<SignalBase>& sb = input_signal.second.sb; | |
183 | ||
4640a84e SA |
184 | shared_ptr<Analog> a = sb->analog_data(); |
185 | const uint32_t last_segment = (a->analog_segments().size() - 1); | |
186 | if (segment_id > last_segment) | |
187 | continue; | |
188 | const shared_ptr<AnalogSegment> segment = a->analog_segments()[segment_id]; | |
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 | ||
198 | void MathSignal::reset_generation() | |
199 | { | |
200 | if (gen_thread_.joinable()) { | |
201 | gen_interrupt_ = true; | |
202 | gen_input_cond_.notify_one(); | |
203 | gen_thread_.join(); | |
204 | } | |
205 | ||
206 | data_->clear(); | |
3f1f6295 | 207 | input_signals_.clear(); |
4640a84e | 208 | |
3f1f6295 SA |
209 | if (exprtk_parser_) { |
210 | delete exprtk_parser_; | |
211 | exprtk_parser_ = nullptr; | |
4640a84e SA |
212 | } |
213 | ||
214 | if (exprtk_expression_) { | |
215 | delete exprtk_expression_; | |
216 | exprtk_expression_ = nullptr; | |
217 | } | |
218 | ||
3f1f6295 SA |
219 | if (exprtk_symbol_table_) { |
220 | delete exprtk_symbol_table_; | |
221 | exprtk_symbol_table_ = nullptr; | |
222 | } | |
223 | ||
224 | if (exprtk_unknown_symbol_table_) { | |
225 | delete exprtk_unknown_symbol_table_; | |
226 | exprtk_unknown_symbol_table_ = nullptr; | |
227 | } | |
228 | ||
229 | if (fnc_sig_sample_) { | |
230 | delete fnc_sig_sample_; | |
231 | fnc_sig_sample_ = nullptr; | |
4640a84e SA |
232 | } |
233 | ||
234 | if (!error_message_.isEmpty()) { | |
235 | error_message_ = QString(); | |
236 | // TODO Emulate noquote() | |
237 | qDebug().nospace() << name() << ": Error cleared"; | |
238 | } | |
239 | } | |
240 | ||
241 | void MathSignal::begin_generation() | |
242 | { | |
243 | reset_generation(); | |
244 | ||
245 | if (expression_.isEmpty()) { | |
246 | set_error_message(tr("No expression defined, nothing to do")); | |
247 | return; | |
248 | } | |
249 | ||
3f1f6295 SA |
250 | fnc_sig_sample_ = new sig_sample<double>(*this); |
251 | ||
252 | exprtk_unknown_symbol_table_ = new exprtk::symbol_table<double>(); | |
253 | ||
4640a84e | 254 | exprtk_symbol_table_ = new exprtk::symbol_table<double>(); |
3f1f6295 | 255 | exprtk_symbol_table_->add_function("sig_sample", *fnc_sig_sample_); |
4640a84e SA |
256 | exprtk_symbol_table_->add_variable("t", exprtk_current_time_); |
257 | exprtk_symbol_table_->add_variable("s", exprtk_current_sample_); | |
258 | exprtk_symbol_table_->add_constants(); | |
259 | ||
260 | exprtk_expression_ = new exprtk::expression<double>(); | |
3f1f6295 | 261 | exprtk_expression_->register_symbol_table(*exprtk_unknown_symbol_table_); |
4640a84e SA |
262 | exprtk_expression_->register_symbol_table(*exprtk_symbol_table_); |
263 | ||
264 | exprtk_parser_ = new exprtk::parser<double>(); | |
3f1f6295 SA |
265 | exprtk_parser_->enable_unknown_symbol_resolver(); |
266 | ||
d203009f | 267 | if (!exprtk_parser_->compile(expression_.toStdString(), *exprtk_expression_)) { |
3f1f6295 | 268 | set_error_message(tr("Error in expression")); |
d203009f | 269 | } else { |
3f1f6295 SA |
270 | // Resolve unknown scalars to signals and add them to the input signal list |
271 | vector<string> unknowns; | |
272 | exprtk_unknown_symbol_table_->get_variable_list(unknowns); | |
273 | for (string& unknown : unknowns) { | |
274 | signal_data* sig_data = signal_from_name(unknown); | |
275 | const shared_ptr<SignalBase> signal = (sig_data) ? (sig_data->sb) : nullptr; | |
276 | if (!signal || (!signal->analog_data())) { | |
277 | set_error_message(QString(tr("%1 isn't a valid signal")).arg( | |
278 | QString::fromStdString(unknown))); | |
279 | } else | |
280 | sig_data->ref = &(exprtk_unknown_symbol_table_->variable_ref(unknown)); | |
281 | } | |
282 | } | |
283 | ||
284 | if (error_message_.isEmpty()) { | |
d203009f SA |
285 | gen_interrupt_ = false; |
286 | gen_thread_ = std::thread(&MathSignal::generation_proc, this); | |
287 | } | |
4640a84e SA |
288 | } |
289 | ||
290 | void MathSignal::generate_samples(uint32_t segment_id, const uint64_t start_sample, | |
291 | const int64_t sample_count) | |
292 | { | |
293 | shared_ptr<Analog> analog = dynamic_pointer_cast<Analog>(data_); | |
294 | shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
295 | ||
3f1f6295 SA |
296 | // Keep the math functions segment IDs in sync |
297 | fnc_sig_sample_->current_segment = segment_id; | |
298 | ||
4640a84e SA |
299 | const double sample_rate = data_->get_samplerate(); |
300 | ||
301 | exprtk_current_sample_ = start_sample; | |
302 | ||
303 | float *sample_data = new float[sample_count]; | |
304 | ||
305 | for (int64_t i = 0; i < sample_count; i++) { | |
4640a84e | 306 | exprtk_current_time_ = exprtk_current_sample_ / sample_rate; |
3f1f6295 SA |
307 | |
308 | for (auto& entry : input_signals_) { | |
309 | signal_data* sig_data = &(entry.second); | |
310 | update_signal_sample(sig_data, segment_id, exprtk_current_sample_); | |
311 | } | |
312 | ||
4640a84e SA |
313 | double value = exprtk_expression_->value(); |
314 | sample_data[i] = value; | |
3f1f6295 | 315 | exprtk_current_sample_ += 1; |
4640a84e SA |
316 | } |
317 | ||
318 | segment->append_interleaved_samples(sample_data, sample_count, 1); | |
319 | ||
320 | delete[] sample_data; | |
321 | } | |
322 | ||
323 | void MathSignal::generation_proc() | |
324 | { | |
325 | uint32_t segment_id = 0; | |
326 | ||
327 | // Don't do anything until we have a valid sample rate | |
328 | do { | |
329 | if (use_custom_sample_rate_) | |
330 | data_->set_samplerate(custom_sample_rate_); | |
331 | else | |
332 | data_->set_samplerate(session_.get_samplerate()); | |
333 | ||
334 | if (data_->get_samplerate() == 1) { | |
335 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
336 | gen_input_cond_.wait(gen_input_lock); | |
337 | } | |
338 | } while ((!gen_interrupt_) && (data_->get_samplerate() == 1)); | |
339 | ||
340 | if (gen_interrupt_) | |
341 | return; | |
342 | ||
5eb9d1c4 | 343 | shared_ptr<Analog> analog = analog_data(); |
4640a84e SA |
344 | |
345 | // Create initial analog segment | |
346 | shared_ptr<AnalogSegment> output_segment = | |
347 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
348 | analog->push_segment(output_segment); | |
349 | ||
350 | // Create analog samples | |
351 | do { | |
352 | const uint64_t input_sample_count = get_working_sample_count(segment_id); | |
353 | const uint64_t output_sample_count = output_segment->get_sample_count(); | |
354 | ||
355 | const uint64_t samples_to_process = | |
356 | (input_sample_count > output_sample_count) ? | |
357 | (input_sample_count - output_sample_count) : 0; | |
358 | ||
359 | // Process the samples if necessary... | |
360 | if (samples_to_process > 0) { | |
361 | const uint64_t chunk_sample_count = ChunkLength; | |
362 | ||
363 | uint64_t processed_samples = 0; | |
364 | do { | |
365 | const uint64_t start_sample = output_sample_count + processed_samples; | |
366 | const uint64_t sample_count = | |
367 | min(samples_to_process - processed_samples, chunk_sample_count); | |
368 | ||
369 | generate_samples(segment_id, start_sample, sample_count); | |
370 | processed_samples += sample_count; | |
371 | ||
372 | // Notify consumers of this signal's data | |
373 | // TODO Does this work when a conversion is active? | |
374 | samples_added(segment_id, start_sample, start_sample + processed_samples); | |
375 | } while (!gen_interrupt_ && (processed_samples < samples_to_process)); | |
376 | } | |
377 | ||
378 | if (samples_to_process == 0) { | |
379 | if (segment_id < session_.get_highest_segment_id()) { | |
5eb9d1c4 SA |
380 | analog->analog_segments().back()->set_complete(); |
381 | ||
4640a84e SA |
382 | // Process next segment |
383 | segment_id++; | |
384 | ||
385 | output_segment = | |
386 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
387 | analog->push_segment(output_segment); | |
388 | } else { | |
389 | // All segments have been processed, wait for more input | |
390 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
391 | gen_input_cond_.wait(gen_input_lock); | |
392 | } | |
393 | } | |
394 | ||
395 | } while (!gen_interrupt_); | |
396 | } | |
397 | ||
3f1f6295 SA |
398 | signal_data* MathSignal::signal_from_name(const std::string& name) |
399 | { | |
400 | // Look up signal in the map and if it doesn't exist yet, add it for future use | |
401 | ||
402 | auto element = input_signals_.find(name); | |
403 | ||
404 | if (element != input_signals_.end()) { | |
405 | return &(element->second); | |
406 | } else { | |
407 | const vector< shared_ptr<SignalBase> > signalbases = session_.signalbases(); | |
408 | const QString sig_name = QString::fromStdString(name); | |
409 | ||
410 | for (const shared_ptr<SignalBase>& sb : signalbases) | |
411 | if (sb->name() == sig_name) | |
412 | return &(input_signals_.insert({name, signal_data(sb)}).first->second); | |
413 | } | |
414 | ||
415 | return nullptr; | |
416 | } | |
417 | ||
418 | void MathSignal::update_signal_sample(signal_data* sig_data, uint32_t segment_id, uint64_t sample_num) | |
419 | { | |
420 | assert(sig_data); | |
421 | ||
422 | // Update the value only if a different sample is requested | |
423 | if (sig_data->sample_num == sample_num) | |
424 | return; | |
425 | ||
426 | assert(sig_data->sb); | |
427 | const shared_ptr<pv::data::Analog> analog = sig_data->sb->analog_data(); | |
428 | assert(analog); | |
429 | ||
430 | assert(segment_id < analog->analog_segments().size()); | |
431 | ||
432 | const shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
433 | ||
434 | sig_data->sample_num = sample_num; | |
435 | sig_data->sample_value = segment->get_sample(sample_num); | |
436 | ||
437 | // We only have a reference if this signal is used as a scalar, | |
438 | // if it's used by a function, it's null | |
439 | if (sig_data->ref) | |
440 | *(sig_data->ref) = sig_data->sample_value; | |
441 | } | |
442 | ||
4640a84e SA |
443 | void MathSignal::on_capture_state_changed(int state) |
444 | { | |
445 | if (state == Session::Running) | |
446 | begin_generation(); | |
5eb9d1c4 SA |
447 | |
448 | if (state == Session::Stopped) { | |
449 | shared_ptr<Analog> analog = analog_data(); | |
450 | analog->analog_segments().back()->set_complete(); | |
451 | } | |
4640a84e SA |
452 | } |
453 | ||
454 | void MathSignal::on_data_received() | |
455 | { | |
456 | gen_input_cond_.notify_one(); | |
b0773a8a SA |
457 | } |
458 | ||
459 | } // namespace data | |
460 | } // namespace pv |