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