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
21 #include <boost/bind.hpp>
28 #include "deviceoptions.h"
30 #include <pv/prop/bool.h>
31 #include <pv/prop/double.h>
32 #include <pv/prop/enum.h>
33 #include <pv/prop/int.h>
36 using boost::function;
37 using boost::optional;
38 using boost::shared_ptr;
48 DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) :
53 GVariant *gvar_opts, *gvar_list;
56 if ((sr_config_list(sdi->driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS,
57 &gvar_opts) != SR_OK))
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;
72 if (sr_config_list(_sdi->driver, _sdi, NULL, key, &gvar_list) != SR_OK)
75 const QString name = QString::fromUtf8(info->name);
79 case SR_CONF_SAMPLERATE:
80 // Sample rate values are not bound because they are shown
84 case SR_CONF_CAPTURE_RATIO:
85 bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100));
88 case SR_CONF_PATTERN_MODE:
89 case SR_CONF_BUFFERSIZE:
90 case SR_CONF_TRIGGER_SOURCE:
92 case SR_CONF_COUPLING:
93 bind_enum(name, key, gvar_list);
100 case SR_CONF_TIMEBASE:
101 bind_enum(name, key, gvar_list, print_timebase);
105 bind_enum(name, key, gvar_list, print_vdiv);
108 case SR_CONF_VOLTAGE_THRESHOLD:
109 bind_enum(name, key, gvar_list, print_voltage_threshold);
114 g_variant_unref(gvar_list);
116 g_variant_unref(gvar_opts);
119 GVariant* DeviceOptions::config_getter(
120 const struct sr_dev_inst *sdi, int key)
122 GVariant *data = NULL;
123 if (sr_config_get(sdi->driver, sdi, NULL, key, &data) != SR_OK) {
125 "WARNING: Failed to get value of config id" << key;
131 void DeviceOptions::config_setter(
132 const struct sr_dev_inst *sdi, int key, GVariant* value)
134 if (sr_config_set(sdi, NULL, key, value) != SR_OK)
135 qDebug() << "WARNING: Failed to set value of sample rate";
138 void DeviceOptions::bind_bool(const QString &name, int key)
140 _properties.push_back(shared_ptr<Property>(
141 new Bool(name, bind(config_getter, _sdi, key),
142 bind(config_setter, _sdi, key, _1))));
145 void DeviceOptions::bind_enum(const QString &name, int key,
146 GVariant *const gvar_list, function<QString (GVariant*)> printer)
150 vector< pair<GVariant*, QString> > values;
154 g_variant_iter_init (&iter, gvar_list);
155 while ((gvar = g_variant_iter_next_value (&iter)))
156 values.push_back(make_pair(gvar, printer(gvar)));
158 _properties.push_back(shared_ptr<Property>(
159 new Enum(name, values,
160 bind(config_getter, _sdi, key),
161 bind(config_setter, _sdi, key, _1))));
164 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
165 optional< std::pair<int64_t, int64_t> > range)
167 _properties.push_back(shared_ptr<Property>(
168 new Int(name, suffix, range,
169 bind(config_getter, _sdi, key),
170 bind(config_setter, _sdi, key, _1))));
173 QString DeviceOptions::print_gvariant(GVariant *const gvar)
177 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
178 s = QString::fromUtf8(g_variant_get_string(gvar, NULL));
181 gchar *const text = g_variant_print(gvar, FALSE);
182 s = QString::fromUtf8(text);
189 QString DeviceOptions::print_timebase(GVariant *const gvar)
192 g_variant_get(gvar, "(tt)", &p, &q);
193 return QString::fromUtf8(sr_period_string(p * q));
196 QString DeviceOptions::print_vdiv(GVariant *const gvar)
199 g_variant_get(gvar, "(tt)", &p, &q);
200 return QString::fromUtf8(sr_voltage_string(p, q));
203 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
207 g_variant_get(gvar, "(dd)", &lo, &hi);
208 snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);
209 return QString::fromUtf8(buf);