X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=samplingbar.cpp;h=711f6f4eae92ebc265d63c1d321f8d81f61cb391;hp=8159a69a4a67c0b17f09d869f6d80608dac3da2f;hb=cdf7bea7f0eead18de2616cbfd8697ac0fffb59b;hpb=d4984fe7119c2fc9bf94b13cc38cc735887e377d diff --git a/samplingbar.cpp b/samplingbar.cpp index 8159a69a..711f6f4e 100644 --- a/samplingbar.cpp +++ b/samplingbar.cpp @@ -18,9 +18,171 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + +#include + +extern "C" { +#include +} + +#include + #include "samplingbar.h" +const uint64_t SamplingBar::RecordLengths[11] = { + 1000000, + 2000000, + 5000000, + 10000000, + 25000000, + 50000000, + 100000000, + 250000000, + 500000000, + 1000000000, + 10000000000 +}; + SamplingBar::SamplingBar(QWidget *parent) : - QToolBar("Sampling Bar", parent) + QToolBar("Sampling Bar", parent), + _device_selector(this), + _record_length_selector(this), + _sample_rate_list(this), + _run_stop_button(this) +{ + connect(&_run_stop_button, SIGNAL(clicked()), this, SIGNAL(run_stop())); + connect(&_device_selector, SIGNAL(currentIndexChanged (int)), + this, SLOT(on_device_selected())); + + _sample_rate_value.setDecimals(0); + _sample_rate_value.setSuffix("Hz"); + + BOOST_FOREACH(uint64_t l, RecordLengths) + { + char *const text = sr_si_string_u64(l, " samples"); + _record_length_selector.addItem(QString(text), + qVariantFromValue(l)); + g_free(text); + } + + _run_stop_button.setText("Run"); + + addWidget(&_device_selector); + addWidget(&_record_length_selector); + _sample_rate_list_action = addWidget(&_sample_rate_list); + _sample_rate_value_action = addWidget(&_sample_rate_value); + addWidget(&_run_stop_button); + + update_device_selector(); + update_sample_rate_selector(); +} + +struct sr_dev_inst* SamplingBar::get_selected_device() const +{ + const int index = _device_selector.currentIndex(); + if(index < 0) + return NULL; + + return (sr_dev_inst*)_device_selector.itemData( + index).value(); +} + +uint64_t SamplingBar::get_record_length() const +{ + const int index = _record_length_selector.currentIndex(); + if(index < 0) + return 0; + + return _record_length_selector.itemData(index).value(); +} + +uint64_t SamplingBar::get_sample_rate() const +{ + assert(_sample_rate_value_action); + assert(_sample_rate_list_action); + + if(_sample_rate_value_action->isVisible()) + return (uint64_t)_sample_rate_value.value(); + else if(_sample_rate_list_action->isVisible()) + { + const int index = _device_selector.currentIndex(); + if(index < 0) + return 0; + + return _device_selector.itemData(index).value(); + } + + return 0; +} + +void SamplingBar::update_device_selector() +{ + GSList *devices = NULL; + + /* Scan all drivers for all devices. */ + struct sr_dev_driver **const drivers = sr_driver_list(); + for (struct sr_dev_driver **driver = drivers; *driver; driver++) { + GSList *tmpdevs = sr_driver_scan(*driver, NULL); + for (GSList *l = tmpdevs; l; l = l->next) + devices = g_slist_append(devices, l->data); + g_slist_free(tmpdevs); + } + + for (GSList *l = devices; l; l = l->next) { + sr_dev_inst *const sdi = (sr_dev_inst*)l->data; + + QString title; + if (sdi->vendor && sdi->vendor[0]) + title += sdi->vendor + QString(" "); + if (sdi->model && sdi->model[0]) + title += sdi->model + QString(" "); + if (sdi->version && sdi->version[0]) + title += sdi->version + QString(" "); + + _device_selector.addItem(title, qVariantFromValue( + (void*)sdi)); + } + + g_slist_free(devices); +} + +void SamplingBar::update_sample_rate_selector() +{ + const sr_dev_inst *const sdi = get_selected_device(); + const struct sr_samplerates *samplerates; + + assert(_sample_rate_value_action); + assert(_sample_rate_list_action); + + if (sr_info_get(sdi->driver, SR_DI_SAMPLERATES, + (const void **)&samplerates, sdi) != SR_OK) + return; + + _sample_rate_list_action->setVisible(false); + _sample_rate_value_action->setVisible(false); + + if (samplerates->step) + { + _sample_rate_value.setRange( + samplerates->low, samplerates->high); + _sample_rate_value.setSingleStep(samplerates->step); + _sample_rate_value_action->setVisible(true); + } + else + { + _sample_rate_list.clear(); + for (const uint64_t *rate = samplerates->list; + *rate; rate++) + _sample_rate_list.addItem( + sr_samplerate_string(*rate), + qVariantFromValue(*rate)); + _sample_rate_list.show(); + _sample_rate_list_action->setVisible(true); + } +} + +void SamplingBar::on_device_selected() { + update_sample_rate_selector(); }