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/double.hpp>
28 #include <pv/prop/enum.hpp>
29 #include <pv/prop/int.hpp>
31 #include <libsigrokcxx/libsigrokcxx.hpp>
33 using boost::optional;
39 using std::shared_ptr;
43 using sigrok::Capability;
44 using sigrok::Configurable;
45 using sigrok::ConfigKey;
49 using pv::prop::Double;
52 using pv::prop::Property;
57 Device::Device(shared_ptr<sigrok::Configurable> configurable) :
58 configurable_(configurable)
61 auto keys = configurable->config_keys();
63 for (auto key : keys) {
65 auto capabilities = configurable->config_capabilities(key);
67 if (!capabilities.count(Capability::GET) ||
68 !capabilities.count(Capability::SET))
73 name_str = key->description();
75 name_str = key->name();
78 const QString name = QString::fromStdString(name_str);
80 const Property::Getter get = [&, key]() {
81 return configurable_->config_get(key); };
82 const Property::Setter set = [&, key](Glib::VariantBase value) {
83 configurable_->config_set(key, value);
88 case SR_CONF_SAMPLERATE:
89 // Sample rate values are not bound because they are shown
93 case SR_CONF_CAPTURE_RATIO:
94 bind_int(name, "%", pair<int64_t, int64_t>(0, 100),
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 bind_enum(name, key, capabilities, get, set);
108 case SR_CONF_EXTERNAL_CLOCK:
110 case SR_CONF_POWER_OFF:
111 bind_bool(name, get, set);
114 case SR_CONF_TIMEBASE:
115 bind_enum(name, key, capabilities, get, set, print_timebase);
119 bind_enum(name, key, capabilities, get, set, print_vdiv);
122 case SR_CONF_VOLTAGE_THRESHOLD:
123 bind_enum(name, key, capabilities, get, set, print_voltage_threshold);
126 case SR_CONF_PROBE_FACTOR:
127 if (capabilities.count(Capability::LIST))
128 bind_enum(name, key, capabilities, get, set, print_probe_factor);
130 bind_int(name, "", pair<int64_t, int64_t>(1, 500), get, set);
139 void Device::bind_bool(const QString &name,
140 Property::Getter getter, Property::Setter setter)
142 assert(configurable_);
143 properties_.push_back(shared_ptr<Property>(new Bool(
144 name, getter, setter)));
147 void Device::bind_enum(const QString &name,
148 const ConfigKey *key, set<const Capability *> capabilities,
149 Property::Getter getter,
150 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
152 Glib::VariantBase gvar;
153 vector< pair<Glib::VariantBase, QString> > values;
155 assert(configurable_);
157 if (!capabilities.count(Capability::LIST))
160 Glib::VariantIter iter(configurable_->config_list(key));
161 while ((iter.next_value(gvar)))
162 values.push_back(make_pair(gvar, printer(gvar)));
164 properties_.push_back(shared_ptr<Property>(new Enum(name, values,
168 void Device::bind_int(const QString &name, QString suffix,
169 optional< pair<int64_t, int64_t> > range,
170 Property::Getter getter, Property::Setter setter)
172 assert(configurable_);
174 properties_.push_back(shared_ptr<Property>(new Int(name, suffix, range,
178 QString Device::print_timebase(Glib::VariantBase gvar)
181 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
182 return QString::fromUtf8(sr_period_string(p, q));
185 QString Device::print_vdiv(Glib::VariantBase gvar)
188 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
189 return QString::fromUtf8(sr_voltage_string(p, q));
192 QString Device::print_voltage_threshold(Glib::VariantBase gvar)
195 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
196 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
199 QString Device::print_probe_factor(Glib::VariantBase gvar)
202 factor = g_variant_get_uint64(gvar.gobj());
203 return QString("%1x").arg(factor);