]> sigrok.org Git - pulseview.git/blame_incremental - pv/binding/device.cpp
Device binding: Notify user when a config option is ignored
[pulseview.git] / pv / binding / device.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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <cstdint>
21
22#include <QDebug>
23
24#include "device.hpp"
25
26#include <pv/prop/bool.hpp>
27#include <pv/prop/enum.hpp>
28#include <pv/prop/int.hpp>
29
30#include <libsigrokcxx/libsigrokcxx.hpp>
31
32using boost::optional;
33
34using std::function;
35using std::pair;
36using std::set;
37using std::shared_ptr;
38using std::string;
39using std::vector;
40
41using sigrok::Capability;
42using sigrok::Configurable;
43using sigrok::ConfigKey;
44using sigrok::Error;
45
46using pv::prop::Bool;
47using pv::prop::Enum;
48using pv::prop::Int;
49using pv::prop::Property;
50
51namespace pv {
52namespace binding {
53
54Device::Device(shared_ptr<sigrok::Configurable> configurable) :
55 configurable_(configurable)
56{
57
58 auto keys = configurable->config_keys();
59
60 for (auto key : keys) {
61
62 auto capabilities = configurable->config_capabilities(key);
63
64 string name_str;
65 try {
66 name_str = key->description();
67 } catch (Error& e) {
68 name_str = key->name();
69 }
70
71 const QString name = QString::fromStdString(name_str);
72
73 if (!capabilities.count(Capability::GET) ||
74 !capabilities.count(Capability::SET)) {
75 qDebug() << QString(tr("Note for device developers: Ignoring device configuration capability '%1' " \
76 "as it is missing GET and/or SET")).arg(name);
77 continue;
78 }
79
80 const Property::Getter get = [&, key]() {
81 return configurable_->config_get(key); };
82 const Property::Setter set = [&, key](Glib::VariantBase value) {
83 configurable_->config_set(key, value);
84 config_changed();
85 };
86
87 switch (key->id()) {
88 case SR_CONF_SAMPLERATE:
89 // Sample rate values are not bound because they are shown
90 // in the MainBar
91 break;
92
93 case SR_CONF_CAPTURE_RATIO:
94 bind_int(name, "", "%", pair<int64_t, int64_t>(0, 100), get, set);
95 break;
96
97 case SR_CONF_LIMIT_FRAMES:
98 // Value 0 means that there is no limit
99 bind_int(name, "", "", pair<int64_t, int64_t>(0, 1000000), get, set,
100 tr("No Limit"));
101 break;
102
103 case SR_CONF_PATTERN_MODE:
104 case SR_CONF_BUFFERSIZE:
105 case SR_CONF_TRIGGER_SOURCE:
106 case SR_CONF_TRIGGER_SLOPE:
107 case SR_CONF_COUPLING:
108 case SR_CONF_CLOCK_EDGE:
109 case SR_CONF_DATA_SOURCE:
110 case SR_CONF_EXTERNAL_CLOCK_SOURCE:
111 bind_enum(name, "", key, capabilities, get, set);
112 break;
113
114 case SR_CONF_FILTER:
115 case SR_CONF_EXTERNAL_CLOCK:
116 case SR_CONF_RLE:
117 case SR_CONF_POWER_OFF:
118 case SR_CONF_AVERAGING:
119 bind_bool(name, "", get, set);
120 break;
121
122 case SR_CONF_TIMEBASE:
123 bind_enum(name, "", key, capabilities, get, set, print_timebase);
124 break;
125
126 case SR_CONF_VDIV:
127 bind_enum(name, "", key, capabilities, get, set, print_vdiv);
128 break;
129
130 case SR_CONF_VOLTAGE_THRESHOLD:
131 bind_enum(name, "", key, capabilities, get, set, print_voltage_threshold);
132 break;
133
134 case SR_CONF_PROBE_FACTOR:
135 if (capabilities.count(Capability::LIST))
136 bind_enum(name, "", key, capabilities, get, set, print_probe_factor);
137 else
138 bind_int(name, "", "", pair<int64_t, int64_t>(1, 500), get, set);
139 break;
140
141 case SR_CONF_AVG_SAMPLES:
142 if (capabilities.count(Capability::LIST))
143 bind_enum(name, "", key, capabilities, get, set, print_averages);
144 else
145 bind_int(name, "", "", pair<int64_t, int64_t>(0, INT32_MAX), get, set);
146 break;
147
148 default:
149 break;
150 }
151 }
152}
153
154void Device::bind_bool(const QString &name, const QString &desc,
155 Property::Getter getter, Property::Setter setter)
156{
157 assert(configurable_);
158 properties_.push_back(shared_ptr<Property>(new Bool(
159 name, desc, getter, setter)));
160}
161
162void Device::bind_enum(const QString &name, const QString &desc,
163 const ConfigKey *key, set<const Capability *> capabilities,
164 Property::Getter getter,
165 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
166{
167 assert(configurable_);
168
169 if (!capabilities.count(Capability::LIST))
170 return;
171
172 try {
173 Glib::VariantContainerBase gvar = configurable_->config_list(key);
174 Glib::VariantIter iter(gvar);
175
176 vector< pair<Glib::VariantBase, QString> > values;
177 while ((iter.next_value(gvar)))
178 values.emplace_back(gvar, printer(gvar));
179
180 properties_.push_back(shared_ptr<Property>(new Enum(name, desc, values,
181 getter, setter)));
182
183 } catch (sigrok::Error& e) {
184 qDebug() << "Error: Listing device key" << name << "failed!";
185 return;
186 }
187}
188
189void Device::bind_int(const QString &name, const QString &desc, QString suffix,
190 optional< pair<int64_t, int64_t> > range, Property::Getter getter,
191 Property::Setter setter, QString special_value_text)
192{
193 assert(configurable_);
194
195 properties_.push_back(shared_ptr<Property>(new Int(name, desc, suffix,
196 range, getter, setter, special_value_text)));
197}
198
199QString Device::print_timebase(Glib::VariantBase gvar)
200{
201 uint64_t p, q;
202 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
203 return QString::fromUtf8(sr_period_string(p, q));
204}
205
206QString Device::print_vdiv(Glib::VariantBase gvar)
207{
208 uint64_t p, q;
209 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
210 return QString::fromUtf8(sr_voltage_string(p, q));
211}
212
213QString Device::print_voltage_threshold(Glib::VariantBase gvar)
214{
215 gdouble lo, hi;
216 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
217 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
218}
219
220QString Device::print_probe_factor(Glib::VariantBase gvar)
221{
222 uint64_t factor;
223 factor = g_variant_get_uint64(gvar.gobj());
224 return QString("%1x").arg(factor);
225}
226
227QString Device::print_averages(Glib::VariantBase gvar)
228{
229 uint64_t avg;
230 avg = g_variant_get_uint64(gvar.gobj());
231 return QString("%1").arg(avg);
232}
233
234} // namespace binding
235} // namespace pv