2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
23 #include <boost/foreach.hpp>
26 #include <libsigrok/libsigrok.h>
31 #include "samplingbar.h"
35 const uint64_t SamplingBar::RecordLengths[11] = {
49 SamplingBar::SamplingBar(QWidget *parent) :
50 QToolBar("Sampling Bar", parent),
51 _device_selector(this),
52 _record_length_selector(this),
53 _sample_rate_list(this),
54 _run_stop_button(this)
56 connect(&_run_stop_button, SIGNAL(clicked()), this, SIGNAL(run_stop()));
57 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
58 this, SLOT(on_device_selected()));
60 _sample_rate_value.setDecimals(0);
61 _sample_rate_value.setSuffix("Hz");
63 BOOST_FOREACH(uint64_t l, RecordLengths)
65 char *const text = sr_si_string_u64(l, " samples");
66 _record_length_selector.addItem(QString(text),
67 qVariantFromValue(l));
71 _run_stop_button.setText("Run");
73 addWidget(&_device_selector);
74 addWidget(&_record_length_selector);
75 _sample_rate_list_action = addWidget(&_sample_rate_list);
76 _sample_rate_value_action = addWidget(&_sample_rate_value);
77 addWidget(&_run_stop_button);
79 update_device_selector();
80 update_sample_rate_selector();
83 struct sr_dev_inst* SamplingBar::get_selected_device() const
85 const int index = _device_selector.currentIndex();
89 return (sr_dev_inst*)_device_selector.itemData(
90 index).value<void*>();
93 uint64_t SamplingBar::get_record_length() const
95 const int index = _record_length_selector.currentIndex();
99 return _record_length_selector.itemData(index).value<uint64_t>();
102 uint64_t SamplingBar::get_sample_rate() const
104 assert(_sample_rate_value_action);
105 assert(_sample_rate_list_action);
107 if(_sample_rate_value_action->isVisible())
108 return (uint64_t)_sample_rate_value.value();
109 else if(_sample_rate_list_action->isVisible())
111 const int index = _sample_rate_list.currentIndex();
115 return _sample_rate_list.itemData(index).value<uint64_t>();
121 void SamplingBar::update_device_selector()
123 GSList *devices = NULL;
125 /* Scan all drivers for all devices. */
126 struct sr_dev_driver **const drivers = sr_driver_list();
127 for (struct sr_dev_driver **driver = drivers; *driver; driver++) {
128 GSList *tmpdevs = sr_driver_scan(*driver, NULL);
129 for (GSList *l = tmpdevs; l; l = l->next)
130 devices = g_slist_append(devices, l->data);
131 g_slist_free(tmpdevs);
134 for (GSList *l = devices; l; l = l->next) {
135 sr_dev_inst *const sdi = (sr_dev_inst*)l->data;
138 if (sdi->vendor && sdi->vendor[0])
139 title += sdi->vendor + QString(" ");
140 if (sdi->model && sdi->model[0])
141 title += sdi->model + QString(" ");
142 if (sdi->version && sdi->version[0])
143 title += sdi->version + QString(" ");
145 _device_selector.addItem(title, qVariantFromValue(
149 g_slist_free(devices);
152 void SamplingBar::update_sample_rate_selector()
154 const sr_dev_inst *const sdi = get_selected_device();
155 const struct sr_samplerates *samplerates;
157 assert(_sample_rate_value_action);
158 assert(_sample_rate_list_action);
160 if (sr_info_get(sdi->driver, SR_DI_SAMPLERATES,
161 (const void **)&samplerates, sdi) != SR_OK)
164 _sample_rate_list_action->setVisible(false);
165 _sample_rate_value_action->setVisible(false);
167 if (samplerates->step)
169 _sample_rate_value.setRange(
170 samplerates->low, samplerates->high);
171 _sample_rate_value.setSingleStep(samplerates->step);
172 _sample_rate_value_action->setVisible(true);
176 _sample_rate_list.clear();
177 for (const uint64_t *rate = samplerates->list;
179 _sample_rate_list.addItem(
180 sr_samplerate_string(*rate),
181 qVariantFromValue(*rate));
182 _sample_rate_list.show();
183 _sample_rate_list_action->setVisible(true);
187 void SamplingBar::on_device_selected()
189 update_sample_rate_selector();