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