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