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