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(const sr_dev_inst *sdi,
49 const sr_probe_group *group) :
55 GVariant *gvar_opts, *gvar_list;
58 if ((sr_config_list(sdi->driver, sdi, group, SR_CONF_DEVICE_OPTIONS,
59 &gvar_opts) != SR_OK))
60 /* Driver supports no device instance options. */
63 const int *const options = (const int32_t *)g_variant_get_fixed_array(
64 gvar_opts, &num_opts, sizeof(int32_t));
65 for (unsigned int i = 0; i < num_opts; i++) {
66 const struct sr_config_info *const info =
67 sr_config_info_get(options[i]);
72 const int key = info->key;
74 if (sr_config_list(_sdi->driver, _sdi, group,
75 key, &gvar_list) != SR_OK)
78 const QString name = QString::fromUtf8(info->name);
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, key, "%", pair<int64_t, int64_t>(0, 100));
91 case SR_CONF_PATTERN_MODE:
92 case SR_CONF_BUFFERSIZE:
93 case SR_CONF_TRIGGER_SOURCE:
95 case SR_CONF_COUPLING:
96 bind_enum(name, key, gvar_list);
100 bind_bool(name, key);
103 case SR_CONF_TIMEBASE:
104 bind_enum(name, key, gvar_list, print_timebase);
108 bind_enum(name, key, gvar_list, print_vdiv);
111 case SR_CONF_VOLTAGE_THRESHOLD:
112 bind_enum(name, key, gvar_list, print_voltage_threshold);
117 g_variant_unref(gvar_list);
119 g_variant_unref(gvar_opts);
122 GVariant* DeviceOptions::config_getter(
123 const sr_dev_inst *sdi, const sr_probe_group *group, int key)
125 GVariant *data = NULL;
126 if (sr_config_get(sdi->driver, sdi, group, key, &data) != SR_OK) {
128 "WARNING: Failed to get value of config id" << key;
134 void DeviceOptions::config_setter(
135 const struct sr_dev_inst *sdi, const sr_probe_group *group, int key,
138 if (sr_config_set(sdi, group, key, value) != SR_OK)
139 qDebug() << "WARNING: Failed to set value of sample rate";
142 void DeviceOptions::bind_bool(const QString &name, int key)
144 _properties.push_back(shared_ptr<Property>(
145 new Bool(name, bind(config_getter, _sdi, _group, key),
146 bind(config_setter, _sdi, _group, key, _1))));
149 void DeviceOptions::bind_enum(const QString &name, int key,
150 GVariant *const gvar_list, function<QString (GVariant*)> printer)
154 vector< pair<GVariant*, QString> > values;
158 g_variant_iter_init (&iter, gvar_list);
159 while ((gvar = g_variant_iter_next_value (&iter)))
160 values.push_back(make_pair(gvar, printer(gvar)));
162 _properties.push_back(shared_ptr<Property>(
163 new Enum(name, values,
164 bind(config_getter, _sdi, _group, key),
165 bind(config_setter, _sdi, _group, key, _1))));
168 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
169 optional< std::pair<int64_t, int64_t> > range)
171 _properties.push_back(shared_ptr<Property>(
172 new Int(name, suffix, range,
173 bind(config_getter, _sdi, _group, key),
174 bind(config_setter, _sdi, _group, key, _1))));
177 QString DeviceOptions::print_gvariant(GVariant *const gvar)
181 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
182 s = QString::fromUtf8(g_variant_get_string(gvar, NULL));
185 gchar *const text = g_variant_print(gvar, FALSE);
186 s = QString::fromUtf8(text);
193 QString DeviceOptions::print_timebase(GVariant *const gvar)
196 g_variant_get(gvar, "(tt)", &p, &q);
197 return QString::fromUtf8(sr_period_string(p * q));
200 QString DeviceOptions::print_vdiv(GVariant *const gvar)
203 g_variant_get(gvar, "(tt)", &p, &q);
204 return QString::fromUtf8(sr_voltage_string(p, q));
207 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
211 g_variant_get(gvar, "(dd)", &lo, &hi);
212 snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);
213 return QString::fromUtf8(buf);