]>
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 | ||
4d5d5d6a BV |
23 | #include <stdint.h> |
24 | ||
48c88445 JH |
25 | #include <QDebug> |
26 | ||
3820592a | 27 | #include "deviceoptions.h" |
cf47ef78 | 28 | |
94574501 | 29 | #include <pv/device/devinst.h> |
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 | |
19adbc2c JH |
35 | #include <libsigrok/libsigrok.h> |
36 | ||
819f4c25 JH |
37 | using boost::bind; |
38 | using boost::function; | |
39 | using boost::optional; | |
40 | using boost::shared_ptr; | |
41 | using std::make_pair; | |
42 | using std::pair; | |
43 | using std::string; | |
44 | using std::vector; | |
7b3810db | 45 | |
cf47ef78 JH |
46 | namespace pv { |
47 | namespace prop { | |
48 | namespace binding { | |
49 | ||
94574501 | 50 | DeviceOptions::DeviceOptions(shared_ptr<pv::device::DevInst> dev_inst, |
2445160a | 51 | const sr_channel_group *group) : |
19adbc2c | 52 | _dev_inst(dev_inst), |
941c1ba7 | 53 | _group(group) |
cf47ef78 | 54 | { |
19adbc2c | 55 | assert(dev_inst); |
95237c18 | 56 | |
8dd44190 | 57 | GVariant *gvar_opts; |
4d5d5d6a | 58 | gsize num_opts; |
7b3810db | 59 | |
8dd44190 | 60 | if (!(gvar_opts = dev_inst->list_config(group, SR_CONF_DEVICE_OPTIONS))) |
7b3810db JH |
61 | /* Driver supports no device instance options. */ |
62 | return; | |
63 | ||
4d5d5d6a BV |
64 | const int *const options = (const int32_t *)g_variant_get_fixed_array( |
65 | gvar_opts, &num_opts, sizeof(int32_t)); | |
66 | for (unsigned int i = 0; i < num_opts; i++) { | |
10b1be92 | 67 | const struct sr_config_info *const info = |
4d5d5d6a BV |
68 | sr_config_info_get(options[i]); |
69 | ||
de1d99bb JH |
70 | if (!info) |
71 | continue; | |
72 | ||
4d5d5d6a | 73 | const int key = info->key; |
8dd44190 | 74 | GVariant *const gvar_list = dev_inst->list_config(group, key); |
7b3810db | 75 | |
27e8df22 | 76 | const QString name = QString::fromUtf8(info->name); |
4d5d5d6a BV |
77 | |
78 | switch(key) | |
7b3810db | 79 | { |
9f6af8bd | 80 | case SR_CONF_SAMPLERATE: |
117ef009 JH |
81 | // Sample rate values are not bound because they are shown |
82 | // in the SamplingBar | |
9f6af8bd JH |
83 | break; |
84 | ||
a2b92157 | 85 | case SR_CONF_CAPTURE_RATIO: |
c6dd7ed2 | 86 | bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100)); |
a2b92157 JH |
87 | break; |
88 | ||
10b1be92 | 89 | case SR_CONF_PATTERN_MODE: |
10b1be92 | 90 | case SR_CONF_BUFFERSIZE: |
10b1be92 | 91 | case SR_CONF_TRIGGER_SOURCE: |
5be76b1a | 92 | case SR_CONF_TRIGGER_SLOPE: |
10b1be92 | 93 | case SR_CONF_FILTER: |
4d5d5d6a | 94 | case SR_CONF_COUPLING: |
5be76b1a | 95 | case SR_CONF_CLOCK_EDGE: |
4d5d5d6a | 96 | bind_enum(name, key, gvar_list); |
7b3810db JH |
97 | break; |
98 | ||
5be76b1a | 99 | case SR_CONF_EXTERNAL_CLOCK: |
de1d99bb JH |
100 | case SR_CONF_RLE: |
101 | bind_bool(name, key); | |
102 | break; | |
103 | ||
4d5d5d6a BV |
104 | case SR_CONF_TIMEBASE: |
105 | bind_enum(name, key, gvar_list, print_timebase); | |
7b3810db JH |
106 | break; |
107 | ||
4d5d5d6a BV |
108 | case SR_CONF_VDIV: |
109 | bind_enum(name, key, gvar_list, print_vdiv); | |
7b3810db | 110 | break; |
62874984 MC |
111 | |
112 | case SR_CONF_VOLTAGE_THRESHOLD: | |
113 | bind_enum(name, key, gvar_list, print_voltage_threshold); | |
114 | break; | |
7b3810db | 115 | } |
4d5d5d6a | 116 | |
de1d99bb JH |
117 | if (gvar_list) |
118 | g_variant_unref(gvar_list); | |
7b3810db | 119 | } |
4d5d5d6a | 120 | g_variant_unref(gvar_opts); |
7b3810db JH |
121 | } |
122 | ||
de1d99bb JH |
123 | void DeviceOptions::bind_bool(const QString &name, int key) |
124 | { | |
8dd44190 JH |
125 | assert(_dev_inst); |
126 | _properties.push_back(shared_ptr<Property>(new Bool(name, | |
94574501 JH |
127 | bind(&device::DevInst::get_config, _dev_inst, _group, key), |
128 | bind(&device::DevInst::set_config, _dev_inst, | |
129 | _group, key, _1)))); | |
de1d99bb JH |
130 | } |
131 | ||
4d5d5d6a BV |
132 | void DeviceOptions::bind_enum(const QString &name, int key, |
133 | GVariant *const gvar_list, function<QString (GVariant*)> printer) | |
9f6af8bd | 134 | { |
4d5d5d6a BV |
135 | GVariant *gvar; |
136 | GVariantIter iter; | |
137 | vector< pair<GVariant*, QString> > values; | |
9f6af8bd | 138 | |
8dd44190 | 139 | assert(_dev_inst); |
48c88445 JH |
140 | if (!gvar_list) { |
141 | qDebug() << "Config key " << key << " was listed, but no " | |
142 | "options were given"; | |
143 | return; | |
144 | } | |
de1d99bb | 145 | |
4d5d5d6a BV |
146 | g_variant_iter_init (&iter, gvar_list); |
147 | while ((gvar = g_variant_iter_next_value (&iter))) | |
148 | values.push_back(make_pair(gvar, printer(gvar))); | |
9f6af8bd | 149 | |
8dd44190 | 150 | _properties.push_back(shared_ptr<Property>(new Enum(name, values, |
94574501 JH |
151 | bind(&device::DevInst::get_config, _dev_inst, _group, key), |
152 | bind(&device::DevInst::set_config, _dev_inst, | |
153 | _group, key, _1)))); | |
9f6af8bd JH |
154 | } |
155 | ||
a2b92157 JH |
156 | void DeviceOptions::bind_int(const QString &name, int key, QString suffix, |
157 | optional< std::pair<int64_t, int64_t> > range) | |
158 | { | |
8dd44190 | 159 | assert(_dev_inst); |
19adbc2c | 160 | |
8dd44190 | 161 | _properties.push_back(shared_ptr<Property>(new Int(name, suffix, range, |
94574501 JH |
162 | bind(&device::DevInst::get_config, _dev_inst, _group, key), |
163 | bind(&device::DevInst::set_config, _dev_inst, _group, key, _1)))); | |
a2b92157 JH |
164 | } |
165 | ||
4d5d5d6a BV |
166 | QString DeviceOptions::print_timebase(GVariant *const gvar) |
167 | { | |
168 | uint64_t p, q; | |
169 | g_variant_get(gvar, "(tt)", &p, &q); | |
27e8df22 | 170 | return QString::fromUtf8(sr_period_string(p * q)); |
4d5d5d6a BV |
171 | } |
172 | ||
173 | QString DeviceOptions::print_vdiv(GVariant *const gvar) | |
174 | { | |
175 | uint64_t p, q; | |
176 | g_variant_get(gvar, "(tt)", &p, &q); | |
27e8df22 | 177 | return QString::fromUtf8(sr_voltage_string(p, q)); |
9f6af8bd JH |
178 | } |
179 | ||
62874984 MC |
180 | QString DeviceOptions::print_voltage_threshold(GVariant *const gvar) |
181 | { | |
182 | gdouble lo, hi; | |
183 | char buf[64]; | |
184 | g_variant_get(gvar, "(dd)", &lo, &hi); | |
185 | snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi); | |
27e8df22 | 186 | return QString::fromUtf8(buf); |
62874984 MC |
187 | } |
188 | ||
cf47ef78 JH |
189 | } // binding |
190 | } // prop | |
191 | } // pv | |
192 |