]>
Commit | Line | Data |
---|---|---|
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 <cassert> | |
22 | ||
23 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
24 | ||
25 | #include <QLabel> | |
26 | ||
27 | #include "connect.hpp" | |
28 | ||
29 | #include <pv/devicemanager.hpp> | |
30 | #include <pv/devices/hardwaredevice.hpp> | |
31 | ||
32 | using std::list; | |
33 | using std::map; | |
34 | using std::shared_ptr; | |
35 | using std::string; | |
36 | ||
37 | using Glib::ustring; | |
38 | using Glib::Variant; | |
39 | using Glib::VariantBase; | |
40 | ||
41 | using sigrok::ConfigKey; | |
42 | using sigrok::Driver; | |
43 | using sigrok::Error; | |
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 | tcp_endpoint_(&form_), | |
59 | tcp_endpoint_layout_(&tcp_endpoint_), | |
60 | tcp_host_(&tcp_endpoint_), | |
61 | tcp_port_(&tcp_endpoint_), | |
62 | scan_button_(tr("&Scan for devices using driver above"), this), | |
63 | device_list_(this), | |
64 | button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, | |
65 | Qt::Horizontal, this) | |
66 | { | |
67 | setWindowTitle(tr("Connect to Device")); | |
68 | ||
69 | connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept())); | |
70 | connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject())); | |
71 | ||
72 | populate_drivers(); | |
73 | connect(&drivers_, SIGNAL(activated(int)), | |
74 | this, SLOT(device_selected(int))); | |
75 | ||
76 | form_.setLayout(&form_layout_); | |
77 | form_layout_.addRow(tr("&Driver"), &drivers_); | |
78 | ||
79 | form_layout_.addRow(tr("Serial &Port"), &serial_devices_); | |
80 | serial_devices_.setEditable(true); | |
81 | ||
82 | tcp_host_.setPlaceholderText("192.168.1.100"); | |
83 | tcp_endpoint_layout_.addWidget(&tcp_host_); | |
84 | tcp_endpoint_layout_.addWidget(new QLabel(":")); | |
85 | tcp_port_.setRange(1, 65535); | |
86 | tcp_port_.setValue(5555); | |
87 | tcp_endpoint_layout_.addWidget(&tcp_port_); | |
88 | tcp_endpoint_layout_.setContentsMargins(0, 0, 0, 0); | |
89 | form_layout_.addRow(tr("TCP &Endpoint"), &tcp_endpoint_); | |
90 | ||
91 | unset_connection(); | |
92 | ||
93 | connect(&scan_button_, SIGNAL(pressed()), | |
94 | this, SLOT(scan_pressed())); | |
95 | ||
96 | setLayout(&layout_); | |
97 | layout_.addWidget(&form_); | |
98 | layout_.addWidget(&scan_button_); | |
99 | layout_.addWidget(&device_list_); | |
100 | layout_.addWidget(&button_box_); | |
101 | } | |
102 | ||
103 | shared_ptr<HardwareDevice> Connect::get_selected_device() const | |
104 | { | |
105 | const QListWidgetItem *const item = device_list_.currentItem(); | |
106 | if (!item) | |
107 | return shared_ptr<HardwareDevice>(); | |
108 | ||
109 | return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>(); | |
110 | } | |
111 | ||
112 | void Connect::populate_drivers() | |
113 | { | |
114 | for (auto entry : device_manager_.context()->drivers()) { | |
115 | auto name = entry.first; | |
116 | auto driver = entry.second; | |
117 | /** | |
118 | * We currently only support devices that can deliver | |
119 | * samples at a fixed samplerate i.e. oscilloscopes and | |
120 | * logic analysers. | |
121 | * @todo Add support for non-monotonic devices i.e. DMMs | |
122 | * and sensors. | |
123 | */ | |
124 | const auto keys = driver->config_keys(); | |
125 | ||
126 | bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) | | |
127 | keys.count(ConfigKey::OSCILLOSCOPE); | |
128 | ||
129 | if (supported_device) | |
130 | drivers_.addItem(QString("%1 (%2)").arg( | |
131 | driver->long_name().c_str(), name.c_str()), | |
132 | qVariantFromValue(driver)); | |
133 | } | |
134 | } | |
135 | ||
136 | void Connect::populate_serials(shared_ptr<Driver> driver) | |
137 | { | |
138 | serial_devices_.clear(); | |
139 | for (auto serial : device_manager_.context()->serials(driver)) | |
140 | serial_devices_.addItem(QString("%1 (%2)").arg( | |
141 | serial.first.c_str(), serial.second.c_str()), | |
142 | QString::fromStdString(serial.first)); | |
143 | } | |
144 | ||
145 | void Connect::unset_connection() | |
146 | { | |
147 | device_list_.clear(); | |
148 | serial_devices_.hide(); | |
149 | form_layout_.labelForField(&serial_devices_)->hide(); | |
150 | tcp_endpoint_.hide(); | |
151 | form_layout_.labelForField(&tcp_endpoint_)->hide(); | |
152 | button_box_.button(QDialogButtonBox::Ok)->setDisabled(true); | |
153 | } | |
154 | ||
155 | void Connect::set_serial_connection(shared_ptr<Driver> driver) | |
156 | { | |
157 | populate_serials(driver); | |
158 | serial_devices_.show(); | |
159 | form_layout_.labelForField(&serial_devices_)->show(); | |
160 | } | |
161 | ||
162 | void Connect::set_tcp_connection(shared_ptr<Driver> driver) | |
163 | { | |
164 | (void)driver; | |
165 | ||
166 | tcp_endpoint_.show(); | |
167 | form_layout_.labelForField(&tcp_endpoint_)->show(); | |
168 | } | |
169 | ||
170 | void Connect::scan_pressed() | |
171 | { | |
172 | device_list_.clear(); | |
173 | ||
174 | const int index = drivers_.currentIndex(); | |
175 | if (index == -1) | |
176 | return; | |
177 | ||
178 | shared_ptr<Driver> driver = | |
179 | drivers_.itemData(index).value<shared_ptr<Driver>>(); | |
180 | ||
181 | assert(driver); | |
182 | ||
183 | map<const ConfigKey *, VariantBase> drvopts; | |
184 | ||
185 | if (serial_devices_.isVisible()) { | |
186 | QString serial; | |
187 | const int index = serial_devices_.currentIndex(); | |
188 | if (index >= 0 && index < serial_devices_.count() && | |
189 | serial_devices_.currentText() == serial_devices_.itemText(index)) | |
190 | serial = serial_devices_.itemData(index).value<QString>(); | |
191 | else | |
192 | serial = serial_devices_.currentText(); | |
193 | drvopts[ConfigKey::CONN] = Variant<ustring>::create( | |
194 | serial.toUtf8().constData()); | |
195 | } | |
196 | ||
197 | if (tcp_endpoint_.isVisible()) { | |
198 | QString host = tcp_host_.text(); | |
199 | QString port = tcp_port_.text(); | |
200 | if(!host.isEmpty()) { | |
201 | QString conn = QString("tcp-raw/%1/%2").arg(host, port); | |
202 | drvopts[ConfigKey::CONN] = Variant<ustring>::create( | |
203 | conn.toUtf8().constData()); | |
204 | } | |
205 | } | |
206 | ||
207 | const list< shared_ptr<HardwareDevice> > devices = | |
208 | device_manager_.driver_scan(driver, drvopts); | |
209 | ||
210 | for (shared_ptr<HardwareDevice> device : devices) { | |
211 | assert(device); | |
212 | ||
213 | QString text = QString::fromStdString( | |
214 | device->display_name(device_manager_)); | |
215 | text += QString(" with %1 channels").arg( | |
216 | device->device()->channels().size()); | |
217 | ||
218 | QListWidgetItem *const item = new QListWidgetItem(text, | |
219 | &device_list_); | |
220 | item->setData(Qt::UserRole, qVariantFromValue(device)); | |
221 | device_list_.addItem(item); | |
222 | } | |
223 | ||
224 | device_list_.setCurrentRow(0); | |
225 | button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0); | |
226 | } | |
227 | ||
228 | void Connect::device_selected(int index) | |
229 | { | |
230 | shared_ptr<Driver> driver = | |
231 | drivers_.itemData(index).value<shared_ptr<Driver>>(); | |
232 | ||
233 | unset_connection(); | |
234 | ||
235 | if (driver->scan_options().count(ConfigKey::SERIALCOMM)) | |
236 | set_serial_connection(driver); | |
237 | ||
238 | if (driver->name() == "rigol-ds") // NBNB | |
239 | set_tcp_connection(driver); | |
240 | } | |
241 | ||
242 | } // namespace dialogs | |
243 | } // namespace pv |