]> sigrok.org Git - pulseview.git/blame - pv/sigsession.cpp
Added SigSession::signals_changed signals to indicate when signals are created
[pulseview.git] / pv / sigsession.cpp
CommitLineData
2953961c 1/*
b3f22de0 2 * This file is part of the PulseView project.
2953961c
JH
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
28a4c9c5
JH
23#include "logicdata.h"
24#include "logicdatasnapshot.h"
8d634081 25#include "view/logicsignal.h"
28a4c9c5 26
009e1503
JH
27#include <QDebug>
28
2953961c
JH
29#include <assert.h>
30
28a4c9c5 31using namespace boost;
e3f65ace 32using namespace std;
28a4c9c5 33
51e77110
JH
34namespace pv {
35
2953961c 36// TODO: This should not be necessary
04abfae9 37SigSession* SigSession::_session = NULL;
2953961c 38
28a4c9c5 39SigSession::SigSession()
2953961c
JH
40{
41 // TODO: This should not be necessary
04abfae9 42 _session = this;
2953961c
JH
43}
44
45SigSession::~SigSession()
46{
2e2946fe
JH
47 if(_sampling_thread.get())
48 _sampling_thread->join();
49 _sampling_thread.reset();
50
2953961c 51 // TODO: This should not be necessary
04abfae9 52 _session = NULL;
2953961c
JH
53}
54
04abfae9 55void SigSession::load_file(const std::string &name)
2953961c
JH
56{
57 if (sr_session_load(name.c_str()) == SR_OK) {
58 /* sigrok session file */
04abfae9 59 sr_session_datafeed_callback_add(data_feed_in_proc);
2953961c
JH
60 sr_session_start();
61 sr_session_run();
62 sr_session_stop();
63 }
64}
65
274d4f13 66void SigSession::start_capture(struct sr_dev_inst *sdi,
215f9499 67 uint64_t record_length, uint64_t sample_rate)
2e2946fe
JH
68{
69 // Check sampling isn't already active
70 if(_sampling_thread.get())
71 _sampling_thread->join();
72
73 _sampling_thread.reset(new boost::thread(
74 &SigSession::sample_thread_proc, this, sdi,
75 record_length, sample_rate));
76}
77
78vector< shared_ptr<view::Signal> >& SigSession::get_signals()
79{
80 return _signals;
81}
82
83boost::shared_ptr<LogicData> SigSession::get_data()
84{
85 return _logic_data;
86}
87
88void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
89 uint64_t record_length, uint64_t sample_rate)
274d4f13
JH
90{
91 sr_session_new();
04abfae9 92 sr_session_datafeed_callback_add(data_feed_in_proc);
274d4f13
JH
93
94 if (sr_session_dev_add(sdi) != SR_OK) {
95 qDebug() << "Failed to use device.";
96 sr_session_destroy();
97 return;
98 }
99
274d4f13 100 if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES,
215f9499 101 &record_length) != SR_OK) {
274d4f13
JH
102 qDebug() << "Failed to configure time-based sample limit.";
103 sr_session_destroy();
104 return;
105 }
106
107 if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
108 &sample_rate) != SR_OK) {
109 qDebug() << "Failed to configure samplerate.";
110 sr_session_destroy();
111 return;
112 }
113
114 if (sr_session_start() != SR_OK) {
115 qDebug() << "Failed to start session.";
116 return;
117 }
118
119 sr_session_run();
120 sr_session_destroy();
121}
122
04abfae9 123void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
2953961c
JH
124 struct sr_datafeed_packet *packet)
125{
8d634081
JH
126 using view::LogicSignal;
127
2953961c
JH
128 assert(sdi);
129 assert(packet);
130
131 switch (packet->type) {
28a4c9c5 132 case SR_DF_HEADER:
2e2946fe
JH
133 {
134 lock_guard<mutex> lock(_data_mutex);
e3f65ace 135 _signals.clear();
009e1503 136 break;
2e2946fe 137 }
009e1503 138
28a4c9c5 139 case SR_DF_META_LOGIC:
2e2946fe
JH
140 {
141 assert(packet->payload);
009e1503 142
2e2946fe 143 lock_guard<mutex> lock(_data_mutex);
009e1503 144
2e2946fe
JH
145 const sr_datafeed_meta_logic &meta_logic =
146 *(sr_datafeed_meta_logic*)packet->payload;
009e1503 147
2e2946fe
JH
148 // Create an empty LogiData for coming data snapshots
149 _logic_data.reset(new LogicData(meta_logic));
150 assert(_logic_data);
151 if(!_logic_data)
152 break;
153
154 // Add the signals
155 for (int i = 0; i < meta_logic.num_probes; i++)
156 {
157 const sr_probe *const probe =
158 (const sr_probe*)g_slist_nth_data(
159 sdi->probes, i);
160 if(probe->enabled)
e3f65ace 161 {
2e2946fe
JH
162 shared_ptr<LogicSignal> signal(
163 new LogicSignal(probe->name,
164 _logic_data,
165 probe->index));
166 _signals.push_back(signal);
e3f65ace 167 }
2953961c 168 }
28a4c9c5 169
69dd2b03 170 signals_changed();
2e2946fe
JH
171 break;
172 }
f556bc6a 173
2e2946fe
JH
174 case SR_DF_LOGIC:
175 {
176 lock_guard<mutex> lock(_data_mutex);
28a4c9c5 177 assert(packet->payload);
f556bc6a
JH
178 if(!_cur_logic_snapshot)
179 {
180 // Create a new data snapshot
181 _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>(
182 new LogicDataSnapshot(
183 *(sr_datafeed_logic*)packet->payload));
184 _logic_data->push_snapshot(_cur_logic_snapshot);
185 }
186 else
187 {
188 // Append to the existing data snapshot
28a4c9c5
JH
189 _cur_logic_snapshot->append_payload(
190 *(sr_datafeed_logic*)packet->payload);
f556bc6a
JH
191 }
192
2953961c 193 break;
2e2946fe 194 }
2953961c
JH
195
196 case SR_DF_END:
2e2946fe
JH
197 {
198 {
199 lock_guard<mutex> lock(_data_mutex);
200 _cur_logic_snapshot.reset();
201 }
04abfae9 202 data_updated();
2953961c
JH
203 break;
204 }
2e2946fe 205 }
2953961c
JH
206}
207
04abfae9 208void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
2953961c
JH
209 struct sr_datafeed_packet *packet)
210{
04abfae9
JH
211 assert(_session);
212 _session->data_feed_in(sdi, packet);
2953961c 213}
51e77110
JH
214
215} // namespace pv