]> sigrok.org Git - pulseview.git/blob - pv/prop/binding/deviceoptions.cpp
9db50dd0548f55c7fd2eeb11bd7ddd52846cc83f
[pulseview.git] / pv / prop / binding / deviceoptions.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 <boost/bind.hpp>
22
23 #include <QDebug>
24 #include <QObject>
25
26 #include "deviceoptions.h"
27
28 #include <pv/prop/double.h>
29 #include <pv/prop/enum.h>
30
31 using namespace boost;
32 using namespace std;
33
34 namespace pv {
35 namespace prop {
36 namespace binding {
37
38 DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) :
39         _sdi(sdi)
40 {
41         const int *options;
42
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. */
46                 return;
47
48         for (int cap = 0; options[cap]; cap++) {
49                 const struct sr_config_info *const info =
50                         sr_config_info_get(options[cap]);
51
52                 if (!info)
53                         continue;
54
55                 switch(info->key)
56                 {
57                 case SR_CONF_SAMPLERATE:
58                         bind_samplerate(info);
59                         break;
60
61                 case SR_CONF_PATTERN_MODE:
62                         bind_stropt(info, SR_CONF_PATTERN_MODE);
63                         break;
64
65                 case SR_CONF_BUFFERSIZE:
66                         bind_buffer_size(info);
67                         break;
68
69                 case SR_CONF_TIMEBASE:
70                         bind_time_base(info);
71                         break;
72
73                 case SR_CONF_TRIGGER_SOURCE:
74                         bind_stropt(info, SR_CONF_TRIGGER_SOURCE);
75                         break;
76
77                 case SR_CONF_FILTER:
78                         bind_stropt(info, SR_CONF_FILTER);
79                         break;
80
81                 case SR_CONF_VDIV:
82                         bind_vdiv(info);
83                         break;
84
85                 case SR_CONF_COUPLING:
86                         bind_stropt(info, SR_CONF_FILTER);
87                         break;
88                 }
89         }
90 }
91
92 void DeviceOptions::expose_enum(const struct sr_config_info *info,
93         const vector< pair<const void*, QString> > &values, int key)
94 {
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))));
99 }
100
101 void DeviceOptions::bind_stropt(
102         const struct sr_config_info *info, int key)
103 {
104         const char **stropts;
105         if (sr_config_list(_sdi->driver, key,
106                 (const void **)&stropts, _sdi) != SR_OK)
107                 return;
108
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]));
112
113         expose_enum(info, values, key);
114 }
115
116 void DeviceOptions::bind_samplerate(const struct sr_config_info *info)
117 {
118         const struct sr_samplerates *samplerates;
119
120         if (sr_config_list(_sdi->driver, SR_CONF_SAMPLERATE,
121                 (const void **)&samplerates, _sdi) != SR_OK)
122                 return;
123
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))));
133         } else {
134                 vector< pair<const void*, QString> > values;
135                 for (const uint64_t *rate = samplerates->list;
136                         *rate; rate++)
137                         values.push_back(make_pair(
138                                 (const void*)rate,
139                                 QString(sr_samplerate_string(*rate))));
140
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))));
145         }
146 }
147
148 void DeviceOptions::bind_buffer_size(const struct sr_config_info *info)
149 {
150         const uint64_t *sizes;
151         if (sr_config_list(_sdi->driver, SR_CONF_BUFFERSIZE,
152                         (const void **)&sizes, _sdi) != SR_OK)
153                 return;
154
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])));
159
160         expose_enum(info, values, SR_CONF_BUFFERSIZE);
161 }
162
163 void DeviceOptions::bind_time_base(const struct sr_config_info *info)
164 {
165         struct sr_rational *timebases;
166         if (sr_config_list(_sdi->driver, SR_CONF_TIMEBASE,
167                         (const void **)&timebases, _sdi) != SR_OK)
168                 return;
169
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))));
175
176         expose_enum(info, values, SR_CONF_TIMEBASE);
177 }
178
179 void DeviceOptions::bind_vdiv(const struct sr_config_info *info)
180 {
181         struct sr_rational *vdivs;
182         if (sr_config_list(_sdi->driver, SR_CONF_VDIV,
183                         (const void **)&vdivs, _sdi) != SR_OK)
184                 return;
185
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))));
190
191         expose_enum(info, values, SR_CONF_VDIV);
192 }
193
194 const void* DeviceOptions::enum_getter(
195         const struct sr_dev_inst *sdi, int key)
196 {
197         const void *data = NULL;
198         if(sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) {
199                 qDebug() <<
200                         "WARNING: Failed to get value of config id" << key;
201                 return NULL;
202         }
203         return data;
204 }
205
206 double DeviceOptions::samplerate_value_getter(
207         const struct sr_dev_inst *sdi)
208 {
209         uint64_t *samplerate = NULL;
210         if(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
211                 (const void**)&samplerate, sdi) != SR_OK) {
212                 qDebug() <<
213                         "WARNING: Failed to get value of sample rate";
214                 return 0.0;
215         }
216         return (double)*samplerate;
217 }
218
219 void DeviceOptions::samplerate_value_setter(
220         struct sr_dev_inst *sdi, double value)
221 {
222         uint64_t samplerate = value;
223         if(sr_config_set(sdi, SR_CONF_SAMPLERATE,
224                 &samplerate) != SR_OK)
225                 qDebug() <<
226                         "WARNING: Failed to set value of sample rate";
227 }
228
229 const void* DeviceOptions::samplerate_list_getter(
230         const struct sr_dev_inst *sdi)
231 {
232         const struct sr_samplerates *samplerates;
233         uint64_t *samplerate = NULL;
234
235         if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
236                 (const void **)&samplerates, sdi) != SR_OK) {
237                 qDebug() <<
238                         "WARNING: Failed to get enumerate sample rates";
239                 return NULL;
240         }
241
242         if(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
243                 (const void**)&samplerate, sdi) != SR_OK ||
244                 !samplerate) {
245                 qDebug() <<
246                         "WARNING: Failed to get value of sample rate";
247                 return NULL;
248         }
249
250         for (const uint64_t *rate = samplerates->list; *rate; rate++)
251                 if(*rate == *samplerate)
252                         return (const void*)rate;
253
254         return NULL;
255 }
256
257 void DeviceOptions::samplerate_list_setter(
258         struct sr_dev_inst *sdi, const void *value)
259 {
260         if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
261                 (uint64_t*)value) != SR_OK)
262                 qDebug() <<
263                         "WARNING: Failed to set value of sample rate";
264 }
265
266 } // binding
267 } // prop
268 } // pv
269