]> sigrok.org Git - pulseview.git/blame - pv/data/mathsignal.hpp
Implement MathSignal
[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
b0773a8a
SA
23#include <QString>
24
4640a84e
SA
25#include <pv/exprtk.hpp>
26#include <pv/util.hpp>
b0773a8a
SA
27#include <pv/data/analog.hpp>
28#include <pv/data/signalbase.hpp>
b0773a8a 29
4640a84e
SA
30using std::atomic;
31using std::condition_variable;
32using std::mutex;
b0773a8a
SA
33using std::shared_ptr;
34
35namespace pv {
36class Session;
37
38namespace data {
39
40class SignalBase;
b0773a8a
SA
41
42class MathSignal : public SignalBase
43{
44 Q_OBJECT
4640a84e
SA
45 Q_PROPERTY(QString expression READ get_expression WRITE set_expression NOTIFY expression_changed)
46 Q_PROPERTY(QString error_message READ error_message)
b0773a8a
SA
47
48private:
49 static const int64_t ChunkLength;
50
51public:
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
4640a84e
SA
58 QString error_message() const;
59
60 QString get_expression() const;
61 void set_expression(QString expression);
62
63private:
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
b0773a8a
SA
82Q_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
4640a84e 90 void expression_changed(QString expression);
b0773a8a 91
4640a84e
SA
92private Q_SLOTS:
93 void on_capture_state_changed(int state);
94 void on_data_received();
b0773a8a
SA
95
96private:
97 pv::Session &session_;
98
4640a84e
SA
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
b0773a8a 106 QString error_message_;
4640a84e
SA
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_;
b0773a8a
SA
118};
119
120} // namespace data
121} // namespace pv
122
123#endif // PULSEVIEW_PV_DATA_MATHSIGNAL_HPP