6f9788932c8dd0d4377d98771cee69ad6b6760e3
[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         std::map< const ConfigKey*, std::set<Capability> > keys;
60
61         try {
62                 keys = configurable->config_keys(ConfigKey::DEVICE_OPTIONS);
63         } catch (const Error) {
64                 return;
65         }
66
67         for (auto entry : keys) {
68                 auto key = entry.first;
69                 auto capabilities = entry.second;
70
71                 if (!capabilities.count(Capability::GET) ||
72                         !capabilities.count(Capability::SET))
73                         continue;
74
75                 string name_str;
76                 try {
77                         name_str = key->description();
78                 } catch (Error e) {
79                         name_str = key->name();
80                 }
81
82                 const QString name = QString::fromStdString(name_str);
83
84                 const Property::Getter get = [&, key]() {
85                         return configurable_->config_get(key); };
86                 const Property::Setter set = [&, key](Glib::VariantBase value) {
87                         configurable_->config_set(key, value);
88                         config_changed();
89                 };
90
91                 switch (key->id())
92                 {
93                 case SR_CONF_SAMPLERATE:
94                         // Sample rate values are not bound because they are shown
95                         // in the MainBar
96                         break;
97
98                 case SR_CONF_CAPTURE_RATIO:
99                         bind_int(name, "%", pair<int64_t, int64_t>(0, 100),
100                                 get, set);
101                         break;
102
103                 case SR_CONF_PATTERN_MODE:
104                 case SR_CONF_BUFFERSIZE:
105                 case SR_CONF_TRIGGER_SOURCE:
106                 case SR_CONF_TRIGGER_SLOPE:
107                 case SR_CONF_FILTER:
108                 case SR_CONF_COUPLING:
109                 case SR_CONF_CLOCK_EDGE:
110                         bind_enum(name, key, capabilities, get, set);
111                         break;
112
113                 case SR_CONF_EXTERNAL_CLOCK:
114                 case SR_CONF_RLE:
115                 case SR_CONF_POWER_OFF:
116                         bind_bool(name, get, set);
117                         break;
118
119                 case SR_CONF_TIMEBASE:
120                         bind_enum(name, key, capabilities, get, set, print_timebase);
121                         break;
122
123                 case SR_CONF_VDIV:
124                         bind_enum(name, key, capabilities, get, set, print_vdiv);
125                         break;
126
127                 case SR_CONF_VOLTAGE_THRESHOLD:
128                         bind_enum(name, key, capabilities, get, set, print_voltage_threshold);
129                         break;
130
131                 case SR_CONF_PROBE_FACTOR:
132                         bind_int(name, "", pair<int64_t, int64_t>(1, 500), get, set);
133                         break;
134
135                 default:
136                         break;
137                 }
138         }
139 }
140
141 void Device::bind_bool(const QString &name,
142         Property::Getter getter, Property::Setter setter)
143 {
144         assert(configurable_);
145         properties_.push_back(shared_ptr<Property>(new Bool(
146                 name, getter, setter)));
147 }
148
149 void Device::bind_enum(const QString &name,
150         const ConfigKey *key, std::set<Capability> capabilities,
151         Property::Getter getter,
152         Property::Setter setter, function<QString (Glib::VariantBase)> printer)
153 {
154         Glib::VariantBase gvar;
155         vector< pair<Glib::VariantBase, QString> > values;
156
157         assert(configurable_);
158
159         if (!capabilities.count(Capability::LIST))
160                 return;
161
162         Glib::VariantIter iter(configurable_->config_list(key));
163         while ((iter.next_value(gvar)))
164                 values.push_back(make_pair(gvar, printer(gvar)));
165
166         properties_.push_back(shared_ptr<Property>(new Enum(name, values,
167                 getter, setter)));
168 }
169
170 void Device::bind_int(const QString &name, QString suffix,
171         optional< std::pair<int64_t, int64_t> > range,
172         Property::Getter getter, Property::Setter setter)
173 {
174         assert(configurable_);
175
176         properties_.push_back(shared_ptr<Property>(new Int(name, suffix, range,
177                 getter, setter)));
178 }
179
180 QString Device::print_timebase(Glib::VariantBase gvar)
181 {
182         uint64_t p, q;
183         g_variant_get(gvar.gobj(), "(tt)", &p, &q);
184         return QString::fromUtf8(sr_period_string(p * q));
185 }
186
187 QString Device::print_vdiv(Glib::VariantBase gvar)
188 {
189         uint64_t p, q;
190         g_variant_get(gvar.gobj(), "(tt)", &p, &q);
191         return QString::fromUtf8(sr_voltage_string(p, q));
192 }
193
194 QString Device::print_voltage_threshold(Glib::VariantBase gvar)
195 {
196         gdouble lo, hi;
197         g_variant_get(gvar.gobj(), "(dd)", &lo, &hi);
198         return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
199 }
200
201 } // binding
202 } // pv