]>
Commit | Line | Data |
---|---|---|
9663c82b JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012-2013 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 "connect.h" | |
22 | ||
23 | extern "C" { | |
24 | /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ | |
25 | #define __STDC_FORMAT_MACROS | |
26 | #include <glib.h> | |
27 | #include <libsigrok/libsigrok.h> | |
28 | } | |
29 | ||
c9de8b32 JH |
30 | extern sr_context *sr_ctx; |
31 | ||
9663c82b JH |
32 | namespace pv { |
33 | namespace dialogs { | |
34 | ||
35 | Connect::Connect(QWidget *parent) : | |
36 | QDialog(parent), | |
37 | _layout(this), | |
38 | _form(this), | |
39 | _form_layout(&_form), | |
40 | _drivers(&_form), | |
41 | _serial_device(&_form), | |
c9de8b32 JH |
42 | _scan_button(tr("Scan for Devices"), this), |
43 | _device_list(this), | |
9663c82b JH |
44 | _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, |
45 | Qt::Horizontal, this) | |
46 | { | |
47 | setWindowTitle(tr("Connect to Device")); | |
48 | ||
49 | connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept())); | |
50 | connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject())); | |
51 | ||
52 | populate_drivers(); | |
53 | connect(&_drivers, SIGNAL(activated(int)), | |
54 | this, SLOT(device_selected(int))); | |
55 | ||
56 | _form.setLayout(&_form_layout); | |
57 | _form_layout.addRow(tr("Driver"), &_drivers); | |
58 | ||
59 | _form_layout.addRow(tr("Serial Port"), &_serial_device); | |
60 | ||
61 | unset_connection(); | |
62 | ||
c9de8b32 JH |
63 | connect(&_scan_button, SIGNAL(pressed()), |
64 | this, SLOT(scan_pressed())); | |
65 | ||
9663c82b JH |
66 | setLayout(&_layout); |
67 | _layout.addWidget(&_form); | |
c9de8b32 JH |
68 | _layout.addWidget(&_scan_button); |
69 | _layout.addWidget(&_device_list); | |
9663c82b JH |
70 | _layout.addWidget(&_button_box); |
71 | } | |
72 | ||
5eb0fa13 JH |
73 | struct sr_dev_inst* Connect::get_selected_device() const |
74 | { | |
75 | const QListWidgetItem *const item = _device_list.currentItem(); | |
76 | if (!item) | |
77 | return NULL; | |
78 | ||
79 | return (sr_dev_inst*)item->data(Qt::UserRole).value<void*>(); | |
80 | } | |
81 | ||
9663c82b JH |
82 | void Connect::populate_drivers() |
83 | { | |
30938303 | 84 | const int *hwopts; |
9663c82b | 85 | struct sr_dev_driver **drivers = sr_driver_list(); |
30938303 JH |
86 | for (int i = 0; drivers[i]; ++i) { |
87 | /** | |
88 | * We currently only support devices that can deliver | |
89 | * samples at a fixed samplerate i.e. oscilloscopes and | |
90 | * logic analysers. | |
91 | * @todo Add support for non-monotonic devices i.e. DMMs | |
92 | * and sensors. | |
93 | */ | |
94 | bool supported_device = false; | |
95 | if ((sr_config_list(drivers[i], SR_CONF_DEVICE_OPTIONS, | |
96 | (const void **)&hwopts, NULL) == SR_OK) && hwopts) | |
97 | for (int j = 0; hwopts[j]; j++) | |
98 | if(hwopts[j] == SR_CONF_SAMPLERATE) { | |
99 | supported_device = true; | |
100 | break; | |
101 | } | |
102 | ||
103 | if(supported_device) | |
104 | _drivers.addItem(QString("%1 (%2)").arg( | |
105 | drivers[i]->longname).arg(drivers[i]->name), | |
106 | qVariantFromValue((void*)drivers[i])); | |
107 | } | |
9663c82b JH |
108 | } |
109 | ||
c9de8b32 JH |
110 | void Connect::unset_connection() |
111 | { | |
112 | _device_list.clear(); | |
113 | _serial_device.hide(); | |
114 | _form_layout.labelForField(&_serial_device)->hide(); | |
f60ec39f | 115 | _button_box.button(QDialogButtonBox::Ok)->setDisabled(true); |
c9de8b32 JH |
116 | } |
117 | ||
118 | void Connect::set_serial_connection() | |
119 | { | |
120 | _serial_device.show(); | |
121 | _form_layout.labelForField(&_serial_device)->show(); | |
122 | } | |
123 | ||
124 | void Connect::scan_pressed() | |
125 | { | |
126 | _device_list.clear(); | |
127 | ||
128 | const int index = _drivers.currentIndex(); | |
129 | if(index == -1) | |
130 | return; | |
131 | ||
132 | sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData( | |
133 | index).value<void*>(); | |
134 | ||
135 | GSList *drvopts = NULL; | |
136 | ||
137 | if (_serial_device.isVisible()) { | |
138 | sr_config *const src = (sr_config*)g_try_malloc(sizeof(sr_config)); | |
139 | src->key = SR_CONF_CONN; | |
140 | const QByteArray byteArray = _serial_device.text().toUtf8(); | |
141 | src->value = g_strdup((const gchar*)byteArray.constData()); | |
142 | drvopts = g_slist_append(drvopts, src); | |
143 | } | |
144 | ||
145 | GSList *const devices = sr_driver_scan(driver, drvopts); | |
146 | ||
147 | for (GSList *l = devices; l; l = l->next) { | |
148 | ||
149 | sr_dev_inst *const sdi = (sr_dev_inst*)l->data; | |
150 | ||
151 | QString text; | |
152 | if (sdi->vendor && sdi->vendor[0]) | |
153 | text += QString("%1 ").arg(sdi->vendor); | |
154 | if (sdi->model && sdi->model[0]) | |
155 | text += QString("%1 ").arg(sdi->model); | |
156 | if (sdi->version && sdi->version[0]) | |
157 | text += QString("%1 ").arg(sdi->version); | |
158 | if (sdi->probes) { | |
159 | text += QString("with %1 probes").arg( | |
160 | g_slist_length(sdi->probes)); | |
161 | } | |
162 | ||
5eb0fa13 JH |
163 | QListWidgetItem *const item = new QListWidgetItem(text, |
164 | &_device_list); | |
165 | item->setData(Qt::UserRole, qVariantFromValue((void*)sdi)); | |
166 | _device_list.addItem(item); | |
c9de8b32 JH |
167 | } |
168 | ||
169 | g_slist_free(devices); | |
170 | g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); | |
f60ec39f JH |
171 | |
172 | _device_list.setCurrentRow(0); | |
173 | _button_box.button(QDialogButtonBox::Ok)->setDisabled(false); | |
c9de8b32 JH |
174 | } |
175 | ||
9663c82b JH |
176 | void Connect::device_selected(int index) |
177 | { | |
178 | const int *hwopts; | |
9663c82b JH |
179 | sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData( |
180 | index).value<void*>(); | |
181 | ||
182 | unset_connection(); | |
183 | ||
184 | if ((sr_config_list(driver, SR_CONF_SCAN_OPTIONS, | |
185 | (const void **)&hwopts, NULL) == SR_OK) && hwopts) { | |
186 | ||
187 | for (int i = 0; hwopts[i]; i++) { | |
188 | switch(hwopts[i]) { | |
189 | case SR_CONF_SERIALCOMM: | |
190 | set_serial_connection(); | |
191 | break; | |
192 | ||
193 | default: | |
194 | continue; | |
195 | } | |
196 | ||
197 | break; | |
198 | } | |
199 | } | |
200 | } | |
201 | ||
c9de8b32 | 202 | void Connect::free_drvopts(struct sr_config *src) |
9663c82b | 203 | { |
c9de8b32 JH |
204 | g_free((void *)src->value); |
205 | g_free(src); | |
9663c82b JH |
206 | } |
207 | ||
9663c82b JH |
208 | } // namespace dialogs |
209 | } // namespace pv |