]> sigrok.org Git - pulseview.git/blob - pv/prop/binding/hwcap.cpp
Ported pv::prop::binding::HwCap to new sigrok API
[pulseview.git] / pv / prop / binding / hwcap.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 "hwcap.h"
24
25 #include <pv/prop/enum.h>
26
27 using namespace boost;
28 using namespace std;
29
30 namespace pv {
31 namespace prop {
32 namespace binding {
33
34 HwCap::HwCap(struct sr_dev_inst *sdi) :
35         _sdi(sdi)
36 {
37         const int *options;
38
39         if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS,
40                 (const void **)&options, sdi) != SR_OK) || !options)
41                 /* Driver supports no device instance options. */
42                 return;
43
44         for (int cap = 0; options[cap]; cap++) {
45                 const struct sr_config_info *const info =
46                         sr_config_info_get(options[cap]);
47
48                 if (!info)
49                         continue;
50
51                 switch(info->key)
52                 {
53                 case SR_CONF_PATTERN_MODE:
54                         bind_stropt(info, SR_CONF_PATTERN_MODE);
55                         break;
56
57                 case SR_CONF_BUFFERSIZE:
58                         bind_buffer_size(info);
59                         break;
60
61                 case SR_CONF_TIMEBASE:
62                         bind_time_base(info);
63                         break;
64
65                 case SR_CONF_TRIGGER_SOURCE:
66                         bind_stropt(info, SR_CONF_TRIGGER_SOURCE);
67                         break;
68
69                 case SR_CONF_FILTER:
70                         bind_stropt(info, SR_CONF_FILTER);
71                         break;
72
73                 case SR_CONF_VDIV:
74                         bind_vdiv(info);
75                         break;
76
77                 case SR_CONF_COUPLING:
78                         bind_stropt(info, SR_CONF_FILTER);
79                         break;
80                 }
81         }
82 }
83
84 void HwCap::expose_enum(const struct sr_config_info *info,
85         const vector< pair<const void*, QString> > &values, int key)
86 {
87         _properties.push_back(shared_ptr<Property>(
88                 new Enum(QString(info->name), values,
89                         function<const void* ()>(),
90                         bind(sr_config_set, _sdi, key, _1))));
91 }
92
93 void HwCap::bind_stropt(const struct sr_config_info *info, int key)
94 {
95         const char **stropts;
96         if (sr_config_list(_sdi->driver, key,
97                 (const void **)&stropts, _sdi) != SR_OK)
98                 return;
99
100         vector< pair<const void*, QString> > values;
101         for (int i = 0; stropts[i]; i++)
102                 values.push_back(make_pair(stropts[i], stropts[i]));
103
104         expose_enum(info, values, key);
105 }
106
107 void HwCap::bind_buffer_size(const struct sr_config_info *info)
108 {
109         const uint64_t *sizes;
110         if (sr_config_list(_sdi->driver, SR_CONF_BUFFERSIZE,
111                         (const void **)&sizes, _sdi) != SR_OK)
112                 return;
113
114         vector< pair<const void*, QString> > values;
115         for (int i = 0; sizes[i]; i++)
116                 values.push_back(make_pair(sizes + i,
117                         QString("%1").arg(sizes[i])));
118
119         expose_enum(info, values, SR_CONF_BUFFERSIZE);
120 }
121
122 void HwCap::bind_time_base(const struct sr_config_info *info)
123 {
124         struct sr_rational *timebases;
125         if (sr_config_list(_sdi->driver, SR_CONF_TIMEBASE,
126                         (const void **)&timebases, _sdi) != SR_OK)
127                 return;
128
129         vector< pair<const void*, QString> > values;
130         for (int i = 0; timebases[i].p && timebases[i].q; i++)
131                 values.push_back(make_pair(timebases + i,
132                         QString(sr_period_string(
133                                 timebases[i].p * timebases[i].q))));
134
135         expose_enum(info, values, SR_CONF_TIMEBASE);
136 }
137
138 void HwCap::bind_vdiv(const struct sr_config_info *info)
139 {
140         struct sr_rational *vdivs;
141         if (sr_config_list(_sdi->driver, SR_CONF_VDIV,
142                         (const void **)&vdivs, _sdi) != SR_OK)
143                 return;
144
145         vector< pair<const void*, QString> > values;
146         for (int i = 0; vdivs[i].p && vdivs[i].q; i++)
147                 values.push_back(make_pair(vdivs + i,
148                         QString(sr_voltage_string(vdivs + i))));
149
150         expose_enum(info, values, SR_CONF_VDIV);
151 }
152
153 } // binding
154 } // prop
155 } // pv
156