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) {
62 auto capabilities = configurable->config_capabilities(key);
64 if (!capabilities.count(Capability::GET) ||
65 !capabilities.count(Capability::SET))
70 name_str = key->description();
72 name_str = key->name();
75 const QString name = QString::fromStdString(name_str);
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);
85 case SR_CONF_SAMPLERATE:
86 // Sample rate values are not bound because they are shown
90 case SR_CONF_CAPTURE_RATIO:
91 bind_int(name, "", "%", pair<int64_t, int64_t>(0, 100), get, set);
94 case SR_CONF_PATTERN_MODE:
95 case SR_CONF_BUFFERSIZE:
96 case SR_CONF_TRIGGER_SOURCE:
97 case SR_CONF_TRIGGER_SLOPE:
98 case SR_CONF_COUPLING:
99 case SR_CONF_CLOCK_EDGE:
100 bind_enum(name, "", key, capabilities, get, set);
104 case SR_CONF_EXTERNAL_CLOCK:
106 case SR_CONF_POWER_OFF:
107 bind_bool(name, "", get, set);
110 case SR_CONF_TIMEBASE:
111 bind_enum(name, "", key, capabilities, get, set, print_timebase);
115 bind_enum(name, "", key, capabilities, get, set, print_vdiv);
118 case SR_CONF_VOLTAGE_THRESHOLD:
119 bind_enum(name, "", key, capabilities, get, set, print_voltage_threshold);
122 case SR_CONF_PROBE_FACTOR:
123 if (capabilities.count(Capability::LIST))
124 bind_enum(name, "", key, capabilities, get, set, print_probe_factor);
126 bind_int(name, "", "", pair<int64_t, int64_t>(1, 500), get, set);
135 void Device::bind_bool(const QString &name, const QString &desc,
136 Property::Getter getter, Property::Setter setter)
138 assert(configurable_);
139 properties_.push_back(shared_ptr<Property>(new Bool(
140 name, desc, getter, setter)));
143 void Device::bind_enum(const QString &name, const QString &desc,
144 const ConfigKey *key, set<const Capability *> capabilities,
145 Property::Getter getter,
146 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
148 assert(configurable_);
150 if (!capabilities.count(Capability::LIST))
154 Glib::VariantContainerBase gvar = configurable_->config_list(key);
155 Glib::VariantIter iter(gvar);
157 vector< pair<Glib::VariantBase, QString> > values;
158 while ((iter.next_value(gvar)))
159 values.emplace_back(gvar, printer(gvar));
161 properties_.push_back(shared_ptr<Property>(new Enum(name, desc, values,
164 } catch (sigrok::Error& e) {
165 qDebug() << "Error: Listing device key" << name << "failed!";
170 void Device::bind_int(const QString &name, const QString &desc, QString suffix,
171 optional< pair<int64_t, int64_t> > range,
172 Property::Getter getter, Property::Setter setter)
174 assert(configurable_);
176 properties_.push_back(shared_ptr<Property>(new Int(name, desc, suffix,
177 range, getter, setter)));
180 QString Device::print_timebase(Glib::VariantBase gvar)
183 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
184 return QString::fromUtf8(sr_period_string(p, q));
187 QString Device::print_vdiv(Glib::VariantBase gvar)
190 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
191 return QString::fromUtf8(sr_voltage_string(p, q));
194 QString Device::print_voltage_threshold(Glib::VariantBase gvar)
197 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
198 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
201 QString Device::print_probe_factor(Glib::VariantBase gvar)
204 factor = g_variant_get_uint64(gvar.gobj());
205 return QString("%1x").arg(factor);
208 } // namespace binding