]>
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 | |
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 | ||
7b3810db JH |
21 | #include <boost/bind.hpp> |
22 | ||
d71bca8b | 23 | #include <QDebug> |
9f6af8bd | 24 | #include <QObject> |
d71bca8b | 25 | |
4d5d5d6a BV |
26 | #include <stdint.h> |
27 | ||
3820592a | 28 | #include "deviceoptions.h" |
cf47ef78 | 29 | |
de1d99bb | 30 | #include <pv/prop/bool.h> |
9f6af8bd | 31 | #include <pv/prop/double.h> |
7b3810db | 32 | #include <pv/prop/enum.h> |
a2b92157 | 33 | #include <pv/prop/int.h> |
7b3810db | 34 | |
819f4c25 JH |
35 | using boost::bind; |
36 | using boost::function; | |
37 | using boost::optional; | |
38 | using boost::shared_ptr; | |
39 | using std::make_pair; | |
40 | using std::pair; | |
41 | using std::string; | |
42 | using std::vector; | |
7b3810db | 43 | |
cf47ef78 JH |
44 | namespace pv { |
45 | namespace prop { | |
46 | namespace binding { | |
47 | ||
2f779c1b | 48 | DeviceOptions::DeviceOptions(const sr_dev_inst *sdi) : |
cf47ef78 JH |
49 | _sdi(sdi) |
50 | { | |
95237c18 JH |
51 | assert(sdi); |
52 | ||
4d5d5d6a BV |
53 | GVariant *gvar_opts, *gvar_list; |
54 | gsize num_opts; | |
7b3810db | 55 | |
68162c29 BV |
56 | if ((sr_config_list(sdi->driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, |
57 | &gvar_opts) != SR_OK)) | |
7b3810db JH |
58 | /* Driver supports no device instance options. */ |
59 | return; | |
60 | ||
4d5d5d6a BV |
61 | const int *const options = (const int32_t *)g_variant_get_fixed_array( |
62 | gvar_opts, &num_opts, sizeof(int32_t)); | |
63 | for (unsigned int i = 0; i < num_opts; i++) { | |
10b1be92 | 64 | const struct sr_config_info *const info = |
4d5d5d6a BV |
65 | sr_config_info_get(options[i]); |
66 | ||
de1d99bb JH |
67 | if (!info) |
68 | continue; | |
69 | ||
4d5d5d6a | 70 | const int key = info->key; |
7b3810db | 71 | |
68162c29 | 72 | if (sr_config_list(_sdi->driver, _sdi, NULL, key, &gvar_list) != SR_OK) |
de1d99bb | 73 | gvar_list = NULL; |
7b3810db | 74 | |
27e8df22 | 75 | const QString name = QString::fromUtf8(info->name); |
4d5d5d6a BV |
76 | |
77 | switch(key) | |
7b3810db | 78 | { |
9f6af8bd | 79 | case SR_CONF_SAMPLERATE: |
117ef009 JH |
80 | // Sample rate values are not bound because they are shown |
81 | // in the SamplingBar | |
9f6af8bd JH |
82 | break; |
83 | ||
a2b92157 | 84 | case SR_CONF_CAPTURE_RATIO: |
c6dd7ed2 | 85 | bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100)); |
a2b92157 JH |
86 | break; |
87 | ||
10b1be92 | 88 | case SR_CONF_PATTERN_MODE: |
10b1be92 | 89 | case SR_CONF_BUFFERSIZE: |
10b1be92 | 90 | case SR_CONF_TRIGGER_SOURCE: |
10b1be92 | 91 | case SR_CONF_FILTER: |
4d5d5d6a BV |
92 | case SR_CONF_COUPLING: |
93 | bind_enum(name, key, gvar_list); | |
7b3810db JH |
94 | break; |
95 | ||
de1d99bb JH |
96 | case SR_CONF_RLE: |
97 | bind_bool(name, key); | |
98 | break; | |
99 | ||
4d5d5d6a BV |
100 | case SR_CONF_TIMEBASE: |
101 | bind_enum(name, key, gvar_list, print_timebase); | |
7b3810db JH |
102 | break; |
103 | ||
4d5d5d6a BV |
104 | case SR_CONF_VDIV: |
105 | bind_enum(name, key, gvar_list, print_vdiv); | |
7b3810db | 106 | break; |
62874984 MC |
107 | |
108 | case SR_CONF_VOLTAGE_THRESHOLD: | |
109 | bind_enum(name, key, gvar_list, print_voltage_threshold); | |
110 | break; | |
7b3810db | 111 | } |
4d5d5d6a | 112 | |
de1d99bb JH |
113 | if (gvar_list) |
114 | g_variant_unref(gvar_list); | |
7b3810db | 115 | } |
4d5d5d6a | 116 | g_variant_unref(gvar_opts); |
7b3810db JH |
117 | } |
118 | ||
4d5d5d6a BV |
119 | GVariant* DeviceOptions::config_getter( |
120 | const struct sr_dev_inst *sdi, int key) | |
7b3810db | 121 | { |
4d5d5d6a | 122 | GVariant *data = NULL; |
68162c29 | 123 | if (sr_config_get(sdi->driver, sdi, NULL, key, &data) != SR_OK) { |
4d5d5d6a BV |
124 | qDebug() << |
125 | "WARNING: Failed to get value of config id" << key; | |
126 | return NULL; | |
127 | } | |
128 | return data; | |
7b3810db JH |
129 | } |
130 | ||
4d5d5d6a BV |
131 | void DeviceOptions::config_setter( |
132 | const struct sr_dev_inst *sdi, int key, GVariant* value) | |
7b3810db | 133 | { |
68162c29 | 134 | if (sr_config_set(sdi, NULL, key, value) != SR_OK) |
4d5d5d6a | 135 | qDebug() << "WARNING: Failed to set value of sample rate"; |
7b3810db JH |
136 | } |
137 | ||
de1d99bb JH |
138 | void DeviceOptions::bind_bool(const QString &name, int key) |
139 | { | |
140 | _properties.push_back(shared_ptr<Property>( | |
141 | new Bool(name, bind(config_getter, _sdi, key), | |
142 | bind(config_setter, _sdi, key, _1)))); | |
143 | } | |
144 | ||
4d5d5d6a BV |
145 | void DeviceOptions::bind_enum(const QString &name, int key, |
146 | GVariant *const gvar_list, function<QString (GVariant*)> printer) | |
9f6af8bd | 147 | { |
4d5d5d6a BV |
148 | GVariant *gvar; |
149 | GVariantIter iter; | |
150 | vector< pair<GVariant*, QString> > values; | |
9f6af8bd | 151 | |
de1d99bb JH |
152 | assert(gvar_list); |
153 | ||
4d5d5d6a BV |
154 | g_variant_iter_init (&iter, gvar_list); |
155 | while ((gvar = g_variant_iter_next_value (&iter))) | |
156 | values.push_back(make_pair(gvar, printer(gvar))); | |
9f6af8bd | 157 | |
4d5d5d6a BV |
158 | _properties.push_back(shared_ptr<Property>( |
159 | new Enum(name, values, | |
160 | bind(config_getter, _sdi, key), | |
161 | bind(config_setter, _sdi, key, _1)))); | |
9f6af8bd JH |
162 | } |
163 | ||
a2b92157 JH |
164 | void DeviceOptions::bind_int(const QString &name, int key, QString suffix, |
165 | optional< std::pair<int64_t, int64_t> > range) | |
166 | { | |
167 | _properties.push_back(shared_ptr<Property>( | |
168 | new Int(name, suffix, range, | |
169 | bind(config_getter, _sdi, key), | |
170 | bind(config_setter, _sdi, key, _1)))); | |
171 | } | |
172 | ||
4d5d5d6a | 173 | QString DeviceOptions::print_gvariant(GVariant *const gvar) |
7b3810db | 174 | { |
4d5d5d6a BV |
175 | QString s; |
176 | ||
177 | if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s"))) | |
27e8df22 | 178 | s = QString::fromUtf8(g_variant_get_string(gvar, NULL)); |
4d5d5d6a BV |
179 | else |
180 | { | |
181 | gchar *const text = g_variant_print(gvar, FALSE); | |
27e8df22 | 182 | s = QString::fromUtf8(text); |
4d5d5d6a BV |
183 | g_free(text); |
184 | } | |
7b3810db | 185 | |
4d5d5d6a | 186 | return s; |
7b3810db JH |
187 | } |
188 | ||
4d5d5d6a BV |
189 | QString DeviceOptions::print_timebase(GVariant *const gvar) |
190 | { | |
191 | uint64_t p, q; | |
192 | g_variant_get(gvar, "(tt)", &p, &q); | |
27e8df22 | 193 | return QString::fromUtf8(sr_period_string(p * q)); |
4d5d5d6a BV |
194 | } |
195 | ||
196 | QString DeviceOptions::print_vdiv(GVariant *const gvar) | |
197 | { | |
198 | uint64_t p, q; | |
199 | g_variant_get(gvar, "(tt)", &p, &q); | |
27e8df22 | 200 | return QString::fromUtf8(sr_voltage_string(p, q)); |
9f6af8bd JH |
201 | } |
202 | ||
62874984 MC |
203 | QString DeviceOptions::print_voltage_threshold(GVariant *const gvar) |
204 | { | |
205 | gdouble lo, hi; | |
206 | char buf[64]; | |
207 | g_variant_get(gvar, "(dd)", &lo, &hi); | |
208 | snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi); | |
27e8df22 | 209 | return QString::fromUtf8(buf); |
62874984 MC |
210 | } |
211 | ||
cf47ef78 JH |
212 | } // binding |
213 | } // prop | |
214 | } // pv | |
215 |