]>
Commit | Line | Data |
---|---|---|
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 "logicdata.h" | |
24 | #include "logicdatasnapshot.h" | |
25 | #include "logicsignal.h" | |
26 | ||
27 | #include <QDebug> | |
28 | ||
29 | #include <assert.h> | |
30 | ||
31 | using namespace boost; | |
32 | using namespace std; | |
33 | ||
34 | namespace pv { | |
35 | ||
36 | // TODO: This should not be necessary | |
37 | SigSession* SigSession::_session = NULL; | |
38 | ||
39 | SigSession::SigSession() | |
40 | { | |
41 | // TODO: This should not be necessary | |
42 | _session = this; | |
43 | } | |
44 | ||
45 | SigSession::~SigSession() | |
46 | { | |
47 | // TODO: This should not be necessary | |
48 | _session = NULL; | |
49 | } | |
50 | ||
51 | void SigSession::load_file(const std::string &name) | |
52 | { | |
53 | if (sr_session_load(name.c_str()) == SR_OK) { | |
54 | /* sigrok session file */ | |
55 | sr_session_datafeed_callback_add(data_feed_in_proc); | |
56 | sr_session_start(); | |
57 | sr_session_run(); | |
58 | sr_session_stop(); | |
59 | } | |
60 | } | |
61 | ||
62 | void SigSession::start_capture(struct sr_dev_inst *sdi, | |
63 | uint64_t record_length, uint64_t sample_rate) | |
64 | { | |
65 | sr_session_new(); | |
66 | sr_session_datafeed_callback_add(data_feed_in_proc); | |
67 | ||
68 | if (sr_session_dev_add(sdi) != SR_OK) { | |
69 | qDebug() << "Failed to use device."; | |
70 | sr_session_destroy(); | |
71 | return; | |
72 | } | |
73 | ||
74 | if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES, | |
75 | &record_length) != SR_OK) { | |
76 | qDebug() << "Failed to configure time-based sample limit."; | |
77 | sr_session_destroy(); | |
78 | return; | |
79 | } | |
80 | ||
81 | if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE, | |
82 | &sample_rate) != SR_OK) { | |
83 | qDebug() << "Failed to configure samplerate."; | |
84 | sr_session_destroy(); | |
85 | return; | |
86 | } | |
87 | ||
88 | if (sr_session_start() != SR_OK) { | |
89 | qDebug() << "Failed to start session."; | |
90 | return; | |
91 | } | |
92 | ||
93 | sr_session_run(); | |
94 | sr_session_destroy(); | |
95 | } | |
96 | ||
97 | vector< shared_ptr<Signal> >& SigSession::get_signals() | |
98 | { | |
99 | return _signals; | |
100 | } | |
101 | ||
102 | boost::shared_ptr<LogicData> SigSession::get_data() | |
103 | { | |
104 | return _logic_data; | |
105 | } | |
106 | ||
107 | void SigSession::data_feed_in(const struct sr_dev_inst *sdi, | |
108 | struct sr_datafeed_packet *packet) | |
109 | { | |
110 | assert(sdi); | |
111 | assert(packet); | |
112 | ||
113 | switch (packet->type) { | |
114 | case SR_DF_HEADER: | |
115 | _signals.clear(); | |
116 | break; | |
117 | ||
118 | case SR_DF_META_LOGIC: | |
119 | { | |
120 | assert(packet->payload); | |
121 | ||
122 | const sr_datafeed_meta_logic &meta_logic = | |
123 | *(sr_datafeed_meta_logic*)packet->payload; | |
124 | ||
125 | // Create an empty LogiData for coming data snapshots | |
126 | _logic_data.reset(new LogicData(meta_logic)); | |
127 | assert(_logic_data); | |
128 | if(!_logic_data) | |
129 | break; | |
130 | ||
131 | // Add the signals | |
132 | for (int i = 0; i < meta_logic.num_probes; i++) | |
133 | { | |
134 | const sr_probe *const probe = | |
135 | (const sr_probe*)g_slist_nth_data( | |
136 | sdi->probes, i); | |
137 | if(probe->enabled) | |
138 | { | |
139 | boost::shared_ptr<LogicSignal> signal( | |
140 | new LogicSignal(probe->name, | |
141 | _logic_data, | |
142 | probe->index)); | |
143 | _signals.push_back(signal); | |
144 | } | |
145 | } | |
146 | ||
147 | break; | |
148 | } | |
149 | ||
150 | case SR_DF_LOGIC: | |
151 | ||
152 | assert(packet->payload); | |
153 | if(!_cur_logic_snapshot) | |
154 | { | |
155 | // Create a new data snapshot | |
156 | _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>( | |
157 | new LogicDataSnapshot( | |
158 | *(sr_datafeed_logic*)packet->payload)); | |
159 | _logic_data->push_snapshot(_cur_logic_snapshot); | |
160 | } | |
161 | else | |
162 | { | |
163 | // Append to the existing data snapshot | |
164 | _cur_logic_snapshot->append_payload( | |
165 | *(sr_datafeed_logic*)packet->payload); | |
166 | } | |
167 | ||
168 | break; | |
169 | ||
170 | case SR_DF_END: | |
171 | _cur_logic_snapshot.reset(); | |
172 | data_updated(); | |
173 | break; | |
174 | } | |
175 | } | |
176 | ||
177 | void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi, | |
178 | struct sr_datafeed_packet *packet) | |
179 | { | |
180 | assert(_session); | |
181 | _session->data_feed_in(sdi, packet); | |
182 | } | |
183 | ||
184 | } // namespace pv |