Session: Use a monotonic clock to measure acquisition time.
[pulseview.git] / 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                 case SR_CONF_VOLTAGE_THRESHOLD:
100                         bind_enum(name, key, gvar_list, print_voltage_threshold);
101                         break;
102                 }
103
104                 if (gvar_list)
105                         g_variant_unref(gvar_list);
106         }
107         g_variant_unref(gvar_opts);
108 }
109
110 GVariant* DeviceOptions::config_getter(
111         const struct sr_dev_inst *sdi, int key)
112 {
113         GVariant *data = NULL;
114         if (sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
115                 qDebug() <<
116                         "WARNING: Failed to get value of config id" << key;
117                 return NULL;
118         }
119         return data;
120 }
121
122 void DeviceOptions::config_setter(
123         const struct sr_dev_inst *sdi, int key, GVariant* value)
124 {
125         if (sr_config_set(sdi, key, value) != SR_OK)
126                 qDebug() << "WARNING: Failed to set value of sample rate";
127 }
128
129 void DeviceOptions::bind_bool(const QString &name, int key)
130 {
131         _properties.push_back(shared_ptr<Property>(
132                 new Bool(name, bind(config_getter, _sdi, key),
133                         bind(config_setter, _sdi, key, _1))));
134 }
135
136 void DeviceOptions::bind_enum(const QString &name, int key,
137         GVariant *const gvar_list, function<QString (GVariant*)> printer)
138 {
139         GVariant *gvar;
140         GVariantIter iter;
141         vector< pair<GVariant*, QString> > values;
142
143         assert(gvar_list);
144
145         g_variant_iter_init (&iter, gvar_list);
146         while ((gvar = g_variant_iter_next_value (&iter)))
147                 values.push_back(make_pair(gvar, printer(gvar)));
148
149         _properties.push_back(shared_ptr<Property>(
150                 new Enum(name, values,
151                         bind(config_getter, _sdi, key),
152                         bind(config_setter, _sdi, key, _1))));
153 }
154
155 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
156         optional< std::pair<int64_t, int64_t> > range)
157 {
158         _properties.push_back(shared_ptr<Property>(
159                 new Int(name, suffix, range,
160                         bind(config_getter, _sdi, key),
161                         bind(config_setter, _sdi, key, _1))));
162 }
163
164 QString DeviceOptions::print_gvariant(GVariant *const gvar)
165 {
166         QString s;
167
168         if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s")))
169                 s = QString(g_variant_get_string(gvar, NULL));
170         else
171         {
172                 gchar *const text = g_variant_print(gvar, FALSE);
173                 s = QString(text);
174                 g_free(text);
175         }
176
177         return s;
178 }
179
180 void DeviceOptions::bind_samplerate(const QString &name,
181         GVariant *const gvar_list)
182 {
183         GVariant *gvar_list_samplerates;
184
185         assert(gvar_list);
186
187         if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
188                         "samplerate-steps", G_VARIANT_TYPE("at"))))
189         {
190                 gsize num_elements;
191                 const uint64_t *const elements =
192                         (const uint64_t *)g_variant_get_fixed_array(
193                                 gvar_list_samplerates, &num_elements, sizeof(uint64_t));
194
195                 assert(num_elements == 3);
196
197                 _properties.push_back(shared_ptr<Property>(
198                         new Double(name, 0, QObject::tr("Hz"),
199                                 make_pair((double)elements[0], (double)elements[1]),
200                                                 (double)elements[2],
201                                 bind(samplerate_double_getter, _sdi),
202                                 bind(samplerate_double_setter, _sdi, _1))));
203
204                 g_variant_unref(gvar_list_samplerates);
205         }
206         else if ((gvar_list_samplerates = g_variant_lookup_value(gvar_list,
207                         "samplerates", G_VARIANT_TYPE("at"))))
208         {
209                 bind_enum(name, SR_CONF_SAMPLERATE,
210                         gvar_list_samplerates, print_samplerate);
211                 g_variant_unref(gvar_list_samplerates);
212         }
213 }
214
215 QString DeviceOptions::print_samplerate(GVariant *const gvar)
216 {
217         char *const s = sr_samplerate_string(
218                 g_variant_get_uint64(gvar));
219         const QString qstring(s);
220         g_free(s);
221         return qstring;
222 }
223
224 GVariant* DeviceOptions::samplerate_double_getter(
225         const struct sr_dev_inst *sdi)
226 {
227         GVariant *const gvar = config_getter(sdi, SR_CONF_SAMPLERATE);
228
229         if(!gvar)
230                 return NULL;
231
232         GVariant *const gvar_double = g_variant_new_double(
233                 g_variant_get_uint64(gvar));
234
235         g_variant_unref(gvar);
236
237         return gvar_double;
238 }
239
240 void DeviceOptions::samplerate_double_setter(
241         struct sr_dev_inst *sdi, GVariant *value)
242 {
243         GVariant *const gvar = g_variant_new_uint64(
244                 g_variant_get_double(value));
245         config_setter(sdi, SR_CONF_SAMPLERATE, gvar);
246 }
247
248 QString DeviceOptions::print_timebase(GVariant *const gvar)
249 {
250         uint64_t p, q;
251         g_variant_get(gvar, "(tt)", &p, &q);
252         return QString(sr_period_string(p * q));
253 }
254
255 QString DeviceOptions::print_vdiv(GVariant *const gvar)
256 {
257         uint64_t p, q;
258         g_variant_get(gvar, "(tt)", &p, &q);
259         return QString(sr_voltage_string(p, q));
260 }
261
262 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
263 {
264         gdouble lo, hi;
265         char buf[64];
266         g_variant_get(gvar, "(dd)", &lo, &hi);
267         snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);
268         return QString(buf);
269 }
270
271 } // binding
272 } // prop
273 } // pv
274