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