]> sigrok.org Git - pulseview.git/blame - pv/binding/device.cpp
Fix #1024 by changing decode channel assigment to PDs
[pulseview.git] / pv / binding / device.cpp
CommitLineData
cf47ef78
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
cf47ef78
JH
18 */
19
eb8269e3 20#include <cstdint>
4d5d5d6a 21
48c88445
JH
22#include <QDebug>
23
3cc9ad7b 24#include "device.hpp"
cf47ef78 25
2acdb232 26#include <pv/prop/bool.hpp>
2acdb232
JH
27#include <pv/prop/enum.hpp>
28#include <pv/prop/int.hpp>
7b3810db 29
fe3a1c21 30#include <libsigrokcxx/libsigrokcxx.hpp>
19adbc2c 31
819f4c25 32using boost::optional;
6f925ba9 33
6db73158 34using std::function;
819f4c25 35using std::pair;
6f925ba9 36using std::set;
f9abf97e 37using std::shared_ptr;
819f4c25
JH
38using std::string;
39using std::vector;
7b3810db 40
e8d00928
ML
41using sigrok::Capability;
42using sigrok::Configurable;
43using sigrok::ConfigKey;
44using sigrok::Error;
45
61703a01 46using pv::prop::Bool;
61703a01
JH
47using pv::prop::Enum;
48using pv::prop::Int;
49using pv::prop::Property;
50
cf47ef78 51namespace pv {
cf47ef78
JH
52namespace binding {
53
3cc9ad7b 54Device::Device(shared_ptr<sigrok::Configurable> configurable) :
8dbbc7f0 55 configurable_(configurable)
cf47ef78 56{
7b3810db 57
7bb0fbf4
ML
58 auto keys = configurable->config_keys();
59
60 for (auto key : keys) {
1e1b3a66 61
7bb0fbf4 62 auto capabilities = configurable->config_capabilities(key);
7b3810db 63
e8d00928
ML
64 if (!capabilities.count(Capability::GET) ||
65 !capabilities.count(Capability::SET))
de1d99bb
JH
66 continue;
67
e8d00928
ML
68 string name_str;
69 try {
70 name_str = key->description();
71 } catch (Error e) {
72 name_str = key->name();
73 }
74
75 const QString name = QString::fromStdString(name_str);
4d5d5d6a 76
6db73158 77 const Property::Getter get = [&, key]() {
8dbbc7f0 78 return configurable_->config_get(key); };
e8d00928 79 const Property::Setter set = [&, key](Glib::VariantBase value) {
8dbbc7f0 80 configurable_->config_set(key, value);
e8d00928
ML
81 config_changed();
82 };
6db73158 83
2ad82c2e 84 switch (key->id()) {
9f6af8bd 85 case SR_CONF_SAMPLERATE:
117ef009 86 // Sample rate values are not bound because they are shown
7c657094 87 // in the MainBar
9f6af8bd
JH
88 break;
89
a2b92157 90 case SR_CONF_CAPTURE_RATIO:
9f95aad1 91 bind_int(name, "", "%", pair<int64_t, int64_t>(0, 100), get, set);
a2b92157
JH
92 break;
93
10b1be92 94 case SR_CONF_PATTERN_MODE:
10b1be92 95 case SR_CONF_BUFFERSIZE:
10b1be92 96 case SR_CONF_TRIGGER_SOURCE:
5be76b1a 97 case SR_CONF_TRIGGER_SLOPE:
4d5d5d6a 98 case SR_CONF_COUPLING:
5be76b1a 99 case SR_CONF_CLOCK_EDGE:
9a267f8d 100 bind_enum(name, "", key, capabilities, get, set);
7b3810db
JH
101 break;
102
0623eb8c 103 case SR_CONF_FILTER:
5be76b1a 104 case SR_CONF_EXTERNAL_CLOCK:
de1d99bb 105 case SR_CONF_RLE:
198f9bc7 106 case SR_CONF_POWER_OFF:
9a267f8d 107 bind_bool(name, "", get, set);
de1d99bb
JH
108 break;
109
4d5d5d6a 110 case SR_CONF_TIMEBASE:
9a267f8d 111 bind_enum(name, "", key, capabilities, get, set, print_timebase);
7b3810db
JH
112 break;
113
4d5d5d6a 114 case SR_CONF_VDIV:
9a267f8d 115 bind_enum(name, "", key, capabilities, get, set, print_vdiv);
7b3810db 116 break;
62874984
MC
117
118 case SR_CONF_VOLTAGE_THRESHOLD:
9a267f8d 119 bind_enum(name, "", key, capabilities, get, set, print_voltage_threshold);
62874984 120 break;
4d5d5d6a 121
198f9bc7 122 case SR_CONF_PROBE_FACTOR:
49c62417 123 if (capabilities.count(Capability::LIST))
9a267f8d 124 bind_enum(name, "", key, capabilities, get, set, print_probe_factor);
49c62417 125 else
9a267f8d 126 bind_int(name, "", "", pair<int64_t, int64_t>(1, 500), get, set);
198f9bc7
BG
127 break;
128
e8d00928
ML
129 default:
130 break;
131 }
7b3810db
JH
132 }
133}
134
9a267f8d 135void Device::bind_bool(const QString &name, const QString &desc,
6db73158 136 Property::Getter getter, Property::Setter setter)
de1d99bb 137{
8dbbc7f0
JH
138 assert(configurable_);
139 properties_.push_back(shared_ptr<Property>(new Bool(
9a267f8d 140 name, desc, getter, setter)));
de1d99bb
JH
141}
142
9a267f8d 143void Device::bind_enum(const QString &name, const QString &desc,
6f925ba9 144 const ConfigKey *key, set<const Capability *> capabilities,
ef2986c0 145 Property::Getter getter,
e8d00928 146 Property::Setter setter, function<QString (Glib::VariantBase)> printer)
9f6af8bd 147{
8dbbc7f0 148 assert(configurable_);
de1d99bb 149
ef2986c0
AJ
150 if (!capabilities.count(Capability::LIST))
151 return;
152
a9627352
SA
153 try {
154 Glib::VariantContainerBase gvar = configurable_->config_list(key);
155 Glib::VariantIter iter(gvar);
156
157 vector< pair<Glib::VariantBase, QString> > values;
158 while ((iter.next_value(gvar)))
20f59e95 159 values.emplace_back(gvar, printer(gvar));
a9627352
SA
160
161 properties_.push_back(shared_ptr<Property>(new Enum(name, desc, values,
162 getter, setter)));
9f6af8bd 163
a9627352
SA
164 } catch (sigrok::Error& e) {
165 qDebug() << "Error: Listing device key" << name << "failed!";
166 return;
167 }
9f6af8bd
JH
168}
169
9a267f8d 170void Device::bind_int(const QString &name, const QString &desc, QString suffix,
6f925ba9 171 optional< pair<int64_t, int64_t> > range,
6db73158 172 Property::Getter getter, Property::Setter setter)
a2b92157 173{
8dbbc7f0 174 assert(configurable_);
19adbc2c 175
9a267f8d
UH
176 properties_.push_back(shared_ptr<Property>(new Int(name, desc, suffix,
177 range, getter, setter)));
a2b92157
JH
178}
179
3cc9ad7b 180QString Device::print_timebase(Glib::VariantBase gvar)
4d5d5d6a
BV
181{
182 uint64_t p, q;
e8d00928 183 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
634ecdcd 184 return QString::fromUtf8(sr_period_string(p, q));
4d5d5d6a
BV
185}
186
3cc9ad7b 187QString Device::print_vdiv(Glib::VariantBase gvar)
4d5d5d6a
BV
188{
189 uint64_t p, q;
e8d00928 190 g_variant_get(gvar.gobj(), "(tt)", &p, &q);
27e8df22 191 return QString::fromUtf8(sr_voltage_string(p, q));
9f6af8bd
JH
192}
193
3cc9ad7b 194QString Device::print_voltage_threshold(Glib::VariantBase gvar)
62874984
MC
195{
196 gdouble lo, hi;
e8d00928 197 g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
b47e449a 198 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
62874984
MC
199}
200
49c62417
AJ
201QString Device::print_probe_factor(Glib::VariantBase gvar)
202{
203 uint64_t factor;
204 factor = g_variant_get_uint64(gvar.gobj());
205 return QString("%1x").arg(factor);
206}
207
870ea3db
UH
208} // namespace binding
209} // namespace pv