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