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, "%", pair<int64_t, int64_t>(0, 100));
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);
99 case SR_CONF_VOLTAGE_THRESHOLD:
100 bind_enum(name, key, gvar_list, print_voltage_threshold);
105 g_variant_unref(gvar_list);
107 g_variant_unref(gvar_opts);
110 GVariant* DeviceOptions::config_getter(
111 const struct sr_dev_inst *sdi, int key)
113 GVariant *data = NULL;
114 if (sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
116 "WARNING: Failed to get value of config id" << key;
122 void DeviceOptions::config_setter(
123 const struct sr_dev_inst *sdi, int key, GVariant* value)
125 if (sr_config_set(sdi, key, value) != SR_OK)
126 qDebug() << "WARNING: Failed to set value of sample rate";
129 void DeviceOptions::bind_bool(const QString &name, int key)
131 _properties.push_back(shared_ptr<Property>(
132 new Bool(name, bind(config_getter, _sdi, key),
133 bind(config_setter, _sdi, key, _1))));
136 void DeviceOptions::bind_enum(const QString &name, int key,
137 GVariant *const gvar_list, function<QString (GVariant*)> printer)
141 vector< pair<GVariant*, QString> > values;
145 g_variant_iter_init (&iter, gvar_list);
146 while ((gvar = g_variant_iter_next_value (&iter)))
147 values.push_back(make_pair(gvar, printer(gvar)));
149 _properties.push_back(shared_ptr<Property>(
150 new Enum(name, values,
151 bind(config_getter, _sdi, key),
152 bind(config_setter, _sdi, key, _1))));
155 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
156 optional< std::pair<int64_t, int64_t> > range)
158 _properties.push_back(shared_ptr<Property>(
159 new Int(name, suffix, range,
160 bind(config_getter, _sdi, key),
161 bind(config_setter, _sdi, key, _1))));
164 QString DeviceOptions::print_gvariant(GVariant *const gvar)
168 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
169 s = QString(g_variant_get_string(gvar, NULL));
172 gchar *const text = g_variant_print(gvar, FALSE);
180 void DeviceOptions::bind_samplerate(const QString &name,
181 GVariant *const gvar_list)
183 GVariant *gvar_list_samplerates;
187 if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
188 "samplerate-steps", G_VARIANT_TYPE("at"))))
191 const uint64_t *const elements =
192 (const uint64_t *)g_variant_get_fixed_array(
193 gvar_list_samplerates, &num_elements, sizeof(uint64_t));
195 assert(num_elements == 3);
197 _properties.push_back(shared_ptr<Property>(
198 new Double(name, 0, QObject::tr("Hz"),
199 make_pair((double)elements[0], (double)elements[1]),
201 bind(samplerate_double_getter, _sdi),
202 bind(samplerate_double_setter, _sdi, _1))));
204 g_variant_unref(gvar_list_samplerates);
206 else if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
207 "samplerates", G_VARIANT_TYPE("at"))))
209 bind_enum(name, SR_CONF_SAMPLERATE,
210 gvar_list_samplerates, print_samplerate);
211 g_variant_unref(gvar_list_samplerates);
215 QString DeviceOptions::print_samplerate(GVariant *const gvar)
217 char *const s = sr_samplerate_string(
218 g_variant_get_uint64(gvar));
219 const QString qstring(s);
224 GVariant* DeviceOptions::samplerate_double_getter(
225 const struct sr_dev_inst *sdi)
227 GVariant *const gvar = config_getter(sdi, SR_CONF_SAMPLERATE);
232 GVariant *const gvar_double = g_variant_new_double(
233 g_variant_get_uint64(gvar));
235 g_variant_unref(gvar);
240 void DeviceOptions::samplerate_double_setter(
241 struct sr_dev_inst *sdi, GVariant *value)
243 GVariant *const gvar = g_variant_new_uint64(
244 g_variant_get_double(value));
245 config_setter(sdi, SR_CONF_SAMPLERATE, gvar);
248 QString DeviceOptions::print_timebase(GVariant *const gvar)
251 g_variant_get(gvar, "(tt)", &p, &q);
252 return QString(sr_period_string(p * q));
255 QString DeviceOptions::print_vdiv(GVariant *const gvar)
258 g_variant_get(gvar, "(tt)", &p, &q);
259 return QString(sr_voltage_string(p, q));
262 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
266 g_variant_get(gvar, "(dd)", &lo, &hi);
267 snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);