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