]> sigrok.org Git - pulseview.git/blob - pv/binding/device.cpp
Device: add support for list of discrete values for probe factor
[pulseview.git] / pv / binding / device.cpp
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
21 #include <stdint.h>
22
23 #include <QDebug>
24
25 #include "device.hpp"
26
27 #include <pv/prop/bool.hpp>
28 #include <pv/prop/double.hpp>
29 #include <pv/prop/enum.hpp>
30 #include <pv/prop/int.hpp>
31
32 #include <libsigrokcxx/libsigrokcxx.hpp>
33
34 using boost::optional;
35 using std::function;
36 using std::make_pair;
37 using std::pair;
38 using std::shared_ptr;
39 using std::string;
40 using std::vector;
41
42 using sigrok::Capability;
43 using sigrok::Configurable;
44 using sigrok::ConfigKey;
45 using sigrok::Error;
46
47 using pv::prop::Bool;
48 using pv::prop::Double;
49 using pv::prop::Enum;
50 using pv::prop::Int;
51 using pv::prop::Property;
52
53 namespace pv {
54 namespace binding {
55
56 Device::Device(shared_ptr<sigrok::Configurable> configurable) :
57         configurable_(configurable)
58 {
59
60         auto keys = configurable->config_keys();
61
62         for (auto key : keys) {
63
64                 auto capabilities = configurable->config_capabilities(key);
65
66                 if (!capabilities.count(Capability::GET) ||
67                         !capabilities.count(Capability::SET))
68                         continue;
69
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);
78
79                 const Property::Getter get = [&, key]() {
80                         return configurable_->config_get(key); };
81                 const Property::Setter set = [&, key](Glib::VariantBase value) {
82                         configurable_->config_set(key, value);
83                         config_changed();
84                 };
85
86                 switch (key->id()) {
87                 case SR_CONF_SAMPLERATE:
88                         // Sample rate values are not bound because they are shown
89                         // in the MainBar
90                         break;
91
92                 case SR_CONF_CAPTURE_RATIO:
93                         bind_int(name, "%", pair<int64_t, int64_t>(0, 100),
94                                 get, set);
95                         break;
96
97                 case SR_CONF_PATTERN_MODE:
98                 case SR_CONF_BUFFERSIZE:
99                 case SR_CONF_TRIGGER_SOURCE:
100                 case SR_CONF_TRIGGER_SLOPE:
101                 case SR_CONF_COUPLING:
102                 case SR_CONF_CLOCK_EDGE:
103                         bind_enum(name, key, capabilities, get, set);
104                         break;
105
106                 case SR_CONF_FILTER:
107                 case SR_CONF_EXTERNAL_CLOCK:
108                 case SR_CONF_RLE:
109                 case SR_CONF_POWER_OFF:
110                         bind_bool(name, get, set);
111                         break;
112
113                 case SR_CONF_TIMEBASE:
114                         bind_enum(name, key, capabilities, get, set, print_timebase);
115                         break;
116
117                 case SR_CONF_VDIV:
118                         bind_enum(name, key, capabilities, get, set, print_vdiv);
119                         break;
120
121                 case SR_CONF_VOLTAGE_THRESHOLD:
122                         bind_enum(name, key, capabilities, get, set, print_voltage_threshold);
123                         break;
124
125                 case SR_CONF_PROBE_FACTOR:
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);
130                         break;
131
132                 default:
133                         break;
134                 }
135         }
136 }
137
138 void Device::bind_bool(const QString &name,
139         Property::Getter getter, Property::Setter setter)
140 {
141         assert(configurable_);
142         properties_.push_back(shared_ptr<Property>(new Bool(
143                 name, getter, setter)));
144 }
145
146 void Device::bind_enum(const QString &name,
147         const ConfigKey *key, std::set<const Capability *> capabilities,
148         Property::Getter getter,
149         Property::Setter setter, function<QString (Glib::VariantBase)> printer)
150 {
151         Glib::VariantBase gvar;
152         vector< pair<Glib::VariantBase, QString> > values;
153
154         assert(configurable_);
155
156         if (!capabilities.count(Capability::LIST))
157                 return;
158
159         Glib::VariantIter iter(configurable_->config_list(key));
160         while ((iter.next_value(gvar)))
161                 values.push_back(make_pair(gvar, printer(gvar)));
162
163         properties_.push_back(shared_ptr<Property>(new Enum(name, values,
164                 getter, setter)));
165 }
166
167 void Device::bind_int(const QString &name, QString suffix,
168         optional< std::pair<int64_t, int64_t> > range,
169         Property::Getter getter, Property::Setter setter)
170 {
171         assert(configurable_);
172
173         properties_.push_back(shared_ptr<Property>(new Int(name, suffix, range,
174                 getter, setter)));
175 }
176
177 QString Device::print_timebase(Glib::VariantBase gvar)
178 {
179         uint64_t p, q;
180         g_variant_get(gvar.gobj(), "(tt)", &p, &q);
181         return QString::fromUtf8(sr_period_string(p * q));
182 }
183
184 QString Device::print_vdiv(Glib::VariantBase gvar)
185 {
186         uint64_t p, q;
187         g_variant_get(gvar.gobj(), "(tt)", &p, &q);
188         return QString::fromUtf8(sr_voltage_string(p, q));
189 }
190
191 QString Device::print_voltage_threshold(Glib::VariantBase gvar)
192 {
193         gdouble lo, hi;
194         g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
195         return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
196 }
197
198 QString 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
205 } // binding
206 } // pv