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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "deviceoptions.h"
27 #include <pv/prop/bool.h>
28 #include <pv/prop/double.h>
29 #include <pv/prop/enum.h>
30 #include <pv/prop/int.h>
32 #include <libsigrok/libsigrok.hpp>
34 using boost::optional;
38 using std::shared_ptr;
42 using sigrok::Capability;
43 using sigrok::Configurable;
44 using sigrok::ConfigKey;
51 DeviceOptions::DeviceOptions(shared_ptr<sigrok::Configurable> configurable) :
52 _configurable(configurable)
56 for (auto entry : configurable->config_keys(ConfigKey::DEVICE_OPTIONS)) {
57 auto key = entry.first;
58 auto capabilities = entry.second;
60 Glib::VariantContainerBase gvar_list;
62 if (!capabilities.count(Capability::GET) ||
63 !capabilities.count(Capability::SET))
66 if (capabilities.count(Capability::LIST))
67 gvar_list = configurable->config_list(key);
71 name_str = key->description();
73 name_str = key->name();
76 const QString name = QString::fromStdString(name_str);
78 const Property::Getter get = [&, key]() {
79 return _configurable->config_get(key); };
80 const Property::Setter set = [&, key](Glib::VariantBase value) {
81 _configurable->config_set(key, value);
87 case SR_CONF_SAMPLERATE:
88 // Sample rate values are not bound because they are shown
92 case SR_CONF_CAPTURE_RATIO:
93 bind_int(name, "%", pair<int64_t, int64_t>(0, 100),
97 case SR_CONF_PATTERN_MODE:
98 case SR_CONF_BUFFERSIZE:
99 case SR_CONF_TRIGGER_SOURCE:
100 case SR_CONF_TRIGGER_SLOPE:
102 case SR_CONF_COUPLING:
103 case SR_CONF_CLOCK_EDGE:
104 bind_enum(name, gvar_list, get, set);
107 case SR_CONF_EXTERNAL_CLOCK:
109 bind_bool(name, get, set);
112 case SR_CONF_TIMEBASE:
113 bind_enum(name, gvar_list, get, set, print_timebase);
117 bind_enum(name, gvar_list, get, set, print_vdiv);
120 case SR_CONF_VOLTAGE_THRESHOLD:
121 bind_enum(name, gvar_list, get, set, print_voltage_threshold);
130 void DeviceOptions::bind_bool(const QString &name,
131 Property::Getter getter, Property::Setter setter)
133 assert(_configurable);
134 _properties.push_back(shared_ptr<Property>(new Bool(
135 name, getter, setter)));
138 void DeviceOptions::bind_enum(const QString &name,
139 Glib::VariantContainerBase gvar_list, Property::Getter getter,
140 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
142 Glib::VariantBase gvar;
143 vector< pair<Glib::VariantBase, QString> > values;
145 assert(_configurable);
147 Glib::VariantIter iter(gvar_list);
148 while ((iter.next_value(gvar)))
149 values.push_back(make_pair(gvar, printer(gvar)));
151 _properties.push_back(shared_ptr<Property>(new Enum(name, values,
155 void DeviceOptions::bind_int(const QString &name, QString suffix,
156 optional< std::pair<int64_t, int64_t> > range,
157 Property::Getter getter, Property::Setter setter)
159 assert(_configurable);
161 _properties.push_back(shared_ptr<Property>(new Int(name, suffix, range,
165 QString DeviceOptions::print_timebase(Glib::VariantBase gvar)
168 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
169 return QString::fromUtf8(sr_period_string(p * q));
172 QString DeviceOptions::print_vdiv(Glib::VariantBase gvar)
175 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
176 return QString::fromUtf8(sr_voltage_string(p, q));
179 QString DeviceOptions::print_voltage_threshold(Glib::VariantBase gvar)
182 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
183 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);