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