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) :
47 GVariant *gvar_opts, *gvar_list;
50 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS,
51 &gvar_opts, sdi) != SR_OK))
52 /* Driver supports no device instance options. */
55 const int *const options = (const int32_t *)g_variant_get_fixed_array(
56 gvar_opts, &num_opts, sizeof(int32_t));
57 for (unsigned int i = 0; i < num_opts; i++) {
58 const struct sr_config_info *const info =
59 sr_config_info_get(options[i]);
64 const int key = info->key;
66 if(sr_config_list(_sdi->driver, key, &gvar_list, _sdi) != SR_OK)
69 const QString name(info->name);
73 case SR_CONF_SAMPLERATE:
74 bind_samplerate(name, gvar_list);
77 case SR_CONF_CAPTURE_RATIO:
78 bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100));
81 case SR_CONF_PATTERN_MODE:
82 case SR_CONF_BUFFERSIZE:
83 case SR_CONF_TRIGGER_SOURCE:
85 case SR_CONF_COUPLING:
86 bind_enum(name, key, gvar_list);
93 case SR_CONF_TIMEBASE:
94 bind_enum(name, key, gvar_list, print_timebase);
98 bind_enum(name, key, gvar_list, print_vdiv);
101 case SR_CONF_VOLTAGE_THRESHOLD:
102 bind_enum(name, key, gvar_list, print_voltage_threshold);
107 g_variant_unref(gvar_list);
109 g_variant_unref(gvar_opts);
112 GVariant* DeviceOptions::config_getter(
113 const struct sr_dev_inst *sdi, int key)
115 GVariant *data = NULL;
116 if (sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
118 "WARNING: Failed to get value of config id" << key;
124 void DeviceOptions::config_setter(
125 const struct sr_dev_inst *sdi, int key, GVariant* value)
127 if (sr_config_set(sdi, key, value) != SR_OK)
128 qDebug() << "WARNING: Failed to set value of sample rate";
131 void DeviceOptions::bind_bool(const QString &name, int key)
133 _properties.push_back(shared_ptr<Property>(
134 new Bool(name, bind(config_getter, _sdi, key),
135 bind(config_setter, _sdi, key, _1))));
138 void DeviceOptions::bind_enum(const QString &name, int key,
139 GVariant *const gvar_list, function<QString (GVariant*)> printer)
143 vector< pair<GVariant*, QString> > values;
147 g_variant_iter_init (&iter, gvar_list);
148 while ((gvar = g_variant_iter_next_value (&iter)))
149 values.push_back(make_pair(gvar, printer(gvar)));
151 _properties.push_back(shared_ptr<Property>(
152 new Enum(name, values,
153 bind(config_getter, _sdi, key),
154 bind(config_setter, _sdi, key, _1))));
157 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
158 optional< std::pair<int64_t, int64_t> > range)
160 _properties.push_back(shared_ptr<Property>(
161 new Int(name, suffix, range,
162 bind(config_getter, _sdi, key),
163 bind(config_setter, _sdi, key, _1))));
166 QString DeviceOptions::print_gvariant(GVariant *const gvar)
170 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
171 s = QString(g_variant_get_string(gvar, NULL));
174 gchar *const text = g_variant_print(gvar, FALSE);
182 void DeviceOptions::bind_samplerate(const QString &name,
183 GVariant *const gvar_list)
185 GVariant *gvar_list_samplerates;
189 if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
190 "samplerate-steps", G_VARIANT_TYPE("at"))))
193 const uint64_t *const elements =
194 (const uint64_t *)g_variant_get_fixed_array(
195 gvar_list_samplerates, &num_elements, sizeof(uint64_t));
197 assert(num_elements == 3);
199 _properties.push_back(shared_ptr<Property>(
200 new Double(name, 0, QObject::tr("Hz"),
201 make_pair((double)elements[0], (double)elements[1]),
203 bind(samplerate_double_getter, _sdi),
204 bind(samplerate_double_setter, _sdi, _1))));
206 g_variant_unref(gvar_list_samplerates);
208 else if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
209 "samplerates", G_VARIANT_TYPE("at"))))
211 bind_enum(name, SR_CONF_SAMPLERATE,
212 gvar_list_samplerates, print_samplerate);
213 g_variant_unref(gvar_list_samplerates);
217 QString DeviceOptions::print_samplerate(GVariant *const gvar)
219 char *const s = sr_samplerate_string(
220 g_variant_get_uint64(gvar));
221 const QString qstring(s);
226 GVariant* DeviceOptions::samplerate_double_getter(
227 const struct sr_dev_inst *sdi)
229 GVariant *const gvar = config_getter(sdi, SR_CONF_SAMPLERATE);
234 GVariant *const gvar_double = g_variant_new_double(
235 g_variant_get_uint64(gvar));
237 g_variant_unref(gvar);
242 void DeviceOptions::samplerate_double_setter(
243 struct sr_dev_inst *sdi, GVariant *value)
245 GVariant *const gvar = g_variant_new_uint64(
246 g_variant_get_double(value));
247 config_setter(sdi, SR_CONF_SAMPLERATE, gvar);
250 QString DeviceOptions::print_timebase(GVariant *const gvar)
253 g_variant_get(gvar, "(tt)", &p, &q);
254 return QString(sr_period_string(p * q));
257 QString DeviceOptions::print_vdiv(GVariant *const gvar)
260 g_variant_get(gvar, "(tt)", &p, &q);
261 return QString(sr_voltage_string(p, q));
264 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
268 g_variant_get(gvar, "(dd)", &lo, &hi);
269 snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);