]> sigrok.org Git - pulseview.git/blobdiff - pv/samplingbar.cpp
Moved all classes into the pv namespace
[pulseview.git] / pv / samplingbar.cpp
diff --git a/pv/samplingbar.cpp b/pv/samplingbar.cpp
new file mode 100644 (file)
index 0000000..3104743
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include <assert.h>
+
+#include <boost/foreach.hpp>
+
+extern "C" {
+#include <libsigrok/libsigrok.h>
+}
+
+#include <QAction>
+
+#include "samplingbar.h"
+
+namespace pv {
+
+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),
+       _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<void*>();
+}
+
+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>();
+}
+
+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 = _sample_rate_list.currentIndex();
+               if(index < 0)
+                       return 0;
+               
+               return _sample_rate_list.itemData(index).value<uint64_t>();
+       }
+
+       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();
+}
+
+} // namespace pv