2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
26 #include <pv/prop/bool.hpp>
27 #include <pv/prop/enum.hpp>
28 #include <pv/prop/int.hpp>
30 #include <libsigrokcxx/libsigrokcxx.hpp>
32 using boost::optional;
37 using std::shared_ptr;
41 using sigrok::Capability;
42 using sigrok::Configurable;
43 using sigrok::ConfigKey;
49 using pv::prop::Property;
54 Device::Device(shared_ptr<sigrok::Configurable> configurable) :
55 configurable_(configurable)
58 auto keys = configurable->config_keys();
60 for (auto key : keys) {
64 descr_str = key->description();
66 descr_str = key->name();
68 const QString descr = QString::fromStdString(descr_str);
70 auto capabilities = configurable->config_capabilities(key);
72 if (!capabilities.count(Capability::GET) ||
73 !capabilities.count(Capability::SET)) {
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))
80 qDebug() << QString(tr("Note for device developers: Ignoring device configuration capability '%1' " \
81 "as it is missing GET and/or SET")).arg(descr);
85 const Property::Getter get = [&, key]() {
86 return configurable_->config_get(key); };
87 const Property::Setter set = [&, key](Glib::VariantBase value) {
88 configurable_->config_set(key, value);
93 case SR_CONF_SAMPLERATE:
94 // Sample rate values are not bound because they are shown
98 case SR_CONF_CAPTURE_RATIO:
99 bind_int(descr, "", "%", pair<int64_t, int64_t>(0, 100), get, set);
102 case SR_CONF_LIMIT_FRAMES:
103 // Value 0 means that there is no limit
104 bind_int(descr, "", "", pair<int64_t, int64_t>(0, 1000000), get, set,
108 case SR_CONF_PATTERN_MODE:
109 case SR_CONF_BUFFERSIZE:
110 case SR_CONF_TRIGGER_SOURCE:
111 case SR_CONF_TRIGGER_SLOPE:
112 case SR_CONF_COUPLING:
113 case SR_CONF_CLOCK_EDGE:
114 case SR_CONF_DATA_SOURCE:
115 case SR_CONF_EXTERNAL_CLOCK_SOURCE:
116 bind_enum(descr, "", key, capabilities, get, set);
120 case SR_CONF_EXTERNAL_CLOCK:
122 case SR_CONF_POWER_OFF:
123 case SR_CONF_AVERAGING:
124 bind_bool(descr, "", get, set);
127 case SR_CONF_TIMEBASE:
128 bind_enum(descr, "", key, capabilities, get, set, print_timebase);
132 bind_enum(descr, "", key, capabilities, get, set, print_vdiv);
135 case SR_CONF_VOLTAGE_THRESHOLD:
136 bind_enum(descr, "", key, capabilities, get, set, print_voltage_threshold);
139 case SR_CONF_PROBE_FACTOR:
140 if (capabilities.count(Capability::LIST))
141 bind_enum(descr, "", key, capabilities, get, set, print_probe_factor);
143 bind_int(descr, "", "", pair<int64_t, int64_t>(1, 500), get, set);
146 case SR_CONF_AVG_SAMPLES:
147 if (capabilities.count(Capability::LIST))
148 bind_enum(descr, "", key, capabilities, get, set, print_averages);
150 bind_int(descr, "", "", pair<int64_t, int64_t>(0, INT32_MAX), get, set);
159 void Device::bind_bool(const QString &name, const QString &desc,
160 Property::Getter getter, Property::Setter setter)
162 assert(configurable_);
163 properties_.push_back(shared_ptr<Property>(new Bool(
164 name, desc, getter, setter)));
167 void Device::bind_enum(const QString &name, const QString &desc,
168 const ConfigKey *key, set<const Capability *> capabilities,
169 Property::Getter getter,
170 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
172 assert(configurable_);
174 if (!capabilities.count(Capability::LIST))
178 Glib::VariantContainerBase gvar = configurable_->config_list(key);
179 Glib::VariantIter iter(gvar);
181 vector< pair<Glib::VariantBase, QString> > values;
182 while ((iter.next_value(gvar)))
183 values.emplace_back(gvar, printer(gvar));
185 properties_.push_back(shared_ptr<Property>(new Enum(name, desc, values,
188 } catch (sigrok::Error& e) {
189 qDebug() << "Error: Listing device key" << name << "failed!";
194 void Device::bind_int(const QString &name, const QString &desc, QString suffix,
195 optional< pair<int64_t, int64_t> > range, Property::Getter getter,
196 Property::Setter setter, QString special_value_text)
198 assert(configurable_);
200 properties_.push_back(shared_ptr<Property>(new Int(name, desc, suffix,
201 range, getter, setter, special_value_text)));
204 QString Device::print_timebase(Glib::VariantBase gvar)
207 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
208 return QString::fromUtf8(sr_period_string(p, q));
211 QString Device::print_vdiv(Glib::VariantBase gvar)
214 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
215 return QString::fromUtf8(sr_voltage_string(p, q));
218 QString Device::print_voltage_threshold(Glib::VariantBase gvar)
221 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
222 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
225 QString Device::print_probe_factor(Glib::VariantBase gvar)
228 factor = g_variant_get_uint64(gvar.gobj());
229 return QString("%1x").arg(factor);
232 QString Device::print_averages(Glib::VariantBase gvar)
235 avg = g_variant_get_uint64(gvar.gobj());
236 return QString("%1").arg(avg);
239 } // namespace binding