]> sigrok.org Git - pulseview.git/blob - pv/data/mathsignal.hpp
Implement MathSignal
[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 <QString>
24
25 #include <pv/exprtk.hpp>
26 #include <pv/util.hpp>
27 #include <pv/data/analog.hpp>
28 #include <pv/data/signalbase.hpp>
29
30 using std::atomic;
31 using std::condition_variable;
32 using std::mutex;
33 using std::shared_ptr;
34
35 namespace pv {
36 class Session;
37
38 namespace data {
39
40 class SignalBase;
41
42 class MathSignal : public SignalBase
43 {
44         Q_OBJECT
45         Q_PROPERTY(QString expression READ get_expression WRITE set_expression NOTIFY expression_changed)
46         Q_PROPERTY(QString error_message READ error_message)
47
48 private:
49         static const int64_t ChunkLength;
50
51 public:
52         MathSignal(pv::Session &session);
53         virtual ~MathSignal();
54
55         virtual void save_settings(QSettings &settings) const;
56         virtual void restore_settings(QSettings &settings);
57
58         QString error_message() const;
59
60         QString get_expression() const;
61         void set_expression(QString expression);
62
63 private:
64         void set_error_message(QString msg);
65
66         /**
67          * Returns the number of samples that can be worked on,
68          * i.e. the number of samples where samples are available
69          * for all connected channels.
70          * If the math signal uses no input channels, this is the
71          * number of samples in the session.
72          */
73         uint64_t get_working_sample_count(uint32_t segment_id) const;
74
75         void reset_generation();
76         void begin_generation();
77
78         void generate_samples(uint32_t segment_id, const uint64_t start_sample,
79                 const int64_t sample_count);
80         void generation_proc();
81
82 Q_SIGNALS:
83         void samples_cleared();
84
85         void samples_added(uint64_t segment_id, uint64_t start_sample,
86                 uint64_t end_sample);
87
88         void min_max_changed(float min, float max);
89
90         void expression_changed(QString expression);
91
92 private Q_SLOTS:
93         void on_capture_state_changed(int state);
94         void on_data_received();
95
96 private:
97         pv::Session &session_;
98
99         uint64_t custom_sample_rate_;
100         uint64_t custom_sample_count_;
101         bool use_custom_sample_rate_, use_custom_sample_count_;
102         vector< shared_ptr<SignalBase>> input_signals_;
103
104         QString expression_;
105
106         QString error_message_;
107
108         mutable mutex input_mutex_;
109         mutable condition_variable gen_input_cond_;
110
111         std::thread gen_thread_;
112         atomic<bool> gen_interrupt_;
113
114         exprtk::symbol_table<double> *exprtk_symbol_table_;
115         exprtk::expression<double> *exprtk_expression_;
116         exprtk::parser<double> *exprtk_parser_;
117         double exprtk_current_time_, exprtk_current_sample_;
118 };
119
120 } // namespace data
121 } // namespace pv
122
123 #endif // PULSEVIEW_PV_DATA_MATHSIGNAL_HPP