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>
35 using namespace boost;
42 DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) :
45 GVariant *gvar_opts, *gvar_list;
48 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS,
49 &gvar_opts, sdi) != SR_OK))
50 /* Driver supports no device instance options. */
53 const int *const options = (const int32_t *)g_variant_get_fixed_array(
54 gvar_opts, &num_opts, sizeof(int32_t));
55 for (unsigned int i = 0; i < num_opts; i++) {
56 const struct sr_config_info *const info =
57 sr_config_info_get(options[i]);
62 const int key = info->key;
64 if(sr_config_list(_sdi->driver, key, &gvar_list, _sdi) != SR_OK)
67 const QString name(info->name);
71 case SR_CONF_SAMPLERATE:
72 bind_samplerate(name, gvar_list);
75 case SR_CONF_CAPTURE_RATIO:
76 bind_int(name, key, "%", make_pair(0L, 100L));
79 case SR_CONF_PATTERN_MODE:
80 case SR_CONF_BUFFERSIZE:
81 case SR_CONF_TRIGGER_SOURCE:
83 case SR_CONF_COUPLING:
84 bind_enum(name, key, gvar_list);
91 case SR_CONF_TIMEBASE:
92 bind_enum(name, key, gvar_list, print_timebase);
96 bind_enum(name, key, gvar_list, print_vdiv);
101 g_variant_unref(gvar_list);
103 g_variant_unref(gvar_opts);
106 GVariant* DeviceOptions::config_getter(
107 const struct sr_dev_inst *sdi, int key)
109 GVariant *data = NULL;
110 if (sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
112 "WARNING: Failed to get value of config id" << key;
118 void DeviceOptions::config_setter(
119 const struct sr_dev_inst *sdi, int key, GVariant* value)
121 if (sr_config_set(sdi, key, value) != SR_OK)
122 qDebug() << "WARNING: Failed to set value of sample rate";
125 void DeviceOptions::bind_bool(const QString &name, int key)
127 _properties.push_back(shared_ptr<Property>(
128 new Bool(name, bind(config_getter, _sdi, key),
129 bind(config_setter, _sdi, key, _1))));
132 void DeviceOptions::bind_enum(const QString &name, int key,
133 GVariant *const gvar_list, function<QString (GVariant*)> printer)
137 vector< pair<GVariant*, QString> > values;
141 g_variant_iter_init (&iter, gvar_list);
142 while ((gvar = g_variant_iter_next_value (&iter)))
143 values.push_back(make_pair(gvar, printer(gvar)));
145 _properties.push_back(shared_ptr<Property>(
146 new Enum(name, values,
147 bind(config_getter, _sdi, key),
148 bind(config_setter, _sdi, key, _1))));
151 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
152 optional< std::pair<int64_t, int64_t> > range)
154 _properties.push_back(shared_ptr<Property>(
155 new Int(name, suffix, range,
156 bind(config_getter, _sdi, key),
157 bind(config_setter, _sdi, key, _1))));
160 QString DeviceOptions::print_gvariant(GVariant *const gvar)
164 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
165 s = QString(g_variant_get_string(gvar, NULL));
168 gchar *const text = g_variant_print(gvar, FALSE);
176 void DeviceOptions::bind_samplerate(const QString &name,
177 GVariant *const gvar_list)
179 GVariant *gvar_list_samplerates;
183 if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
184 "samplerate-steps", G_VARIANT_TYPE("at"))))
187 const uint64_t *const elements =
188 (const uint64_t *)g_variant_get_fixed_array(
189 gvar_list_samplerates, &num_elements, sizeof(uint64_t));
191 assert(num_elements == 3);
193 _properties.push_back(shared_ptr<Property>(
194 new Double(name, 0, QObject::tr("Hz"),
195 make_pair((double)elements[0], (double)elements[1]),
197 bind(samplerate_double_getter, _sdi),
198 bind(samplerate_double_setter, _sdi, _1))));
200 g_variant_unref(gvar_list_samplerates);
202 else if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
203 "samplerates", G_VARIANT_TYPE("at"))))
205 bind_enum(name, SR_CONF_SAMPLERATE,
206 gvar_list_samplerates, print_samplerate);
207 g_variant_unref(gvar_list_samplerates);
211 QString DeviceOptions::print_samplerate(GVariant *const gvar)
213 char *const s = sr_samplerate_string(
214 g_variant_get_uint64(gvar));
215 const QString qstring(s);
220 GVariant* DeviceOptions::samplerate_double_getter(
221 const struct sr_dev_inst *sdi)
223 GVariant *const gvar = config_getter(sdi, SR_CONF_SAMPLERATE);
228 GVariant *const gvar_double = g_variant_new_double(
229 g_variant_get_uint64(gvar));
231 g_variant_unref(gvar);
236 void DeviceOptions::samplerate_double_setter(
237 struct sr_dev_inst *sdi, GVariant *value)
239 GVariant *const gvar = g_variant_new_uint64(
240 g_variant_get_double(value));
241 config_setter(sdi, SR_CONF_SAMPLERATE, gvar);
244 QString DeviceOptions::print_timebase(GVariant *const gvar)
247 g_variant_get(gvar, "(tt)", &p, &q);
248 return QString(sr_period_string(p * q));
251 QString DeviceOptions::print_vdiv(GVariant *const gvar)
254 g_variant_get(gvar, "(tt)", &p, &q);
255 return QString(sr_voltage_string(p, q));