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