]> sigrok.org Git - pulseview.git/blob - pv/binding/device.cpp
Allow setting a frame limit
[pulseview.git] / pv / binding / device.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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 #include <cstdint>
21
22 #include <QDebug>
23
24 #include "device.hpp"
25
26 #include <pv/prop/bool.hpp>
27 #include <pv/prop/enum.hpp>
28 #include <pv/prop/int.hpp>
29
30 #include <libsigrokcxx/libsigrokcxx.hpp>
31
32 using boost::optional;
33
34 using std::function;
35 using std::pair;
36 using std::set;
37 using std::shared_ptr;
38 using std::string;
39 using std::vector;
40
41 using sigrok::Capability;
42 using sigrok::Configurable;
43 using sigrok::ConfigKey;
44 using sigrok::Error;
45
46 using pv::prop::Bool;
47 using pv::prop::Enum;
48 using pv::prop::Int;
49 using pv::prop::Property;
50
51 namespace pv {
52 namespace binding {
53
54 Device::Device(shared_ptr<sigrok::Configurable> configurable) :
55         configurable_(configurable)
56 {
57
58         auto keys = configurable->config_keys();
59
60         for (auto key : keys) {
61
62                 auto capabilities = configurable->config_capabilities(key);
63
64                 if (!capabilities.count(Capability::GET) ||
65                         !capabilities.count(Capability::SET))
66                         continue;
67
68                 string name_str;
69                 try {
70                         name_str = key->description();
71                 } catch (Error& e) {
72                         name_str = key->name();
73                 }
74
75                 const QString name = QString::fromStdString(name_str);
76
77                 const Property::Getter get = [&, key]() {
78                         return configurable_->config_get(key); };
79                 const Property::Setter set = [&, key](Glib::VariantBase value) {
80                         configurable_->config_set(key, value);
81                         config_changed();
82                 };
83
84                 switch (key->id()) {
85                 case SR_CONF_SAMPLERATE:
86                         // Sample rate values are not bound because they are shown
87                         // in the MainBar
88                         break;
89
90                 case SR_CONF_CAPTURE_RATIO:
91                         bind_int(name, "", "%", pair<int64_t, int64_t>(0, 100), get, set);
92                         break;
93
94                 case SR_CONF_LIMIT_FRAMES:
95                         bind_int(name, "", "", pair<int64_t, int64_t>(0, 1000000), get, set);
96                         break;
97
98                 case SR_CONF_PATTERN_MODE:
99                 case SR_CONF_BUFFERSIZE:
100                 case SR_CONF_TRIGGER_SOURCE:
101                 case SR_CONF_TRIGGER_SLOPE:
102                 case SR_CONF_COUPLING:
103                 case SR_CONF_CLOCK_EDGE:
104                 case SR_CONF_DATA_SOURCE:
105                 case SR_CONF_EXTERNAL_CLOCK_SOURCE:
106                         bind_enum(name, "", key, capabilities, get, set);
107                         break;
108
109                 case SR_CONF_FILTER:
110                 case SR_CONF_EXTERNAL_CLOCK:
111                 case SR_CONF_RLE:
112                 case SR_CONF_POWER_OFF:
113                 case SR_CONF_AVERAGING:
114                         bind_bool(name, "", get, set);
115                         break;
116
117                 case SR_CONF_TIMEBASE:
118                         bind_enum(name, "", key, capabilities, get, set, print_timebase);
119                         break;
120
121                 case SR_CONF_VDIV:
122                         bind_enum(name, "", key, capabilities, get, set, print_vdiv);
123                         break;
124
125                 case SR_CONF_VOLTAGE_THRESHOLD:
126                         bind_enum(name, "", key, capabilities, get, set, print_voltage_threshold);
127                         break;
128
129                 case SR_CONF_PROBE_FACTOR:
130                         if (capabilities.count(Capability::LIST))
131                                 bind_enum(name, "", key, capabilities, get, set, print_probe_factor);
132                         else
133                                 bind_int(name, "", "", pair<int64_t, int64_t>(1, 500), get, set);
134                         break;
135
136                 case SR_CONF_AVG_SAMPLES:
137                         if (capabilities.count(Capability::LIST))
138                                 bind_enum(name, "", key, capabilities, get, set, print_averages);
139                         else
140                                 bind_int(name, "", "", pair<int64_t, int64_t>(0, INT32_MAX), get, set);
141                         break;
142
143                 default:
144                         break;
145                 }
146         }
147 }
148
149 void Device::bind_bool(const QString &name, const QString &desc,
150         Property::Getter getter, Property::Setter setter)
151 {
152         assert(configurable_);
153         properties_.push_back(shared_ptr<Property>(new Bool(
154                 name, desc, getter, setter)));
155 }
156
157 void Device::bind_enum(const QString &name, const QString &desc,
158         const ConfigKey *key, set<const Capability *> capabilities,
159         Property::Getter getter,
160         Property::Setter setter, function<QString (Glib::VariantBase)> printer)
161 {
162         assert(configurable_);
163
164         if (!capabilities.count(Capability::LIST))
165                 return;
166
167         try {
168                 Glib::VariantContainerBase gvar = configurable_->config_list(key);
169                 Glib::VariantIter iter(gvar);
170
171                 vector< pair<Glib::VariantBase, QString> > values;
172                 while ((iter.next_value(gvar)))
173                         values.emplace_back(gvar, printer(gvar));
174
175                 properties_.push_back(shared_ptr<Property>(new Enum(name, desc, values,
176                         getter, setter)));
177
178         } catch (sigrok::Error& e) {
179                 qDebug() << "Error: Listing device key" << name << "failed!";
180                 return;
181         }
182 }
183
184 void Device::bind_int(const QString &name, const QString &desc, QString suffix,
185         optional< pair<int64_t, int64_t> > range,
186         Property::Getter getter, Property::Setter setter)
187 {
188         assert(configurable_);
189
190         properties_.push_back(shared_ptr<Property>(new Int(name, desc, suffix,
191                 range, getter, setter)));
192 }
193
194 QString Device::print_timebase(Glib::VariantBase gvar)
195 {
196         uint64_t p, q;
197         g_variant_get(gvar.gobj(), "(tt)", &p, &q);
198         return QString::fromUtf8(sr_period_string(p, q));
199 }
200
201 QString Device::print_vdiv(Glib::VariantBase gvar)
202 {
203         uint64_t p, q;
204         g_variant_get(gvar.gobj(), "(tt)", &p, &q);
205         return QString::fromUtf8(sr_voltage_string(p, q));
206 }
207
208 QString Device::print_voltage_threshold(Glib::VariantBase gvar)
209 {
210         gdouble lo, hi;
211         g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
212         return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
213 }
214
215 QString Device::print_probe_factor(Glib::VariantBase gvar)
216 {
217         uint64_t factor;
218         factor = g_variant_get_uint64(gvar.gobj());
219         return QString("%1x").arg(factor);
220 }
221
222 QString Device::print_averages(Glib::VariantBase gvar)
223 {
224         uint64_t avg;
225         avg = g_variant_get_uint64(gvar.gobj());
226         return QString("%1").arg(avg);
227 }
228
229 }  // namespace binding
230 }  // namespace pv