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