]> sigrok.org Git - pulseview.git/blob - pv/dialogs/connect.cpp
284ce07240c2a279d247252aac081cab66333d07
[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, 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
30 namespace pv {
31 namespace dialogs {
32
33 Connect::Connect(QWidget *parent) :
34         QDialog(parent),
35         _layout(this),
36         _form(this),
37         _form_layout(&_form),
38         _drivers(&_form),
39         _serial_device(&_form),
40         _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
41                 Qt::Horizontal, this)
42 {
43         setWindowTitle(tr("Connect to Device"));
44
45         connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept()));
46         connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject()));
47
48         populate_drivers();
49         connect(&_drivers, SIGNAL(activated(int)),
50                 this, SLOT(device_selected(int)));
51
52         _form.setLayout(&_form_layout);
53         _form_layout.addRow(tr("Driver"), &_drivers);
54
55         _form_layout.addRow(tr("Serial Port"), &_serial_device);
56
57         unset_connection();
58
59         setLayout(&_layout);
60         _layout.addWidget(&_form);
61         _layout.addWidget(&_button_box);
62 }
63
64 void Connect::populate_drivers()
65 {
66         const int *hwopts;
67         struct sr_dev_driver **drivers = sr_driver_list();
68         for (int i = 0; drivers[i]; ++i) {
69                 /**
70                  * We currently only support devices that can deliver
71                  * samples at a fixed samplerate i.e. oscilloscopes and
72                  * logic analysers.
73                  * @todo Add support for non-monotonic devices i.e. DMMs
74                  * and sensors.
75                  */
76                 bool supported_device = false;
77                 if ((sr_config_list(drivers[i], SR_CONF_DEVICE_OPTIONS,
78                         (const void **)&hwopts, NULL) == SR_OK) && hwopts)
79                         for (int j = 0; hwopts[j]; j++)
80                                 if(hwopts[j] == SR_CONF_SAMPLERATE) {
81                                         supported_device = true;
82                                         break;
83                                 }
84
85                 if(supported_device)
86                         _drivers.addItem(QString("%1 (%2)").arg(
87                                 drivers[i]->longname).arg(drivers[i]->name),
88                                 qVariantFromValue((void*)drivers[i]));
89         }
90 }
91
92 void Connect::device_selected(int index)
93 {
94         const int *hwopts;
95         const struct sr_hwcap_option *hwo;
96         sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData(
97                 index).value<void*>();
98
99         unset_connection();
100
101         if ((sr_config_list(driver, SR_CONF_SCAN_OPTIONS,
102                 (const void **)&hwopts, NULL) == SR_OK) && hwopts) {
103
104                 for (int i = 0; hwopts[i]; i++) {
105                         switch(hwopts[i]) {
106                         case SR_CONF_SERIALCOMM:
107                                 set_serial_connection();
108                                 break;
109
110                         default:
111                                 continue;
112                         }
113
114                         break;
115                 }
116         }
117 }
118
119 void Connect::unset_connection()
120 {
121         _serial_device.hide();
122         _form_layout.labelForField(&_serial_device)->hide();
123 }
124
125 void Connect::set_serial_connection()
126 {
127         _serial_device.show();
128         _form_layout.labelForField(&_serial_device)->show();
129 }
130
131
132 } // namespace dialogs
133 } // namespace pv