]> sigrok.org Git - pulseview.git/blob - pv/prop/binding/deviceoptions.cpp
Update for 'probe_group' -> 'channel_group' rename in libsigrok.
[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 <stdint.h>
24
25 #include "deviceoptions.h"
26
27 #include <pv/device/devinst.h>
28 #include <pv/prop/bool.h>
29 #include <pv/prop/double.h>
30 #include <pv/prop/enum.h>
31 #include <pv/prop/int.h>
32
33 #include <libsigrok/libsigrok.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(shared_ptr<pv::device::DevInst> dev_inst,
49         const sr_channel_group *group) :
50         _dev_inst(dev_inst),
51         _group(group)
52 {
53         assert(dev_inst);
54
55         GVariant *gvar_opts;
56         gsize num_opts;
57
58         if (!(gvar_opts = dev_inst->list_config(group, SR_CONF_DEVICE_OPTIONS)))
59                 /* Driver supports no device instance options. */
60                 return;
61
62         const int *const options = (const int32_t *)g_variant_get_fixed_array(
63                 gvar_opts, &num_opts, sizeof(int32_t));
64         for (unsigned int i = 0; i < num_opts; i++) {
65                 const struct sr_config_info *const info =
66                         sr_config_info_get(options[i]);
67
68                 if (!info)
69                         continue;
70
71                 const int key = info->key;
72                 GVariant *const gvar_list = dev_inst->list_config(group, key);
73
74                 const QString name = QString::fromUtf8(info->name);
75
76                 switch(key)
77                 {
78                 case SR_CONF_SAMPLERATE:
79                         // Sample rate values are not bound because they are shown
80                         // in the SamplingBar
81                         break;
82
83                 case SR_CONF_CAPTURE_RATIO:
84                         bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100));
85                         break;
86
87                 case SR_CONF_PATTERN_MODE:
88                 case SR_CONF_BUFFERSIZE:
89                 case SR_CONF_TRIGGER_SOURCE:
90                 case SR_CONF_TRIGGER_SLOPE:
91                 case SR_CONF_FILTER:
92                 case SR_CONF_COUPLING:
93                 case SR_CONF_CLOCK_EDGE:
94                         bind_enum(name, key, gvar_list);
95                         break;
96
97                 case SR_CONF_EXTERNAL_CLOCK:
98                 case SR_CONF_RLE:
99                         bind_bool(name, key);
100                         break;
101
102                 case SR_CONF_TIMEBASE:
103                         bind_enum(name, key, gvar_list, print_timebase);
104                         break;
105
106                 case SR_CONF_VDIV:
107                         bind_enum(name, key, gvar_list, print_vdiv);
108                         break;
109
110                 case SR_CONF_VOLTAGE_THRESHOLD:
111                         bind_enum(name, key, gvar_list, print_voltage_threshold);
112                         break;
113                 }
114
115                 if (gvar_list)
116                         g_variant_unref(gvar_list);
117         }
118         g_variant_unref(gvar_opts);
119 }
120
121 void DeviceOptions::bind_bool(const QString &name, int key)
122 {
123         assert(_dev_inst);
124         _properties.push_back(shared_ptr<Property>(new Bool(name,
125                 bind(&device::DevInst::get_config, _dev_inst, _group, key),
126                 bind(&device::DevInst::set_config, _dev_inst,
127                         _group, key, _1))));
128 }
129
130 void DeviceOptions::bind_enum(const QString &name, int key,
131         GVariant *const gvar_list, function<QString (GVariant*)> printer)
132 {
133         GVariant *gvar;
134         GVariantIter iter;
135         vector< pair<GVariant*, QString> > values;
136
137         assert(_dev_inst);
138         assert(gvar_list);
139
140         g_variant_iter_init (&iter, gvar_list);
141         while ((gvar = g_variant_iter_next_value (&iter)))
142                 values.push_back(make_pair(gvar, printer(gvar)));
143
144         _properties.push_back(shared_ptr<Property>(new Enum(name, values,
145                 bind(&device::DevInst::get_config, _dev_inst, _group, key),
146                 bind(&device::DevInst::set_config, _dev_inst,
147                         _group, key, _1))));
148 }
149
150 void DeviceOptions::bind_int(const QString &name, int key, QString suffix,
151         optional< std::pair<int64_t, int64_t> > range)
152 {
153         assert(_dev_inst);
154
155         _properties.push_back(shared_ptr<Property>(new Int(name, suffix, range,
156                 bind(&device::DevInst::get_config, _dev_inst, _group, key),
157                 bind(&device::DevInst::set_config, _dev_inst, _group, key, _1))));
158 }
159
160 QString DeviceOptions::print_timebase(GVariant *const gvar)
161 {
162         uint64_t p, q;
163         g_variant_get(gvar, "(tt)", &p, &q);
164         return QString::fromUtf8(sr_period_string(p * q));
165 }
166
167 QString DeviceOptions::print_vdiv(GVariant *const gvar)
168 {
169         uint64_t p, q;
170         g_variant_get(gvar, "(tt)", &p, &q);
171         return QString::fromUtf8(sr_voltage_string(p, q));
172 }
173
174 QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
175 {
176         gdouble lo, hi;
177         char buf[64];
178         g_variant_get(gvar, "(dd)", &lo, &hi);
179         snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);
180         return QString::fromUtf8(buf);
181 }
182
183 } // binding
184 } // prop
185 } // pv
186