]> sigrok.org Git - pulseview.git/blob - pv/dialogs/connect.cpp
Connect dialog: add baud rate combobox for serial ports
[pulseview.git] / pv / dialogs / connect.cpp
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <cassert>
21
22 #include <libsigrokcxx/libsigrokcxx.hpp>
23
24 #include <QGroupBox>
25 #include <QLabel>
26 #include <QRadioButton>
27
28 #include "connect.hpp"
29
30 #include <pv/devicemanager.hpp>
31 #include <pv/devices/hardwaredevice.hpp>
32
33 using std::list;
34 using std::map;
35 using std::shared_ptr;
36 using std::string;
37
38 using Glib::ustring;
39 using Glib::Variant;
40 using Glib::VariantBase;
41
42 using sigrok::ConfigKey;
43 using sigrok::Driver;
44
45 using pv::devices::HardwareDevice;
46
47 namespace pv {
48 namespace dialogs {
49
50 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
51         QDialog(parent),
52         device_manager_(device_manager),
53         layout_(this),
54         form_(this),
55         form_layout_(&form_),
56         drivers_(&form_),
57         serial_devices_(&form_),
58         scan_button_(tr("&Scan for devices using driver above"), this),
59         device_list_(this),
60         button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
61                 Qt::Horizontal, this)
62 {
63         setWindowTitle(tr("Connect to Device"));
64
65         connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
66         connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
67
68         populate_drivers();
69         connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int)));
70
71         form_.setLayout(&form_layout_);
72
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);
82
83         radiobtn_usb->setChecked(true);
84
85         serial_config_ = new QWidget();
86         QHBoxLayout *serial_config_layout = new QHBoxLayout(serial_config_);
87
88         serial_devices_.setEditable(true);
89         serial_devices_.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
90
91         serial_baudrate_.setEditable(true);
92         serial_baudrate_.addItem("");
93         serial_baudrate_.addItem("921600");
94         serial_baudrate_.addItem("115200");
95         serial_baudrate_.addItem("57600");
96         serial_baudrate_.addItem("19200");
97         serial_baudrate_.addItem("9600");
98
99         serial_config_layout->addWidget(&serial_devices_);
100         serial_config_layout->addWidget(&serial_baudrate_);
101         serial_config_layout->addWidget(new QLabel("baud"));
102
103         tcp_config_ = new QWidget();
104         QHBoxLayout *tcp_config_layout = new QHBoxLayout(tcp_config_);
105         tcp_host_ = new QLineEdit;
106         tcp_host_->setText("192.168.1.100");
107         tcp_config_layout->addWidget(tcp_host_);
108         tcp_config_layout->addWidget(new QLabel(":"));
109         tcp_port_ = new QSpinBox;
110         tcp_port_->setRange(1, 65535);
111         tcp_port_->setValue(5555);
112         tcp_config_layout->addWidget(tcp_port_);
113
114         tcp_config_layout->addSpacing(30);
115         tcp_config_layout->addWidget(new QLabel(tr("Protocol:")));
116         tcp_protocol_ = new QComboBox();
117         tcp_protocol_->addItem("Raw TCP", QVariant("tcp-raw/%1/%2"));
118         tcp_protocol_->addItem("VXI", QVariant("vxi/%1/%2"));
119         tcp_config_layout->addWidget(tcp_protocol_);
120         tcp_config_layout->setContentsMargins(0, 0, 0, 0);
121         tcp_config_->setEnabled(false);
122
123         // Let the device list occupy only the minimum space needed
124         device_list_.setMaximumHeight(device_list_.minimumSizeHint().height());
125
126         QVBoxLayout *vbox_if = new QVBoxLayout;
127         vbox_if->addWidget(radiobtn_usb);
128         vbox_if->addWidget(radiobtn_serial);
129         vbox_if->addWidget(serial_config_);
130         vbox_if->addWidget(radiobtn_tcp);
131         vbox_if->addWidget(tcp_config_);
132
133         QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface"));
134         groupbox_if->setLayout(vbox_if);
135         form_layout_.addRow(groupbox_if);
136
137         QVBoxLayout *vbox_scan = new QVBoxLayout;
138         vbox_scan->addWidget(&scan_button_);
139         QGroupBox *groupbox_scan = new QGroupBox(tr("Step 3: Scan for devices"));
140         groupbox_scan->setLayout(vbox_scan);
141         form_layout_.addRow(groupbox_scan);
142
143         QVBoxLayout *vbox_select = new QVBoxLayout;
144         vbox_select->addWidget(&device_list_);
145         QGroupBox *groupbox_select = new QGroupBox(tr("Step 4: Select the device"));
146         groupbox_select->setLayout(vbox_select);
147         form_layout_.addRow(groupbox_select);
148
149         unset_connection();
150
151         connect(radiobtn_serial, SIGNAL(toggled(bool)), this, SLOT(serial_toggled(bool)));
152         connect(radiobtn_tcp, SIGNAL(toggled(bool)), this, SLOT(tcp_toggled(bool)));
153         connect(&scan_button_, SIGNAL(pressed()), this, SLOT(scan_pressed()));
154
155         setLayout(&layout_);
156
157         layout_.addWidget(&form_);
158         layout_.addWidget(&button_box_);
159 }
160
161 shared_ptr<HardwareDevice> Connect::get_selected_device() const
162 {
163         const QListWidgetItem *const item = device_list_.currentItem();
164         if (!item)
165                 return shared_ptr<HardwareDevice>();
166
167         return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
168 }
169
170 void Connect::populate_drivers()
171 {
172         for (auto& entry : device_manager_.context()->drivers()) {
173                 auto name = entry.first;
174                 auto driver = entry.second;
175                 /**
176                  * We currently only support devices that can deliver
177                  * samples at a fixed samplerate i.e. oscilloscopes and
178                  * logic analysers.
179                  * @todo Add support for non-monotonic devices i.e. DMMs
180                  * and sensors.
181                  */
182                 const auto keys = driver->config_keys();
183
184                 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
185                         keys.count(ConfigKey::OSCILLOSCOPE);
186
187                 if (supported_device)
188                         drivers_.addItem(QString("%1 (%2)").arg(
189                                 driver->long_name().c_str(), name.c_str()),
190                                 qVariantFromValue(driver));
191         }
192 }
193
194 void Connect::populate_serials(shared_ptr<Driver> driver)
195 {
196         serial_devices_.clear();
197         for (auto& serial : device_manager_.context()->serials(driver))
198                 serial_devices_.addItem(QString("%1 (%2)").arg(
199                         serial.first.c_str(), serial.second.c_str()),
200                         QString::fromStdString(serial.first));
201 }
202
203 void Connect::unset_connection()
204 {
205         device_list_.clear();
206         button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
207 }
208
209 void Connect::serial_toggled(bool checked)
210 {
211         serial_devices_.setEnabled(checked);
212         serial_baudrate_.setEnabled(checked);
213 }
214
215 void Connect::tcp_toggled(bool checked)
216 {
217         tcp_config_->setEnabled(checked);
218 }
219
220 void Connect::scan_pressed()
221 {
222         device_list_.clear();
223
224         const int index = drivers_.currentIndex();
225         if (index == -1)
226                 return;
227
228         shared_ptr<Driver> driver =
229                 drivers_.itemData(index).value<shared_ptr<Driver>>();
230
231         assert(driver);
232
233         map<const ConfigKey *, VariantBase> drvopts;
234
235         if (serial_config_->isEnabled()) {
236                 QString serial;
237                 const int index = serial_devices_.currentIndex();
238                 if (index >= 0 && index < serial_devices_.count() &&
239                     serial_devices_.currentText() == serial_devices_.itemText(index))
240                         serial = serial_devices_.itemData(index).toString();
241                 else
242                         serial = serial_devices_.currentText();
243
244                 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
245                         serial.toUtf8().constData());
246
247                 // Set baud rate if specified
248                 if (serial_baudrate_.currentText().length() > 0)
249                         drvopts[ConfigKey::SERIALCOMM] = Variant<ustring>::create(
250                                 QString("%1/8n1").arg(serial_baudrate_.currentText()).toUtf8().constData());
251         }
252
253         if (tcp_config_->isEnabled()) {
254                 QString host = tcp_host_->text();
255                 QString port = tcp_port_->text();
256                 if (!host.isEmpty()) {
257                         QString conn =
258                                 tcp_protocol_->itemData(tcp_protocol_->currentIndex()).toString();
259
260                         conn = conn.arg(host, port);
261
262                         drvopts[ConfigKey::CONN] = Variant<ustring>::create(
263                                 conn.toUtf8().constData());
264                 }
265         }
266
267         const list< shared_ptr<HardwareDevice> > devices =
268                 device_manager_.driver_scan(driver, drvopts);
269
270         for (const shared_ptr<HardwareDevice>& device : devices) {
271                 assert(device);
272
273                 QString text = QString::fromStdString(device->display_name(device_manager_));
274                 text += QString(" with %1 channels").arg(device->device()->channels().size());
275
276                 QListWidgetItem *const item = new QListWidgetItem(text, &device_list_);
277                 item->setData(Qt::UserRole, qVariantFromValue(device));
278                 device_list_.addItem(item);
279         }
280
281         device_list_.setCurrentRow(0);
282         button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
283 }
284
285 void Connect::driver_selected(int index)
286 {
287         shared_ptr<Driver> driver =
288                 drivers_.itemData(index).value<shared_ptr<Driver>>();
289
290         unset_connection();
291
292         populate_serials(driver);
293 }
294
295 } // namespace dialogs
296 } // namespace pv