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>
34 using namespace boost;
41 DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) :
44 GVariant *gvar_opts, *gvar_list;
47 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS,
48 &gvar_opts, sdi) != SR_OK))
49 /* Driver supports no device instance options. */
52 const int *const options = (const int32_t *)g_variant_get_fixed_array(
53 gvar_opts, &num_opts, sizeof(int32_t));
54 for (unsigned int i = 0; i < num_opts; i++) {
55 const struct sr_config_info *const info =
56 sr_config_info_get(options[i]);
61 const int key = info->key;
63 if(sr_config_list(_sdi->driver, key, &gvar_list, _sdi) != SR_OK)
66 const QString name(info->name);
70 case SR_CONF_SAMPLERATE:
71 bind_samplerate(name, gvar_list);
74 case SR_CONF_PATTERN_MODE:
75 case SR_CONF_BUFFERSIZE:
76 case SR_CONF_TRIGGER_SOURCE:
78 case SR_CONF_COUPLING:
79 bind_enum(name, key, gvar_list);
86 case SR_CONF_TIMEBASE:
87 bind_enum(name, key, gvar_list, print_timebase);
91 bind_enum(name, key, gvar_list, print_vdiv);
96 g_variant_unref(gvar_list);
98 g_variant_unref(gvar_opts);
101 GVariant* DeviceOptions::config_getter(
102 const struct sr_dev_inst *sdi, int key)
104 GVariant *data = NULL;
105 if (sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
107 "WARNING: Failed to get value of config id" << key;
113 void DeviceOptions::config_setter(
114 const struct sr_dev_inst *sdi, int key, GVariant* value)
116 if (sr_config_set(sdi, key, value) != SR_OK)
117 qDebug() << "WARNING: Failed to set value of sample rate";
120 void DeviceOptions::bind_bool(const QString &name, int key)
122 _properties.push_back(shared_ptr<Property>(
123 new Bool(name, bind(config_getter, _sdi, key),
124 bind(config_setter, _sdi, key, _1))));
127 void DeviceOptions::bind_enum(const QString &name, int key,
128 GVariant *const gvar_list, function<QString (GVariant*)> printer)
132 vector< pair<GVariant*, QString> > values;
136 g_variant_iter_init (&iter, gvar_list);
137 while ((gvar = g_variant_iter_next_value (&iter)))
138 values.push_back(make_pair(gvar, printer(gvar)));
140 _properties.push_back(shared_ptr<Property>(
141 new Enum(name, values,
142 bind(config_getter, _sdi, key),
143 bind(config_setter, _sdi, key, _1))));
146 QString DeviceOptions::print_gvariant(GVariant *const gvar)
150 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
151 s = QString(g_variant_get_string(gvar, NULL));
154 gchar *const text = g_variant_print(gvar, FALSE);
162 void DeviceOptions::bind_samplerate(const QString &name,
163 GVariant *const gvar_list)
165 GVariant *gvar_list_samplerates;
169 if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
170 "samplerate-steps", G_VARIANT_TYPE("at"))))
173 const uint64_t *const elements =
174 (const uint64_t *)g_variant_get_fixed_array(
175 gvar_list_samplerates, &num_elements, sizeof(uint64_t));
177 assert(num_elements == 3);
179 _properties.push_back(shared_ptr<Property>(
180 new Double(name, 0, QObject::tr("Hz"),
181 make_pair((double)elements[0], (double)elements[1]),
183 bind(samplerate_double_getter, _sdi),
184 bind(samplerate_double_setter, _sdi, _1))));
186 g_variant_unref(gvar_list_samplerates);
188 else if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
189 "samplerates", G_VARIANT_TYPE("at"))))
191 bind_enum(name, SR_CONF_SAMPLERATE,
192 gvar_list_samplerates, print_samplerate);
193 g_variant_unref(gvar_list_samplerates);
197 QString DeviceOptions::print_samplerate(GVariant *const gvar)
199 char *const s = sr_samplerate_string(
200 g_variant_get_uint64(gvar));
201 const QString qstring(s);
206 GVariant* DeviceOptions::samplerate_double_getter(
207 const struct sr_dev_inst *sdi)
209 GVariant *const gvar = config_getter(sdi, SR_CONF_SAMPLERATE);
214 GVariant *const gvar_double = g_variant_new_double(
215 g_variant_get_uint64(gvar));
217 g_variant_unref(gvar);
222 void DeviceOptions::samplerate_double_setter(
223 struct sr_dev_inst *sdi, GVariant *value)
225 GVariant *const gvar = g_variant_new_uint64(
226 g_variant_get_double(value));
227 config_setter(sdi, SR_CONF_SAMPLERATE, gvar);
230 QString DeviceOptions::print_timebase(GVariant *const gvar)
233 g_variant_get(gvar, "(tt)", &p, &q);
234 return QString(sr_period_string(p * q));
237 QString DeviceOptions::print_vdiv(GVariant *const gvar)
240 g_variant_get(gvar, "(tt)", &p, &q);
241 return QString(sr_voltage_string(p, q));