]> sigrok.org Git - pulseview.git/blame_incremental - pv/sigsession.cpp
Ported sampling to new sigrok API
[pulseview.git] / pv / sigsession.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView 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
21#include "sigsession.h"
22
23#include "data/analog.h"
24#include "data/analogsnapshot.h"
25#include "data/logic.h"
26#include "data/logicsnapshot.h"
27#include "view/analogsignal.h"
28#include "view/logicsignal.h"
29
30#include <QDebug>
31
32#include <assert.h>
33
34using namespace boost;
35using namespace std;
36
37namespace pv {
38
39// TODO: This should not be necessary
40SigSession* SigSession::_session = NULL;
41
42SigSession::SigSession() :
43 _capture_state(Stopped)
44{
45 // TODO: This should not be necessary
46 _session = this;
47}
48
49SigSession::~SigSession()
50{
51 stop_capture();
52
53 if (_sampling_thread.get())
54 _sampling_thread->join();
55 _sampling_thread.reset();
56
57 // TODO: This should not be necessary
58 _session = NULL;
59}
60
61void SigSession::load_file(const string &name)
62{
63 stop_capture();
64 _sampling_thread.reset(new boost::thread(
65 &SigSession::load_thread_proc, this, name));
66}
67
68SigSession::capture_state SigSession::get_capture_state() const
69{
70 lock_guard<mutex> lock(_sampling_mutex);
71 return _capture_state;
72}
73
74void SigSession::start_capture(struct sr_dev_inst *sdi,
75 uint64_t record_length, uint64_t sample_rate)
76{
77 stop_capture();
78
79 lock_guard<mutex> lock(_sampling_mutex);
80 _sample_rate = sample_rate;
81
82 _sampling_thread.reset(new boost::thread(
83 &SigSession::sample_thread_proc, this, sdi,
84 record_length));
85}
86
87void SigSession::stop_capture()
88{
89 if (get_capture_state() == Stopped)
90 return;
91
92 sr_session_stop();
93
94 // Check that sampling stopped
95 if (_sampling_thread.get())
96 _sampling_thread->join();
97 _sampling_thread.reset();
98}
99
100vector< shared_ptr<view::Signal> > SigSession::get_signals()
101{
102 lock_guard<mutex> lock(_signals_mutex);
103 return _signals;
104}
105
106boost::shared_ptr<data::Logic> SigSession::get_data()
107{
108 return _logic_data;
109}
110
111void SigSession::set_capture_state(capture_state state)
112{
113 lock_guard<mutex> lock(_sampling_mutex);
114 _capture_state = state;
115 capture_state_changed(state);
116}
117
118void SigSession::load_thread_proc(const string name)
119{
120 if (sr_session_load(name.c_str()) != SR_OK) {
121 qDebug() << "Failed to load file.";
122 return;
123 }
124
125 sr_session_datafeed_callback_add(data_feed_in_proc);
126
127 if (sr_session_start() != SR_OK) {
128 qDebug() << "Failed to start session.";
129 return;
130 }
131
132 set_capture_state(Running);
133
134 sr_session_run();
135 sr_session_stop();
136
137 set_capture_state(Stopped);
138}
139
140void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
141 uint64_t record_length)
142{
143 shared_ptr<view::Signal> signal;
144 unsigned int logic_probe_count = 0;
145 unsigned int analog_probe_count = 0;
146
147 assert(sdi);
148
149 sr_session_new();
150 sr_session_datafeed_callback_add(data_feed_in_proc);
151
152 if (sr_session_dev_add(sdi) != SR_OK) {
153 qDebug() << "Failed to use device.";
154 sr_session_destroy();
155 return;
156 }
157
158 // Set the sample limit
159 if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
160 &record_length) != SR_OK) {
161 qDebug() << "Failed to configure time-based sample limit.";
162 sr_session_destroy();
163 return;
164 }
165
166 // Set the samplerate
167 {
168 lock_guard<mutex> lock(_sampling_mutex);
169 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
170 &_sample_rate) != SR_OK) {
171 qDebug() << "Failed to configure samplerate.";
172 sr_session_destroy();
173 return;
174 }
175 }
176
177 // Detect what data types we will receive
178 for (const GSList *l = sdi->probes; l; l = l->next) {
179 const sr_probe *const probe = (const sr_probe *)l->data;
180 if (!probe->enabled)
181 continue;
182
183 switch(probe->type) {
184 case SR_PROBE_LOGIC:
185 logic_probe_count++;
186 break;
187
188 case SR_PROBE_ANALOG:
189 analog_probe_count++;
190 break;
191 }
192 }
193
194 // Create data containers for the coming data snapshots
195 {
196 lock_guard<mutex> data_lock(_data_mutex);
197 lock_guard<mutex> sampling_lock(_sampling_mutex);
198
199 if (logic_probe_count != 0) {
200 _logic_data.reset(new data::Logic(
201 logic_probe_count, _sample_rate));
202 assert(_logic_data);
203 }
204
205 if (analog_probe_count != 0) {
206 _analog_data.reset(new data::Analog(_sample_rate));
207 assert(_analog_data);
208 }
209 }
210
211 // Make the logic probe list
212 {
213 lock_guard<mutex> lock(_signals_mutex);
214
215 _signals.clear();
216
217 for (const GSList *l = sdi->probes; l; l = l->next) {
218 const sr_probe *const probe =
219 (const sr_probe *)l->data;
220 assert(probe);
221 if (!probe->enabled)
222 continue;
223
224 switch(probe->type) {
225 case SR_PROBE_LOGIC:
226 signal = shared_ptr<view::Signal>(
227 new view::LogicSignal(probe->name,
228 _logic_data, probe->index));
229 break;
230
231 case SR_PROBE_ANALOG:
232 signal = shared_ptr<view::Signal>(
233 new view::AnalogSignal(probe->name,
234 _analog_data));
235 break;
236 }
237
238 _signals.push_back(signal);
239 }
240
241 signals_changed();
242 }
243
244 if (sr_session_start() != SR_OK) {
245 qDebug() << "Failed to start session.";
246 return;
247 }
248
249 set_capture_state(Running);
250
251 sr_session_run();
252 sr_session_destroy();
253
254 set_capture_state(Stopped);
255}
256
257void SigSession::feed_in_meta(const sr_dev_inst *sdi,
258 const sr_datafeed_meta &meta)
259{
260 for (const GSList *l = meta.config; l; l = l->next) {
261 const sr_config *const src = (const sr_config*)l->data;
262 switch (src->key) {
263 case SR_CONF_SAMPLERATE:
264 /// @todo handle samplerate changes
265 /// samplerate = (uint64_t *)src->value;
266 break;
267 default:
268 // Unknown metadata is not an error.
269 break;
270 }
271 }
272}
273
274void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
275{
276 lock_guard<mutex> lock(_data_mutex);
277 if (!_cur_logic_snapshot)
278 {
279 assert(_logic_data);
280
281 // Create a new data snapshot
282 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
283 new data::LogicSnapshot(logic));
284 _logic_data->push_snapshot(_cur_logic_snapshot);
285 }
286 else
287 {
288 // Append to the existing data snapshot
289 _cur_logic_snapshot->append_payload(logic);
290 }
291
292 data_updated();
293}
294
295void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
296{
297 lock_guard<mutex> lock(_data_mutex);
298 if (!_cur_analog_snapshot)
299 {
300 assert(_analog_data);
301
302 // Create a new data snapshot
303 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
304 new data::AnalogSnapshot(analog));
305 _analog_data->push_snapshot(_cur_analog_snapshot);
306 }
307 else
308 {
309 // Append to the existing data snapshot
310 _cur_analog_snapshot->append_payload(analog);
311 }
312
313 data_updated();
314}
315
316void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
317 const struct sr_datafeed_packet *packet)
318{
319 assert(sdi);
320 assert(packet);
321
322 switch (packet->type) {
323 case SR_DF_HEADER:
324 break;
325
326 case SR_DF_META:
327 assert(packet->payload);
328 feed_in_meta(sdi,
329 *(const sr_datafeed_meta*)packet->payload);
330 break;
331
332 case SR_DF_LOGIC:
333 assert(packet->payload);
334 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
335 break;
336
337 case SR_DF_ANALOG:
338 assert(packet->payload);
339 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
340 break;
341
342 case SR_DF_END:
343 {
344 {
345 lock_guard<mutex> lock(_data_mutex);
346 _cur_logic_snapshot.reset();
347 _cur_analog_snapshot.reset();
348 }
349 data_updated();
350 break;
351 }
352 }
353}
354
355void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
356 const struct sr_datafeed_packet *packet)
357{
358 assert(_session);
359 _session->data_feed_in(sdi, packet);
360}
361
362} // namespace pv