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/device/devinst.h>
28 #include <pv/prop/bool.h>
29 #include <pv/prop/double.h>
30 #include <pv/prop/enum.h>
31 #include <pv/prop/int.h>
33 #include <libsigrok/libsigrok.h>
35 using boost::optional;
39 using std::shared_ptr;
47 DeviceOptions::DeviceOptions(shared_ptr<pv::device::DevInst> dev_inst,
48 const sr_channel_group *group) :
57 if (!(gvar_opts = dev_inst->list_config(group, SR_CONF_DEVICE_OPTIONS)))
58 /* Driver supports no device instance options. */
61 const int *const options = (const int32_t *)g_variant_get_fixed_array(
62 gvar_opts, &num_opts, sizeof(int32_t));
63 for (unsigned int i = 0; i < num_opts; i++) {
64 const struct sr_config_info *const info =
65 sr_config_info_get(options[i]);
70 const int key = info->key;
71 GVariant *const gvar_list = dev_inst->list_config(group, key);
73 const QString name = QString::fromUtf8(info->name);
75 const Property::Getter get = [&, key]() {
76 return _dev_inst->get_config(_group, key); };
77 const Property::Setter set = [&, key](GVariant *value) {
78 _dev_inst->set_config(_group, key, value); };
82 case SR_CONF_SAMPLERATE:
83 // Sample rate values are not bound because they are shown
87 case SR_CONF_CAPTURE_RATIO:
88 bind_int(name, "%", pair<int64_t, int64_t>(0, 100),
92 case SR_CONF_PATTERN_MODE:
93 case SR_CONF_BUFFERSIZE:
94 case SR_CONF_TRIGGER_SOURCE:
95 case SR_CONF_TRIGGER_SLOPE:
97 case SR_CONF_COUPLING:
98 case SR_CONF_CLOCK_EDGE:
99 bind_enum(name, key, gvar_list, get, set);
102 case SR_CONF_EXTERNAL_CLOCK:
104 bind_bool(name, get, set);
107 case SR_CONF_TIMEBASE:
108 bind_enum(name, key, gvar_list,
109 get, set, print_timebase);
113 bind_enum(name, key, gvar_list, get, set, print_vdiv);
116 case SR_CONF_VOLTAGE_THRESHOLD:
117 bind_enum(name, key, gvar_list,
118 get, set, print_voltage_threshold);
123 g_variant_unref(gvar_list);
125 g_variant_unref(gvar_opts);
128 void DeviceOptions::bind_bool(const QString &name,
129 Property::Getter getter, Property::Setter setter)
132 _properties.push_back(shared_ptr<Property>(new Bool(
133 name, getter, setter)));
136 void DeviceOptions::bind_enum(const QString &name, int key,
137 GVariant *const gvar_list, Property::Getter getter,
138 Property::Setter setter, function<QString (GVariant*)> printer)
142 vector< pair<GVariant*, QString> > values;
146 qDebug() << "Config key " << key << " was listed, but no "
147 "options were given";
151 g_variant_iter_init (&iter, gvar_list);
152 while ((gvar = g_variant_iter_next_value (&iter)))
153 values.push_back(make_pair(gvar, printer(gvar)));
155 _properties.push_back(shared_ptr<Property>(new Enum(name, values,
159 void DeviceOptions::bind_int(const QString &name, QString suffix,
160 optional< std::pair<int64_t, int64_t> > range,
161 Property::Getter getter, Property::Setter setter)
165 _properties.push_back(shared_ptr<Property>(new Int(name, suffix, range,
169 QString DeviceOptions::print_timebase(GVariant *const gvar)
172 g_variant_get(gvar, "(tt)", &p, &q);
173 return QString::fromUtf8(sr_period_string(p * q));
176 QString DeviceOptions::print_vdiv(GVariant *const gvar)
179 g_variant_get(gvar, "(tt)", &p, &q);
180 return QString::fromUtf8(sr_voltage_string(p, q));
183 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
186 g_variant_get(gvar, "(dd)", &lo, &hi);
187 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);