]> sigrok.org Git - pulseview.git/blob - pv/prop/binding/deviceoptions.cpp
e8ae002bae8ba0c0339b2da1fb5f9861d71fb5c3
[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                         // Sample rate values are not bound because they are shown
75                         // in the SamplingBar
76                         break;
77
78                 case SR_CONF_CAPTURE_RATIO:
79                         bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100));
80                         break;
81
82                 case SR_CONF_PATTERN_MODE:
83                 case SR_CONF_BUFFERSIZE:
84                 case SR_CONF_TRIGGER_SOURCE:
85                 case SR_CONF_FILTER:
86                 case SR_CONF_COUPLING:
87                         bind_enum(name, key, gvar_list);
88                         break;
89
90                 case SR_CONF_RLE:
91                         bind_bool(name, key);
92                         break;
93
94                 case SR_CONF_TIMEBASE:
95                         bind_enum(name, key, gvar_list, print_timebase);
96                         break;
97
98                 case SR_CONF_VDIV:
99                         bind_enum(name, key, gvar_list, print_vdiv);
100                         break;
101
102                 case SR_CONF_VOLTAGE_THRESHOLD:
103                         bind_enum(name, key, gvar_list, print_voltage_threshold);
104                         break;
105                 }
106
107                 if (gvar_list)
108                         g_variant_unref(gvar_list);
109         }
110         g_variant_unref(gvar_opts);
111 }
112
113 GVariant* DeviceOptions::config_getter(
114         const struct sr_dev_inst *sdi, int key)
115 {
116         GVariant *data = NULL;
117         if (sr_config_get(sdi->driver, sdi, NULL, key, &data) != SR_OK) {
118                 qDebug() <<
119                         "WARNING: Failed to get value of config id" << key;
120                 return NULL;
121         }
122         return data;
123 }
124
125 void DeviceOptions::config_setter(
126         const struct sr_dev_inst *sdi, int key, GVariant* value)
127 {
128         if (sr_config_set(sdi, NULL, key, value) != SR_OK)
129                 qDebug() << "WARNING: Failed to set value of sample rate";
130 }
131
132 void DeviceOptions::bind_bool(const QString &name, int key)
133 {
134         _properties.push_back(shared_ptr<Property>(
135                 new Bool(name, bind(config_getter, _sdi, key),
136                         bind(config_setter, _sdi, key, _1))));
137 }
138
139 void DeviceOptions::bind_enum(const QString &name, int key,
140         GVariant *const gvar_list, function<QString (GVariant*)> printer)
141 {
142         GVariant *gvar;
143         GVariantIter iter;
144         vector< pair<GVariant*, QString> > values;
145
146         assert(gvar_list);
147
148         g_variant_iter_init (&iter, gvar_list);
149         while ((gvar = g_variant_iter_next_value (&iter)))
150                 values.push_back(make_pair(gvar, printer(gvar)));
151
152         _properties.push_back(shared_ptr<Property>(
153                 new Enum(name, values,
154                         bind(config_getter, _sdi, key),
155                         bind(config_setter, _sdi, key, _1))));
156 }
157
158 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
159         optional< std::pair<int64_t, int64_t> > range)
160 {
161         _properties.push_back(shared_ptr<Property>(
162                 new Int(name, suffix, range,
163                         bind(config_getter, _sdi, key),
164                         bind(config_setter, _sdi, key, _1))));
165 }
166
167 QString DeviceOptions::print_gvariant(GVariant *const gvar)
168 {
169         QString s;
170
171         if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
172                 s = QString(g_variant_get_string(gvar, NULL));
173         else
174         {
175                 gchar *const text = g_variant_print(gvar, FALSE);
176                 s = QString(text);
177                 g_free(text);
178         }
179
180         return s;
181 }
182
183 QString DeviceOptions::print_timebase(GVariant *const gvar)
184 {
185         uint64_t p, q;
186         g_variant_get(gvar, "(tt)", &p, &q);
187         return QString(sr_period_string(p * q));
188 }
189
190 QString DeviceOptions::print_vdiv(GVariant *const gvar)
191 {
192         uint64_t p, q;
193         g_variant_get(gvar, "(tt)", &p, &q);
194         return QString(sr_voltage_string(p, q));
195 }
196
197 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
198 {
199         gdouble lo, hi;
200         char buf[64];
201         g_variant_get(gvar, "(dd)", &lo, &hi);
202         snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);
203         return QString(buf);
204 }
205
206 } // binding
207 } // prop
208 } // pv
209