]> sigrok.org Git - pulseview.git/blame_incremental - pv/prop/binding/deviceoptions.cpp
Replaced boost::bind with C++11 lambdas
[pulseview.git] / pv / prop / binding / deviceoptions.cpp
... / ...
CommitLineData
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 <stdint.h>
22
23#include <QDebug>
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
35using boost::optional;
36using std::function;
37using std::make_pair;
38using std::pair;
39using std::shared_ptr;
40using std::string;
41using std::vector;
42
43namespace pv {
44namespace prop {
45namespace binding {
46
47DeviceOptions::DeviceOptions(shared_ptr<pv::device::DevInst> dev_inst,
48 const sr_channel_group *group) :
49 _dev_inst(dev_inst),
50 _group(group)
51{
52 assert(dev_inst);
53
54 GVariant *gvar_opts;
55 gsize num_opts;
56
57 if (!(gvar_opts = dev_inst->list_config(group, SR_CONF_DEVICE_OPTIONS)))
58 /* Driver supports no device instance options. */
59 return;
60
61 const int *const options = (const int32_t *)g_variant_get_fixed_array(
62 gvar_opts, &num_opts, sizeof(int32_t));
63 for (unsigned int i = 0; i < num_opts; i++) {
64 const struct sr_config_info *const info =
65 sr_config_info_get(options[i]);
66
67 if (!info)
68 continue;
69
70 const int key = info->key;
71 GVariant *const gvar_list = dev_inst->list_config(group, key);
72
73 const QString name = QString::fromUtf8(info->name);
74
75 const Property::Getter get = [&, key]() {
76 return _dev_inst->get_config(_group, key); };
77 const Property::Setter set = [&, key](GVariant *value) {
78 _dev_inst->set_config(_group, key, value); };
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, "%", pair<int64_t, int64_t>(0, 100),
89 get, set);
90 break;
91
92 case SR_CONF_PATTERN_MODE:
93 case SR_CONF_BUFFERSIZE:
94 case SR_CONF_TRIGGER_SOURCE:
95 case SR_CONF_TRIGGER_SLOPE:
96 case SR_CONF_FILTER:
97 case SR_CONF_COUPLING:
98 case SR_CONF_CLOCK_EDGE:
99 bind_enum(name, key, gvar_list, get, set);
100 break;
101
102 case SR_CONF_EXTERNAL_CLOCK:
103 case SR_CONF_RLE:
104 bind_bool(name, get, set);
105 break;
106
107 case SR_CONF_TIMEBASE:
108 bind_enum(name, key, gvar_list,
109 get, set, print_timebase);
110 break;
111
112 case SR_CONF_VDIV:
113 bind_enum(name, key, gvar_list, get, set, print_vdiv);
114 break;
115
116 case SR_CONF_VOLTAGE_THRESHOLD:
117 bind_enum(name, key, gvar_list,
118 get, set, print_voltage_threshold);
119 break;
120 }
121
122 if (gvar_list)
123 g_variant_unref(gvar_list);
124 }
125 g_variant_unref(gvar_opts);
126}
127
128void DeviceOptions::bind_bool(const QString &name,
129 Property::Getter getter, Property::Setter setter)
130{
131 assert(_dev_inst);
132 _properties.push_back(shared_ptr<Property>(new Bool(
133 name, getter, setter)));
134}
135
136void DeviceOptions::bind_enum(const QString &name, int key,
137 GVariant *const gvar_list, Property::Getter getter,
138 Property::Setter setter, function<QString (GVariant*)> printer)
139{
140 GVariant *gvar;
141 GVariantIter iter;
142 vector< pair<GVariant*, QString> > values;
143
144 assert(_dev_inst);
145 if (!gvar_list) {
146 qDebug() << "Config key " << key << " was listed, but no "
147 "options were given";
148 return;
149 }
150
151 g_variant_iter_init (&iter, gvar_list);
152 while ((gvar = g_variant_iter_next_value (&iter)))
153 values.push_back(make_pair(gvar, printer(gvar)));
154
155 _properties.push_back(shared_ptr<Property>(new Enum(name, values,
156 getter, setter)));
157}
158
159void DeviceOptions::bind_int(const QString &name, QString suffix,
160 optional< std::pair<int64_t, int64_t> > range,
161 Property::Getter getter, Property::Setter setter)
162{
163 assert(_dev_inst);
164
165 _properties.push_back(shared_ptr<Property>(new Int(name, suffix, range,
166 getter, setter)));
167}
168
169QString DeviceOptions::print_timebase(GVariant *const gvar)
170{
171 uint64_t p, q;
172 g_variant_get(gvar, "(tt)", &p, &q);
173 return QString::fromUtf8(sr_period_string(p * q));
174}
175
176QString DeviceOptions::print_vdiv(GVariant *const gvar)
177{
178 uint64_t p, q;
179 g_variant_get(gvar, "(tt)", &p, &q);
180 return QString::fromUtf8(sr_voltage_string(p, q));
181}
182
183QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
184{
185 gdouble lo, hi;
186 char buf[64];
187 g_variant_get(gvar, "(dd)", &lo, &hi);
188 snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi);
189 return QString::fromUtf8(buf);
190}
191
192} // binding
193} // prop
194} // pv
195