Consistenty use auto-generated namespace comments.
[pulseview.git] / pv / binding / device.cpp
blob3ebc9f9f41839b67d0d6fac1b8cb4d26f7b5ff33
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
20 #include <cstdint>
22 #include <QDebug>
24 #include "device.hpp"
26 #include <pv/prop/bool.hpp>
27 #include <pv/prop/enum.hpp>
28 #include <pv/prop/int.hpp>
30 #include <libsigrokcxx/libsigrokcxx.hpp>
32 using boost::optional;
34 using std::function;
35 using std::make_pair;
36 using std::pair;
37 using std::set;
38 using std::shared_ptr;
39 using std::string;
40 using std::vector;
42 using sigrok::Capability;
43 using sigrok::Configurable;
44 using sigrok::ConfigKey;
45 using sigrok::Error;
47 using pv::prop::Bool;
48 using pv::prop::Enum;
49 using pv::prop::Int;
50 using pv::prop::Property;
52 namespace pv {
53 namespace binding {
55 Device::Device(shared_ptr<sigrok::Configurable> configurable) :
56 configurable_(configurable)
59 auto keys = configurable->config_keys();
61 for (auto key : keys) {
63 auto capabilities = configurable->config_capabilities(key);
65 if (!capabilities.count(Capability::GET) ||
66 !capabilities.count(Capability::SET))
67 continue;
69 string name_str;
70 try {
71 name_str = key->description();
72 } catch (Error e) {
73 name_str = key->name();
76 const QString name = QString::fromStdString(name_str);
78 const Property::Getter get = [&, key]() {
79 return configurable_->config_get(key); };
80 const Property::Setter set = [&, key](Glib::VariantBase value) {
81 configurable_->config_set(key, value);
82 config_changed();
85 switch (key->id()) {
86 case SR_CONF_SAMPLERATE:
87 // Sample rate values are not bound because they are shown
88 // in the MainBar
89 break;
91 case SR_CONF_CAPTURE_RATIO:
92 bind_int(name, "%", pair<int64_t, int64_t>(0, 100),
93 get, set);
94 break;
96 case SR_CONF_PATTERN_MODE:
97 case SR_CONF_BUFFERSIZE:
98 case SR_CONF_TRIGGER_SOURCE:
99 case SR_CONF_TRIGGER_SLOPE:
100 case SR_CONF_COUPLING:
101 case SR_CONF_CLOCK_EDGE:
102 bind_enum(name, key, capabilities, get, set);
103 break;
105 case SR_CONF_FILTER:
106 case SR_CONF_EXTERNAL_CLOCK:
107 case SR_CONF_RLE:
108 case SR_CONF_POWER_OFF:
109 bind_bool(name, get, set);
110 break;
112 case SR_CONF_TIMEBASE:
113 bind_enum(name, key, capabilities, get, set, print_timebase);
114 break;
116 case SR_CONF_VDIV:
117 bind_enum(name, key, capabilities, get, set, print_vdiv);
118 break;
120 case SR_CONF_VOLTAGE_THRESHOLD:
121 bind_enum(name, key, capabilities, get, set, print_voltage_threshold);
122 break;
124 case SR_CONF_PROBE_FACTOR:
125 if (capabilities.count(Capability::LIST))
126 bind_enum(name, key, capabilities, get, set, print_probe_factor);
127 else
128 bind_int(name, "", pair<int64_t, int64_t>(1, 500), get, set);
129 break;
131 default:
132 break;
137 void Device::bind_bool(const QString &name,
138 Property::Getter getter, Property::Setter setter)
140 assert(configurable_);
141 properties_.push_back(shared_ptr<Property>(new Bool(
142 name, getter, setter)));
145 void Device::bind_enum(const QString &name,
146 const ConfigKey *key, set<const Capability *> capabilities,
147 Property::Getter getter,
148 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
150 Glib::VariantBase gvar;
151 vector< pair<Glib::VariantBase, QString> > values;
153 assert(configurable_);
155 if (!capabilities.count(Capability::LIST))
156 return;
158 Glib::VariantIter iter(configurable_->config_list(key));
159 while ((iter.next_value(gvar)))
160 values.push_back(make_pair(gvar, printer(gvar)));
162 properties_.push_back(shared_ptr<Property>(new Enum(name, values,
163 getter, setter)));
166 void Device::bind_int(const QString &name, QString suffix,
167 optional< pair<int64_t, int64_t> > range,
168 Property::Getter getter, Property::Setter setter)
170 assert(configurable_);
172 properties_.push_back(shared_ptr<Property>(new Int(name, suffix, range,
173 getter, setter)));
176 QString Device::print_timebase(Glib::VariantBase gvar)
178 uint64_t p, q;
179 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
180 return QString::fromUtf8(sr_period_string(p, q));
183 QString Device::print_vdiv(Glib::VariantBase gvar)
185 uint64_t p, q;
186 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
187 return QString::fromUtf8(sr_voltage_string(p, q));
190 QString Device::print_voltage_threshold(Glib::VariantBase gvar)
192 gdouble lo, hi;
193 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
194 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
197 QString Device::print_probe_factor(Glib::VariantBase gvar)
199 uint64_t factor;
200 factor = g_variant_get_uint64(gvar.gobj());
201 return QString("%1x").arg(factor);
204 } // namespace binding
205 } // namespace pv