]> sigrok.org Git - pulseview.git/blame - pv/data/mathsignal.hpp
Make error handling generic improve math error detail
[pulseview.git] / pv / data / mathsignal.hpp
CommitLineData
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#ifndef PULSEVIEW_PV_DATA_MATHSIGNAL_HPP
21#define PULSEVIEW_PV_DATA_MATHSIGNAL_HPP
22
3f1f6295
SA
23#include <limits>
24
b0773a8a
SA
25#include <QString>
26
4640a84e
SA
27#include <pv/exprtk.hpp>
28#include <pv/util.hpp>
b0773a8a
SA
29#include <pv/data/analog.hpp>
30#include <pv/data/signalbase.hpp>
b0773a8a 31
4640a84e
SA
32using std::atomic;
33using std::condition_variable;
34using std::mutex;
3f1f6295 35using std::numeric_limits;
b0773a8a
SA
36using std::shared_ptr;
37
38namespace pv {
39class Session;
40
41namespace data {
42
43class SignalBase;
b0773a8a 44
3f1f6295
SA
45template<typename T>
46struct sig_sample;
47
48struct signal_data {
49 signal_data(const shared_ptr<SignalBase> _sb) :
50 sb(_sb), sample_num(numeric_limits<uint64_t>::max()), sample_value(0), ref(nullptr)
51 {}
52
53 const shared_ptr<SignalBase> sb;
54 uint64_t sample_num;
55 double sample_value;
56 double* ref;
57};
58
b0773a8a
SA
59class MathSignal : public SignalBase
60{
61 Q_OBJECT
4640a84e 62 Q_PROPERTY(QString expression READ get_expression WRITE set_expression NOTIFY expression_changed)
b0773a8a
SA
63
64private:
65 static const int64_t ChunkLength;
66
67public:
68 MathSignal(pv::Session &session);
69 virtual ~MathSignal();
70
71 virtual void save_settings(QSettings &settings) const;
72 virtual void restore_settings(QSettings &settings);
73
4640a84e
SA
74 QString get_expression() const;
75 void set_expression(QString expression);
76
77private:
f6a93932 78 virtual void set_error_message(QString msg);
4640a84e
SA
79
80 /**
81 * Returns the number of samples that can be worked on,
82 * i.e. the number of samples where samples are available
83 * for all connected channels.
84 * If the math signal uses no input channels, this is the
85 * number of samples in the session.
86 */
87 uint64_t get_working_sample_count(uint32_t segment_id) const;
88
144e72c9 89 void update_completeness(uint32_t segment_id, uint64_t output_sample_count);
bee54d9e 90
4640a84e
SA
91 void reset_generation();
92 void begin_generation();
93
94 void generate_samples(uint32_t segment_id, const uint64_t start_sample,
95 const int64_t sample_count);
96 void generation_proc();
97
3f1f6295
SA
98 signal_data* signal_from_name(const std::string& name);
99 void update_signal_sample(signal_data* sig_data, uint32_t segment_id, uint64_t sample_num);
100
b0773a8a
SA
101Q_SIGNALS:
102 void samples_cleared();
103
104 void samples_added(uint64_t segment_id, uint64_t start_sample,
105 uint64_t end_sample);
106
107 void min_max_changed(float min, float max);
108
4640a84e 109 void expression_changed(QString expression);
b0773a8a 110
4640a84e
SA
111private Q_SLOTS:
112 void on_capture_state_changed(int state);
113 void on_data_received();
b0773a8a
SA
114
115private:
116 pv::Session &session_;
117
4640a84e
SA
118 uint64_t custom_sample_rate_;
119 uint64_t custom_sample_count_;
120 bool use_custom_sample_rate_, use_custom_sample_count_;
3f1f6295 121 map<std::string, signal_data> input_signals_;
4640a84e 122
f6a93932 123 QString expression_;
4640a84e
SA
124
125 mutable mutex input_mutex_;
126 mutable condition_variable gen_input_cond_;
127
128 std::thread gen_thread_;
129 atomic<bool> gen_interrupt_;
130
3f1f6295 131 exprtk::symbol_table<double> *exprtk_unknown_symbol_table_, *exprtk_symbol_table_;
4640a84e
SA
132 exprtk::expression<double> *exprtk_expression_;
133 exprtk::parser<double> *exprtk_parser_;
134 double exprtk_current_time_, exprtk_current_sample_;
3f1f6295
SA
135
136 sig_sample<double>* fnc_sig_sample_;
137
138 // Give sig_sample access to the private helper functions
139 friend struct sig_sample<double>;
b0773a8a
SA
140};
141
142} // namespace data
143} // namespace pv
144
145#endif // PULSEVIEW_PV_DATA_MATHSIGNAL_HPP