]> sigrok.org Git - pulseview.git/blame - pv/binding/device.cpp
Fix #1607 by ignoring common read-only config keys
[pulseview.git] / pv / binding / device.cpp
CommitLineData
cf47ef78
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
cf47ef78
JH
18 */
19
eb8269e3 20#include <cstdint>
4d5d5d6a 21
48c88445
JH
22#include <QDebug>
23
3cc9ad7b 24#include "device.hpp"
cf47ef78 25
2acdb232 26#include <pv/prop/bool.hpp>
2acdb232
JH
27#include <pv/prop/enum.hpp>
28#include <pv/prop/int.hpp>
7b3810db 29
fe3a1c21 30#include <libsigrokcxx/libsigrokcxx.hpp>
19adbc2c 31
819f4c25 32using boost::optional;
6f925ba9 33
6db73158 34using std::function;
819f4c25 35using std::pair;
6f925ba9 36using std::set;
f9abf97e 37using std::shared_ptr;
819f4c25
JH
38using std::string;
39using std::vector;
7b3810db 40
e8d00928
ML
41using sigrok::Capability;
42using sigrok::Configurable;
43using sigrok::ConfigKey;
44using sigrok::Error;
45
61703a01 46using pv::prop::Bool;
61703a01
JH
47using pv::prop::Enum;
48using pv::prop::Int;
49using pv::prop::Property;
50
cf47ef78 51namespace pv {
cf47ef78
JH
52namespace binding {
53
3cc9ad7b 54Device::Device(shared_ptr<sigrok::Configurable> configurable) :
8dbbc7f0 55 configurable_(configurable)
cf47ef78 56{
7b3810db 57
7bb0fbf4
ML
58 auto keys = configurable->config_keys();
59
60 for (auto key : keys) {
1e1b3a66 61
d1b479f7 62 string descr_str;
e8d00928 63 try {
d1b479f7 64 descr_str = key->description();
30677c13 65 } catch (Error& e) {
d1b479f7 66 descr_str = key->name();
e8d00928 67 }
d1b479f7 68 const QString descr = QString::fromStdString(descr_str);
e8d00928 69
d1b479f7 70 auto capabilities = configurable->config_capabilities(key);
4d5d5d6a 71
fe1e6ad6
SA
72 if (!capabilities.count(Capability::GET) ||
73 !capabilities.count(Capability::SET)) {
d1b479f7
SA
74
75 // Ignore common read-only keys
76 if ((key->id() == SR_CONF_CONTINUOUS) || (key->id() == SR_CONF_TRIGGER_MATCH) ||
77 (key->id() == SR_CONF_CONN) || (key->id() == SR_CONF_SERIALCOMM))
78 continue;
79
fe1e6ad6 80 qDebug() << QString(tr("Note for device developers: Ignoring device configuration capability '%1' " \
d1b479f7 81 "as it is missing GET and/or SET")).arg(descr);
fe1e6ad6
SA
82 continue;
83 }
84
6db73158 85 const Property::Getter get = [&, key]() {
8dbbc7f0 86 return configurable_->config_get(key); };
e8d00928 87 const Property::Setter set = [&, key](Glib::VariantBase value) {
8dbbc7f0 88 configurable_->config_set(key, value);
e8d00928
ML
89 config_changed();
90 };
6db73158 91
2ad82c2e 92 switch (key->id()) {
9f6af8bd 93 case SR_CONF_SAMPLERATE:
117ef009 94 // Sample rate values are not bound because they are shown
7c657094 95 // in the MainBar
9f6af8bd
JH
96 break;
97
a2b92157 98 case SR_CONF_CAPTURE_RATIO:
d1b479f7 99 bind_int(descr, "", "%", pair<int64_t, int64_t>(0, 100), get, set);
a2b92157
JH
100 break;
101
a2ccc698 102 case SR_CONF_LIMIT_FRAMES:
6275a668 103 // Value 0 means that there is no limit
d1b479f7 104 bind_int(descr, "", "", pair<int64_t, int64_t>(0, 1000000), get, set,
6275a668 105 tr("No Limit"));
a2ccc698
VO
106 break;
107
10b1be92 108 case SR_CONF_PATTERN_MODE:
10b1be92 109 case SR_CONF_BUFFERSIZE:
10b1be92 110 case SR_CONF_TRIGGER_SOURCE:
5be76b1a 111 case SR_CONF_TRIGGER_SLOPE:
4d5d5d6a 112 case SR_CONF_COUPLING:
5be76b1a 113 case SR_CONF_CLOCK_EDGE:
e639f241 114 case SR_CONF_DATA_SOURCE:
9cca8508 115 case SR_CONF_EXTERNAL_CLOCK_SOURCE:
d1b479f7 116 bind_enum(descr, "", key, capabilities, get, set);
7b3810db
JH
117 break;
118
0623eb8c 119 case SR_CONF_FILTER:
5be76b1a 120 case SR_CONF_EXTERNAL_CLOCK:
de1d99bb 121 case SR_CONF_RLE:
198f9bc7 122 case SR_CONF_POWER_OFF:
b196610d 123 case SR_CONF_AVERAGING:
d1b479f7 124 bind_bool(descr, "", get, set);
de1d99bb
JH
125 break;
126
4d5d5d6a 127 case SR_CONF_TIMEBASE:
d1b479f7 128 bind_enum(descr, "", key, capabilities, get, set, print_timebase);
7b3810db
JH
129 break;
130
4d5d5d6a 131 case SR_CONF_VDIV:
d1b479f7 132 bind_enum(descr, "", key, capabilities, get, set, print_vdiv);
7b3810db 133 break;
62874984
MC
134
135 case SR_CONF_VOLTAGE_THRESHOLD:
d1b479f7 136 bind_enum(descr, "", key, capabilities, get, set, print_voltage_threshold);
62874984 137 break;
4d5d5d6a 138
198f9bc7 139 case SR_CONF_PROBE_FACTOR:
49c62417 140 if (capabilities.count(Capability::LIST))
d1b479f7 141 bind_enum(descr, "", key, capabilities, get, set, print_probe_factor);
49c62417 142 else
d1b479f7 143 bind_int(descr, "", "", pair<int64_t, int64_t>(1, 500), get, set);
198f9bc7
BG
144 break;
145
f1a79cae 146 case SR_CONF_AVG_SAMPLES:
f125ab7f 147 if (capabilities.count(Capability::LIST))
d1b479f7 148 bind_enum(descr, "", key, capabilities, get, set, print_averages);
f125ab7f 149 else
d1b479f7 150 bind_int(descr, "", "", pair<int64_t, int64_t>(0, INT32_MAX), get, set);
f1a79cae
UH
151 break;
152
e8d00928
ML
153 default:
154 break;
155 }
7b3810db
JH
156 }
157}
158
9a267f8d 159void Device::bind_bool(const QString &name, const QString &desc,
6db73158 160 Property::Getter getter, Property::Setter setter)
de1d99bb 161{
8dbbc7f0
JH
162 assert(configurable_);
163 properties_.push_back(shared_ptr<Property>(new Bool(
9a267f8d 164 name, desc, getter, setter)));
de1d99bb
JH
165}
166
9a267f8d 167void Device::bind_enum(const QString &name, const QString &desc,
6f925ba9 168 const ConfigKey *key, set<const Capability *> capabilities,
ef2986c0 169 Property::Getter getter,
e8d00928 170 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
9f6af8bd 171{
8dbbc7f0 172 assert(configurable_);
de1d99bb 173
ef2986c0
AJ
174 if (!capabilities.count(Capability::LIST))
175 return;
176
a9627352
SA
177 try {
178 Glib::VariantContainerBase gvar = configurable_->config_list(key);
179 Glib::VariantIter iter(gvar);
180
181 vector< pair<Glib::VariantBase, QString> > values;
182 while ((iter.next_value(gvar)))
20f59e95 183 values.emplace_back(gvar, printer(gvar));
a9627352
SA
184
185 properties_.push_back(shared_ptr<Property>(new Enum(name, desc, values,
186 getter, setter)));
9f6af8bd 187
a9627352
SA
188 } catch (sigrok::Error& e) {
189 qDebug() << "Error: Listing device key" << name << "failed!";
190 return;
191 }
9f6af8bd
JH
192}
193
9a267f8d 194void Device::bind_int(const QString &name, const QString &desc, QString suffix,
6275a668
SA
195 optional< pair<int64_t, int64_t> > range, Property::Getter getter,
196 Property::Setter setter, QString special_value_text)
a2b92157 197{
8dbbc7f0 198 assert(configurable_);
19adbc2c 199
9a267f8d 200 properties_.push_back(shared_ptr<Property>(new Int(name, desc, suffix,
6275a668 201 range, getter, setter, special_value_text)));
a2b92157
JH
202}
203
3cc9ad7b 204QString Device::print_timebase(Glib::VariantBase gvar)
4d5d5d6a
BV
205{
206 uint64_t p, q;
e8d00928 207 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
634ecdcd 208 return QString::fromUtf8(sr_period_string(p, q));
4d5d5d6a
BV
209}
210
3cc9ad7b 211QString Device::print_vdiv(Glib::VariantBase gvar)
4d5d5d6a
BV
212{
213 uint64_t p, q;
e8d00928 214 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
27e8df22 215 return QString::fromUtf8(sr_voltage_string(p, q));
9f6af8bd
JH
216}
217
3cc9ad7b 218QString Device::print_voltage_threshold(Glib::VariantBase gvar)
62874984
MC
219{
220 gdouble lo, hi;
e8d00928 221 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
b47e449a 222 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
62874984
MC
223}
224
49c62417
AJ
225QString Device::print_probe_factor(Glib::VariantBase gvar)
226{
227 uint64_t factor;
228 factor = g_variant_get_uint64(gvar.gobj());
229 return QString("%1x").arg(factor);
230}
231
f125ab7f
UH
232QString Device::print_averages(Glib::VariantBase gvar)
233{
234 uint64_t avg;
235 avg = g_variant_get_uint64(gvar.gobj());
236 return QString("%1").arg(avg);
237}
238
870ea3db
UH
239} // namespace binding
240} // namespace pv