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