]> sigrok.org Git - pulseview.git/blame - samplingbar.cpp
Added initial sampling support
[pulseview.git] / samplingbar.cpp
CommitLineData
d4984fe7
JH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 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
dde1a563
JH
21#include <assert.h>
22
6fb67b27
JH
23extern "C" {
24#include <libsigrok/libsigrok.h>
25}
26
dde1a563
JH
27#include <QAction>
28
d4984fe7
JH
29#include "samplingbar.h"
30
31SamplingBar::SamplingBar(QWidget *parent) :
6fb67b27 32 QToolBar("Sampling Bar", parent),
dde1a563 33 _device_selector(this),
274d4f13
JH
34 _sample_rate_list(this),
35 _run_stop_button(this)
6fb67b27 36{
274d4f13 37 connect(&_run_stop_button, SIGNAL(clicked()), this, SIGNAL(run_stop()));
dde1a563
JH
38 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
39 this, SLOT(on_device_selected()));
40
41 _sample_rate_value.setDecimals(0);
42 _sample_rate_value.setSuffix("Hz");
43
274d4f13
JH
44 _run_stop_button.setText("Run");
45
6fb67b27 46 addWidget(&_device_selector);
dde1a563
JH
47 _sample_rate_list_action = addWidget(&_sample_rate_list);
48 _sample_rate_value_action = addWidget(&_sample_rate_value);
274d4f13 49 addWidget(&_run_stop_button);
6fb67b27
JH
50
51 update_device_selector();
dde1a563 52 update_sample_rate_selector();
6fb67b27
JH
53}
54
55struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 56{
6fb67b27
JH
57 const int index = _device_selector.currentIndex();
58 if(index < 0)
59 return NULL;
60
61 return (sr_dev_inst*)_device_selector.itemData(
62 index).value<void*>();
63}
64
dde1a563
JH
65uint64_t SamplingBar::get_sample_rate() const
66{
67 assert(_sample_rate_value_action);
68 assert(_sample_rate_list_action);
69
70 if(_sample_rate_value_action->isVisible())
71 return (uint64_t)_sample_rate_value.value();
72 else if(_sample_rate_list_action->isVisible())
73 {
74 const int index = _device_selector.currentIndex();
75 if(index < 0)
76 return 0;
77
78 return _device_selector.itemData(index).value<uint64_t>();
79 }
80
81 return 0;
82}
83
6fb67b27
JH
84void SamplingBar::update_device_selector()
85{
86 GSList *devices = NULL;
87
88 /* Scan all drivers for all devices. */
89 struct sr_dev_driver **const drivers = sr_driver_list();
90 for (struct sr_dev_driver **driver = drivers; *driver; driver++) {
91 GSList *tmpdevs = sr_driver_scan(*driver, NULL);
92 for (GSList *l = tmpdevs; l; l = l->next)
93 devices = g_slist_append(devices, l->data);
94 g_slist_free(tmpdevs);
95 }
96
97 for (GSList *l = devices; l; l = l->next) {
98 sr_dev_inst *const sdi = (sr_dev_inst*)l->data;
99
100 QString title;
101 if (sdi->vendor && sdi->vendor[0])
102 title += sdi->vendor + QString(" ");
103 if (sdi->model && sdi->model[0])
104 title += sdi->model + QString(" ");
105 if (sdi->version && sdi->version[0])
106 title += sdi->version + QString(" ");
107
108 _device_selector.addItem(title, qVariantFromValue(
109 (void*)sdi));
110 }
111
112 g_slist_free(devices);
d4984fe7 113}
dde1a563
JH
114
115void SamplingBar::update_sample_rate_selector()
116{
117 const sr_dev_inst *const sdi = get_selected_device();
118 const struct sr_samplerates *samplerates;
119
120 assert(_sample_rate_value_action);
121 assert(_sample_rate_list_action);
122
123 if (sr_info_get(sdi->driver, SR_DI_SAMPLERATES,
124 (const void **)&samplerates, sdi) != SR_OK)
125 return;
126
127 _sample_rate_list_action->setVisible(false);
128 _sample_rate_value_action->setVisible(false);
129
130 if (samplerates->step)
131 {
132 _sample_rate_value.setRange(
133 samplerates->low, samplerates->high);
134 _sample_rate_value.setSingleStep(samplerates->step);
135 _sample_rate_value_action->setVisible(true);
136 }
137 else
138 {
139 _sample_rate_list.clear();
140 for (const uint64_t *rate = samplerates->list;
141 *rate; rate++)
142 _sample_rate_list.addItem(
143 sr_samplerate_string(*rate),
144 qVariantFromValue(*rate));
145 _sample_rate_list.show();
146 _sample_rate_list_action->setVisible(true);
147 }
148}
149
150void SamplingBar::on_device_selected()
151{
152 update_sample_rate_selector();
153}