X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=pv%2Fdialogs%2Fconnect.cpp;h=19dfde8d1a3f227065e39cd1499b2300212f3d8a;hb=e8d009288de28cb194bc7964f96677c2baf900c9;hp=284ce07240c2a279d247252aac081cab66333d07;hpb=3093830343c9d3b3220c2874a942c843d7a05cf7;p=pulseview.git diff --git a/pv/dialogs/connect.cpp b/pv/dialogs/connect.cpp index 284ce072..19dfde8d 100644 --- a/pv/dialogs/connect.cpp +++ b/pv/dialogs/connect.cpp @@ -18,25 +18,41 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + +#include + #include "connect.h" -extern "C" { -/* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ -#define __STDC_FORMAT_MACROS -#include -#include -} +#include "pv/devicemanager.h" + +using std::list; +using std::map; +using std::shared_ptr; +using std::string; + +using Glib::ustring; +using Glib::Variant; +using Glib::VariantBase; + +using sigrok::ConfigKey; +using sigrok::Driver; +using sigrok::Error; +using sigrok::HardwareDevice; namespace pv { namespace dialogs { -Connect::Connect(QWidget *parent) : +Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) : QDialog(parent), + _device_manager(device_manager), _layout(this), _form(this), _form_layout(&_form), _drivers(&_form), _serial_device(&_form), + _scan_button(tr("Scan for Devices"), this), + _device_list(this), _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this) { @@ -56,16 +72,30 @@ Connect::Connect(QWidget *parent) : unset_connection(); + connect(&_scan_button, SIGNAL(pressed()), + this, SLOT(scan_pressed())); + setLayout(&_layout); _layout.addWidget(&_form); + _layout.addWidget(&_scan_button); + _layout.addWidget(&_device_list); _layout.addWidget(&_button_box); } +shared_ptr Connect::get_selected_device() const +{ + const QListWidgetItem *const item = _device_list.currentItem(); + if (!item) + return shared_ptr(); + + return item->data(Qt::UserRole).value>(); +} + void Connect::populate_drivers() { - const int *hwopts; - struct sr_dev_driver **drivers = sr_driver_list(); - for (int i = 0; drivers[i]; ++i) { + for (auto entry : _device_manager.context()->drivers()) { + auto name = entry.first; + auto driver = entry.second; /** * We currently only support devices that can deliver * samples at a fixed samplerate i.e. oscilloscopes and @@ -73,53 +103,22 @@ void Connect::populate_drivers() * @todo Add support for non-monotonic devices i.e. DMMs * and sensors. */ - bool supported_device = false; - if ((sr_config_list(drivers[i], SR_CONF_DEVICE_OPTIONS, - (const void **)&hwopts, NULL) == SR_OK) && hwopts) - for (int j = 0; hwopts[j]; j++) - if(hwopts[j] == SR_CONF_SAMPLERATE) { - supported_device = true; - break; - } - - if(supported_device) - _drivers.addItem(QString("%1 (%2)").arg( - drivers[i]->longname).arg(drivers[i]->name), - qVariantFromValue((void*)drivers[i])); - } -} - -void Connect::device_selected(int index) -{ - const int *hwopts; - const struct sr_hwcap_option *hwo; - sr_dev_driver *const driver = (sr_dev_driver*)_drivers.itemData( - index).value(); - - unset_connection(); - - if ((sr_config_list(driver, SR_CONF_SCAN_OPTIONS, - (const void **)&hwopts, NULL) == SR_OK) && hwopts) { - - for (int i = 0; hwopts[i]; i++) { - switch(hwopts[i]) { - case SR_CONF_SERIALCOMM: - set_serial_connection(); - break; - - default: - continue; - } + bool supported_device = driver->config_check( + ConfigKey::SAMPLERATE, ConfigKey::DEVICE_OPTIONS); - break; - } + if (supported_device) + _drivers.addItem(QString("%1 (%2)").arg( + driver->long_name().c_str()).arg(name.c_str()), + qVariantFromValue(driver)); } } void Connect::unset_connection() { + _device_list.clear(); _serial_device.hide(); _form_layout.labelForField(&_serial_device)->hide(); + _button_box.button(QDialogButtonBox::Ok)->setDisabled(true); } void Connect::set_serial_connection() @@ -128,6 +127,56 @@ void Connect::set_serial_connection() _form_layout.labelForField(&_serial_device)->show(); } +void Connect::scan_pressed() +{ + _device_list.clear(); + + const int index = _drivers.currentIndex(); + if (index == -1) + return; + + shared_ptr driver = + _drivers.itemData(index).value>(); + + assert(driver); + + map drvopts; + + if (_serial_device.isVisible()) + drvopts[ConfigKey::CONN] = Variant::create( + _serial_device.text().toUtf8().constData()); + + list< shared_ptr > devices = + _device_manager.driver_scan(driver, drvopts); + + for (shared_ptr device : devices) + { + assert(device); + + QString text = QString::fromStdString( + _device_manager.device_description(device)); + text += QString(" with %1 channels").arg(device->channels().size()); + + QListWidgetItem *const item = new QListWidgetItem(text, + &_device_list); + item->setData(Qt::UserRole, qVariantFromValue(device)); + _device_list.addItem(item); + } + + _device_list.setCurrentRow(0); + _button_box.button(QDialogButtonBox::Ok)->setDisabled(_device_list.count() == 0); +} + +void Connect::device_selected(int index) +{ + shared_ptr driver = + _drivers.itemData(index).value>(); + + unset_connection(); + + if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS)) + set_serial_connection(); +} } // namespace dialogs } // namespace pv