2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <boost/bind.hpp>
26 #include "deviceoptions.h"
28 #include <pv/prop/double.h>
29 #include <pv/prop/enum.h>
31 using namespace boost;
38 DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) :
43 if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS,
44 (const void **)&options, sdi) != SR_OK) || !options)
45 /* Driver supports no device instance options. */
48 for (int cap = 0; options[cap]; cap++) {
49 const struct sr_config_info *const info =
50 sr_config_info_get(options[cap]);
57 case SR_CONF_SAMPLERATE:
58 bind_samplerate(info);
61 case SR_CONF_PATTERN_MODE:
62 bind_stropt(info, SR_CONF_PATTERN_MODE);
65 case SR_CONF_BUFFERSIZE:
66 bind_buffer_size(info);
69 case SR_CONF_TIMEBASE:
73 case SR_CONF_TRIGGER_SOURCE:
74 bind_stropt(info, SR_CONF_TRIGGER_SOURCE);
78 bind_stropt(info, SR_CONF_FILTER);
85 case SR_CONF_COUPLING:
86 bind_stropt(info, SR_CONF_FILTER);
92 void DeviceOptions::expose_enum(const struct sr_config_info *info,
93 const vector< pair<const void*, QString> > &values, int key)
95 _properties.push_back(shared_ptr<Property>(
96 new Enum(QString(info->name), values,
97 bind(enum_getter, _sdi, key),
98 bind(sr_config_set, _sdi, key, _1))));
101 void DeviceOptions::bind_stropt(
102 const struct sr_config_info *info, int key)
104 const char **stropts;
105 if (sr_config_list(_sdi->driver, key,
106 (const void **)&stropts, _sdi) != SR_OK)
109 vector< pair<const void*, QString> > values;
110 for (int i = 0; stropts[i]; i++)
111 values.push_back(make_pair(stropts[i], stropts[i]));
113 expose_enum(info, values, key);
116 void DeviceOptions::bind_samplerate(const struct sr_config_info *info)
118 const struct sr_samplerates *samplerates;
120 if (sr_config_list(_sdi->driver, SR_CONF_SAMPLERATE,
121 (const void **)&samplerates, _sdi) != SR_OK)
124 if (samplerates->step) {
125 _properties.push_back(shared_ptr<Property>(
126 new Double(QString(info->name),
127 0, QObject::tr("Hz"),
128 make_pair((double)samplerates->low,
129 (double)samplerates->high),
130 (double)samplerates->step,
131 bind(samplerate_value_getter, _sdi),
132 bind(samplerate_value_setter, _sdi, _1))));
134 vector< pair<const void*, QString> > values;
135 for (const uint64_t *rate = samplerates->list;
137 values.push_back(make_pair(
139 QString(sr_samplerate_string(*rate))));
141 _properties.push_back(shared_ptr<Property>(
142 new Enum(QString(info->name), values,
143 bind(samplerate_list_getter, _sdi),
144 bind(samplerate_list_setter, _sdi, _1))));
148 void DeviceOptions::bind_buffer_size(const struct sr_config_info *info)
150 const uint64_t *sizes;
151 if (sr_config_list(_sdi->driver, SR_CONF_BUFFERSIZE,
152 (const void **)&sizes, _sdi) != SR_OK)
155 vector< pair<const void*, QString> > values;
156 for (int i = 0; sizes[i]; i++)
157 values.push_back(make_pair(sizes + i,
158 QString("%1").arg(sizes[i])));
160 expose_enum(info, values, SR_CONF_BUFFERSIZE);
163 void DeviceOptions::bind_time_base(const struct sr_config_info *info)
165 struct sr_rational *timebases;
166 if (sr_config_list(_sdi->driver, SR_CONF_TIMEBASE,
167 (const void **)&timebases, _sdi) != SR_OK)
170 vector< pair<const void*, QString> > values;
171 for (int i = 0; timebases[i].p && timebases[i].q; i++)
172 values.push_back(make_pair(timebases + i,
173 QString(sr_period_string(
174 timebases[i].p * timebases[i].q))));
176 expose_enum(info, values, SR_CONF_TIMEBASE);
179 void DeviceOptions::bind_vdiv(const struct sr_config_info *info)
181 struct sr_rational *vdivs;
182 if (sr_config_list(_sdi->driver, SR_CONF_VDIV,
183 (const void **)&vdivs, _sdi) != SR_OK)
186 vector< pair<const void*, QString> > values;
187 for (int i = 0; vdivs[i].p && vdivs[i].q; i++)
188 values.push_back(make_pair(vdivs + i,
189 QString(sr_voltage_string(vdivs + i))));
191 expose_enum(info, values, SR_CONF_VDIV);
194 const void* DeviceOptions::enum_getter(
195 const struct sr_dev_inst *sdi, int key)
197 const void *data = NULL;
198 if(sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
200 "WARNING: Failed to get value of config id" << key;
206 double DeviceOptions::samplerate_value_getter(
207 const struct sr_dev_inst *sdi)
209 uint64_t *samplerate = NULL;
210 if(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
211 (const void**)&samplerate, sdi) != SR_OK) {
213 "WARNING: Failed to get value of sample rate";
216 return (double)*samplerate;
219 void DeviceOptions::samplerate_value_setter(
220 struct sr_dev_inst *sdi, double value)
222 uint64_t samplerate = value;
223 if(sr_config_set(sdi, SR_CONF_SAMPLERATE,
224 &samplerate) != SR_OK)
226 "WARNING: Failed to set value of sample rate";
229 const void* DeviceOptions::samplerate_list_getter(
230 const struct sr_dev_inst *sdi)
232 const struct sr_samplerates *samplerates;
233 uint64_t *samplerate = NULL;
235 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
236 (const void **)&samplerates, sdi) != SR_OK) {
238 "WARNING: Failed to get enumerate sample rates";
242 if(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
243 (const void**)&samplerate, sdi) != SR_OK ||
246 "WARNING: Failed to get value of sample rate";
250 for (const uint64_t *rate = samplerates->list; *rate; rate++)
251 if(*rate == *samplerate)
252 return (const void*)rate;
257 void DeviceOptions::samplerate_list_setter(
258 struct sr_dev_inst *sdi, const void *value)
260 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
261 (uint64_t*)value) != SR_OK)
263 "WARNING: Failed to set value of sample rate";