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