]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
9663c82b JH |
18 | */ |
19 | ||
f9abf97e JH |
20 | #include <cassert> |
21 | ||
fe3a1c21 | 22 | #include <libsigrokcxx/libsigrokcxx.hpp> |
19adbc2c | 23 | |
1ebe875c | 24 | #include <QGroupBox> |
aca9aa83 | 25 | #include <QLabel> |
1ebe875c | 26 | #include <QRadioButton> |
26f209b7 | 27 | |
2acdb232 | 28 | #include "connect.hpp" |
9663c82b | 29 | |
da30ecb7 JH |
30 | #include <pv/devicemanager.hpp> |
31 | #include <pv/devices/hardwaredevice.hpp> | |
9663c82b | 32 | |
819f4c25 | 33 | using std::list; |
e8d00928 | 34 | using std::map; |
f9abf97e | 35 | using std::shared_ptr; |
819f4c25 | 36 | using std::string; |
107ca6d3 | 37 | |
e8d00928 ML |
38 | using Glib::ustring; |
39 | using Glib::Variant; | |
40 | using Glib::VariantBase; | |
41 | ||
42 | using sigrok::ConfigKey; | |
43 | using sigrok::Driver; | |
da30ecb7 JH |
44 | |
45 | using pv::devices::HardwareDevice; | |
c9de8b32 | 46 | |
9663c82b JH |
47 | namespace pv { |
48 | namespace dialogs { | |
49 | ||
107ca6d3 | 50 | Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) : |
9663c82b | 51 | QDialog(parent), |
8dbbc7f0 JH |
52 | device_manager_(device_manager), |
53 | layout_(this), | |
54 | form_(this), | |
55 | form_layout_(&form_), | |
56 | drivers_(&form_), | |
34750abe | 57 | serial_devices_(&form_), |
be7645e8 | 58 | scan_button_(tr("&Scan for devices using driver above"), this), |
8dbbc7f0 JH |
59 | device_list_(this), |
60 | button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, | |
9663c82b JH |
61 | Qt::Horizontal, this) |
62 | { | |
63 | setWindowTitle(tr("Connect to Device")); | |
64 | ||
8dbbc7f0 JH |
65 | connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept())); |
66 | connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject())); | |
9663c82b JH |
67 | |
68 | populate_drivers(); | |
1ebe875c | 69 | connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int))); |
9663c82b | 70 | |
8dbbc7f0 | 71 | form_.setLayout(&form_layout_); |
9663c82b | 72 | |
1ebe875c SA |
73 | QVBoxLayout *vbox_drv = new QVBoxLayout; |
74 | vbox_drv->addWidget(&drivers_); | |
75 | QGroupBox *groupbox_drv = new QGroupBox(tr("Step 1: Choose the driver")); | |
76 | groupbox_drv->setLayout(vbox_drv); | |
77 | form_layout_.addRow(groupbox_drv); | |
78 | ||
79 | QRadioButton *radiobtn_usb = new QRadioButton(tr("&USB"), this); | |
80 | QRadioButton *radiobtn_serial = new QRadioButton(tr("Serial &Port"), this); | |
81 | QRadioButton *radiobtn_tcp = new QRadioButton(tr("&TCP/IP"), this); | |
9663c82b | 82 | |
1ebe875c SA |
83 | radiobtn_usb->setChecked(true); |
84 | ||
85 | serial_devices_.setEditable(true); | |
86 | serial_devices_.setEnabled(false); | |
87 | ||
88 | tcp_config_ = new QWidget(); | |
89 | QHBoxLayout *tcp_config_layout = new QHBoxLayout(tcp_config_); | |
90 | tcp_host_ = new QLineEdit; | |
91 | tcp_host_->setText("192.168.1.100"); | |
92 | tcp_config_layout->addWidget(tcp_host_); | |
93 | tcp_config_layout->addWidget(new QLabel(":")); | |
94 | tcp_port_ = new QSpinBox; | |
95 | tcp_port_->setRange(1, 65535); | |
96 | tcp_port_->setValue(5555); | |
97 | tcp_config_layout->addWidget(tcp_port_); | |
98 | tcp_use_vxi_ = new QCheckBox(); | |
99 | tcp_use_vxi_->setText(tr("Use VXI")); | |
100 | tcp_config_layout->addSpacing(30); | |
101 | tcp_config_layout->addWidget(tcp_use_vxi_); | |
102 | tcp_config_layout->setContentsMargins(0, 0, 0, 0); | |
103 | tcp_config_->setEnabled(false); | |
104 | ||
105 | QVBoxLayout *vbox_if = new QVBoxLayout; | |
106 | vbox_if->addWidget(radiobtn_usb); | |
107 | vbox_if->addWidget(radiobtn_serial); | |
108 | vbox_if->addWidget(&serial_devices_); | |
109 | vbox_if->addWidget(radiobtn_tcp); | |
110 | vbox_if->addWidget(tcp_config_); | |
111 | ||
112 | QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface")); | |
113 | groupbox_if->setLayout(vbox_if); | |
114 | form_layout_.addRow(groupbox_if); | |
115 | ||
116 | QVBoxLayout *vbox_scan = new QVBoxLayout; | |
117 | vbox_scan->addWidget(&scan_button_); | |
118 | QGroupBox *groupbox_scan = new QGroupBox(tr("Step 3: Scan for devices")); | |
119 | groupbox_scan->setLayout(vbox_scan); | |
120 | form_layout_.addRow(groupbox_scan); | |
121 | ||
122 | QVBoxLayout *vbox_select = new QVBoxLayout; | |
123 | vbox_select->addWidget(&device_list_); | |
124 | QGroupBox *groupbox_select = new QGroupBox(tr("Step 4: Select the device")); | |
125 | groupbox_select->setLayout(vbox_select); | |
126 | form_layout_.addRow(groupbox_select); | |
26f209b7 | 127 | |
9663c82b JH |
128 | unset_connection(); |
129 | ||
1ebe875c SA |
130 | connect(radiobtn_serial, SIGNAL(toggled(bool)), this, SLOT(serial_toggled(bool))); |
131 | connect(radiobtn_tcp, SIGNAL(toggled(bool)), this, SLOT(tcp_toggled(bool))); | |
132 | connect(&scan_button_, SIGNAL(pressed()), this, SLOT(scan_pressed())); | |
c9de8b32 | 133 | |
8dbbc7f0 | 134 | setLayout(&layout_); |
1ebe875c | 135 | |
8dbbc7f0 | 136 | layout_.addWidget(&form_); |
8dbbc7f0 | 137 | layout_.addWidget(&button_box_); |
9663c82b JH |
138 | } |
139 | ||
e8d00928 | 140 | shared_ptr<HardwareDevice> Connect::get_selected_device() const |
5eb0fa13 | 141 | { |
8dbbc7f0 | 142 | const QListWidgetItem *const item = device_list_.currentItem(); |
5eb0fa13 | 143 | if (!item) |
e8d00928 | 144 | return shared_ptr<HardwareDevice>(); |
19adbc2c | 145 | |
e8d00928 | 146 | return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>(); |
5eb0fa13 JH |
147 | } |
148 | ||
9663c82b JH |
149 | void Connect::populate_drivers() |
150 | { | |
8dbbc7f0 | 151 | for (auto entry : device_manager_.context()->drivers()) { |
e8d00928 ML |
152 | auto name = entry.first; |
153 | auto driver = entry.second; | |
30938303 JH |
154 | /** |
155 | * We currently only support devices that can deliver | |
156 | * samples at a fixed samplerate i.e. oscilloscopes and | |
157 | * logic analysers. | |
158 | * @todo Add support for non-monotonic devices i.e. DMMs | |
159 | * and sensors. | |
160 | */ | |
7bb0fbf4 ML |
161 | const auto keys = driver->config_keys(); |
162 | ||
163 | bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) | | |
164 | keys.count(ConfigKey::OSCILLOSCOPE); | |
30938303 | 165 | |
793f8096 | 166 | if (supported_device) |
8dbbc7f0 | 167 | drivers_.addItem(QString("%1 (%2)").arg( |
744aa24f | 168 | driver->long_name().c_str(), name.c_str()), |
e8d00928 | 169 | qVariantFromValue(driver)); |
30938303 | 170 | } |
9663c82b JH |
171 | } |
172 | ||
34750abe AJ |
173 | void Connect::populate_serials(shared_ptr<Driver> driver) |
174 | { | |
175 | serial_devices_.clear(); | |
176 | for (auto serial : device_manager_.context()->serials(driver)) | |
177 | serial_devices_.addItem(QString("%1 (%2)").arg( | |
744aa24f | 178 | serial.first.c_str(), serial.second.c_str()), |
34750abe AJ |
179 | QString::fromStdString(serial.first)); |
180 | } | |
181 | ||
c9de8b32 JH |
182 | void Connect::unset_connection() |
183 | { | |
8dbbc7f0 | 184 | device_list_.clear(); |
8dbbc7f0 | 185 | button_box_.button(QDialogButtonBox::Ok)->setDisabled(true); |
c9de8b32 JH |
186 | } |
187 | ||
1ebe875c | 188 | void Connect::serial_toggled(bool checked) |
c9de8b32 | 189 | { |
1ebe875c | 190 | serial_devices_.setEnabled(checked); |
c9de8b32 JH |
191 | } |
192 | ||
1ebe875c | 193 | void Connect::tcp_toggled(bool checked) |
26f209b7 | 194 | { |
1ebe875c | 195 | tcp_config_->setEnabled(checked); |
26f209b7 | 196 | } |
197 | ||
c9de8b32 JH |
198 | void Connect::scan_pressed() |
199 | { | |
8dbbc7f0 | 200 | device_list_.clear(); |
c9de8b32 | 201 | |
8dbbc7f0 | 202 | const int index = drivers_.currentIndex(); |
793f8096 | 203 | if (index == -1) |
c9de8b32 JH |
204 | return; |
205 | ||
e8d00928 | 206 | shared_ptr<Driver> driver = |
8dbbc7f0 | 207 | drivers_.itemData(index).value<shared_ptr<Driver>>(); |
c9de8b32 | 208 | |
e8d00928 | 209 | assert(driver); |
c9de8b32 | 210 | |
e8d00928 | 211 | map<const ConfigKey *, VariantBase> drvopts; |
c9de8b32 | 212 | |
1ebe875c | 213 | if (serial_devices_.isEnabled()) { |
34750abe AJ |
214 | QString serial; |
215 | const int index = serial_devices_.currentIndex(); | |
216 | if (index >= 0 && index < serial_devices_.count() && | |
217 | serial_devices_.currentText() == serial_devices_.itemText(index)) | |
218 | serial = serial_devices_.itemData(index).value<QString>(); | |
219 | else | |
220 | serial = serial_devices_.currentText(); | |
e8d00928 | 221 | drvopts[ConfigKey::CONN] = Variant<ustring>::create( |
34750abe AJ |
222 | serial.toUtf8().constData()); |
223 | } | |
c9de8b32 | 224 | |
1ebe875c SA |
225 | if (tcp_config_->isEnabled()) { |
226 | QString host = tcp_host_->text(); | |
227 | QString port = tcp_port_->text(); | |
228 | if (!host.isEmpty()) { | |
229 | QString conn; | |
230 | if (tcp_use_vxi_->isChecked()) | |
231 | conn = QString("vxi/%1/%2").arg(host, port); | |
232 | else | |
233 | conn = QString("tcp-raw/%1/%2").arg(host, port); | |
234 | ||
26f209b7 | 235 | drvopts[ConfigKey::CONN] = Variant<ustring>::create( |
236 | conn.toUtf8().constData()); | |
237 | } | |
238 | } | |
239 | ||
da30ecb7 | 240 | const list< shared_ptr<HardwareDevice> > devices = |
8dbbc7f0 | 241 | device_manager_.driver_scan(driver, drvopts); |
c9de8b32 | 242 | |
2ad82c2e | 243 | for (shared_ptr<HardwareDevice> device : devices) { |
e8d00928 | 244 | assert(device); |
19adbc2c | 245 | |
e8d00928 | 246 | QString text = QString::fromStdString( |
3084ed4b | 247 | device->display_name(device_manager_)); |
da30ecb7 JH |
248 | text += QString(" with %1 channels").arg( |
249 | device->device()->channels().size()); | |
c9de8b32 | 250 | |
5eb0fa13 | 251 | QListWidgetItem *const item = new QListWidgetItem(text, |
8dbbc7f0 | 252 | &device_list_); |
e8d00928 | 253 | item->setData(Qt::UserRole, qVariantFromValue(device)); |
8dbbc7f0 | 254 | device_list_.addItem(item); |
c9de8b32 JH |
255 | } |
256 | ||
8dbbc7f0 JH |
257 | device_list_.setCurrentRow(0); |
258 | button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0); | |
c9de8b32 JH |
259 | } |
260 | ||
1ebe875c | 261 | void Connect::driver_selected(int index) |
9663c82b | 262 | { |
e8d00928 | 263 | shared_ptr<Driver> driver = |
8dbbc7f0 | 264 | drivers_.itemData(index).value<shared_ptr<Driver>>(); |
9663c82b JH |
265 | |
266 | unset_connection(); | |
267 | ||
1ebe875c | 268 | populate_serials(driver); |
9663c82b JH |
269 | } |
270 | ||
9663c82b JH |
271 | } // namespace dialogs |
272 | } // namespace pv |