]> sigrok.org Git - pulseview.git/blame_incremental - pv/prop/binding/deviceoptions.cpp
Removed an uneeded include
[pulseview.git] / pv / prop / binding / deviceoptions.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <boost/bind.hpp>
22
23#include <QDebug>
24#include <QObject>
25
26#include <stdint.h>
27
28#include "deviceoptions.h"
29
30#include <pv/prop/bool.h>
31#include <pv/prop/double.h>
32#include <pv/prop/enum.h>
33
34using namespace boost;
35using namespace std;
36
37namespace pv {
38namespace prop {
39namespace binding {
40
41DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) :
42 _sdi(sdi)
43{
44 GVariant *gvar_opts, *gvar_list;
45 gsize num_opts;
46
47 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS,
48 &gvar_opts, sdi) != SR_OK))
49 /* Driver supports no device instance options. */
50 return;
51
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]);
57
58 if (!info)
59 continue;
60
61 const int key = info->key;
62
63 if(sr_config_list(_sdi->driver, key, &gvar_list, _sdi) != SR_OK)
64 gvar_list = NULL;
65
66 const QString name(info->name);
67
68 switch(key)
69 {
70 case SR_CONF_SAMPLERATE:
71 bind_samplerate(name, gvar_list);
72 break;
73
74 case SR_CONF_PATTERN_MODE:
75 case SR_CONF_BUFFERSIZE:
76 case SR_CONF_TRIGGER_SOURCE:
77 case SR_CONF_FILTER:
78 case SR_CONF_COUPLING:
79 bind_enum(name, key, gvar_list);
80 break;
81
82 case SR_CONF_RLE:
83 bind_bool(name, key);
84 break;
85
86 case SR_CONF_TIMEBASE:
87 bind_enum(name, key, gvar_list, print_timebase);
88 break;
89
90 case SR_CONF_VDIV:
91 bind_enum(name, key, gvar_list, print_vdiv);
92 break;
93 }
94
95 if (gvar_list)
96 g_variant_unref(gvar_list);
97 }
98 g_variant_unref(gvar_opts);
99}
100
101GVariant* DeviceOptions::config_getter(
102 const struct sr_dev_inst *sdi, int key)
103{
104 GVariant *data = NULL;
105 if (sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
106 qDebug() <<
107 "WARNING: Failed to get value of config id" << key;
108 return NULL;
109 }
110 return data;
111}
112
113void DeviceOptions::config_setter(
114 const struct sr_dev_inst *sdi, int key, GVariant* value)
115{
116 if (sr_config_set(sdi, key, value) != SR_OK)
117 qDebug() << "WARNING: Failed to set value of sample rate";
118}
119
120void DeviceOptions::bind_bool(const QString &name, int key)
121{
122 _properties.push_back(shared_ptr<Property>(
123 new Bool(name, bind(config_getter, _sdi, key),
124 bind(config_setter, _sdi, key, _1))));
125}
126
127void DeviceOptions::bind_enum(const QString &name, int key,
128 GVariant *const gvar_list, function<QString (GVariant*)> printer)
129{
130 GVariant *gvar;
131 GVariantIter iter;
132 vector< pair<GVariant*, QString> > values;
133
134 assert(gvar_list);
135
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)));
139
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))));
144}
145
146QString DeviceOptions::print_gvariant(GVariant *const gvar)
147{
148 QString s;
149
150 if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
151 s = QString(g_variant_get_string(gvar, NULL));
152 else
153 {
154 gchar *const text = g_variant_print(gvar, FALSE);
155 s = QString(text);
156 g_free(text);
157 }
158
159 return s;
160}
161
162void DeviceOptions::bind_samplerate(const QString &name,
163 GVariant *const gvar_list)
164{
165 GVariant *gvar_list_samplerates;
166
167 assert(gvar_list);
168
169 if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
170 "samplerate-steps", G_VARIANT_TYPE("at"))))
171 {
172 gsize num_elements;
173 const uint64_t *const elements =
174 (const uint64_t *)g_variant_get_fixed_array(
175 gvar_list_samplerates, &num_elements, sizeof(uint64_t));
176
177 assert(num_elements == 3);
178
179 _properties.push_back(shared_ptr<Property>(
180 new Double(name, 0, QObject::tr("Hz"),
181 make_pair((double)elements[0], (double)elements[1]),
182 (double)elements[2],
183 bind(samplerate_double_getter, _sdi),
184 bind(samplerate_double_setter, _sdi, _1))));
185
186 g_variant_unref(gvar_list_samplerates);
187 }
188 else if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
189 "samplerates", G_VARIANT_TYPE("at"))))
190 {
191 bind_enum(name, SR_CONF_SAMPLERATE,
192 gvar_list_samplerates, print_samplerate);
193 g_variant_unref(gvar_list_samplerates);
194 }
195}
196
197QString DeviceOptions::print_samplerate(GVariant *const gvar)
198{
199 char *const s = sr_samplerate_string(
200 g_variant_get_uint64(gvar));
201 const QString qstring(s);
202 g_free(s);
203 return qstring;
204}
205
206GVariant* DeviceOptions::samplerate_double_getter(
207 const struct sr_dev_inst *sdi)
208{
209 GVariant *const gvar = config_getter(sdi, SR_CONF_SAMPLERATE);
210
211 if(!gvar)
212 return NULL;
213
214 GVariant *const gvar_double = g_variant_new_double(
215 g_variant_get_uint64(gvar));
216
217 g_variant_unref(gvar);
218
219 return gvar_double;
220}
221
222void DeviceOptions::samplerate_double_setter(
223 struct sr_dev_inst *sdi, GVariant *value)
224{
225 GVariant *const gvar = g_variant_new_uint64(
226 g_variant_get_double(value));
227 config_setter(sdi, SR_CONF_SAMPLERATE, gvar);
228}
229
230QString DeviceOptions::print_timebase(GVariant *const gvar)
231{
232 uint64_t p, q;
233 g_variant_get(gvar, "(tt)", &p, &q);
234 return QString(sr_period_string(p * q));
235}
236
237QString DeviceOptions::print_vdiv(GVariant *const gvar)
238{
239 uint64_t p, q;
240 g_variant_get(gvar, "(tt)", &p, &q);
241 return QString(sr_voltage_string(p, q));
242}
243
244} // binding
245} // prop
246} // pv
247