]>
Commit | Line | Data |
---|---|---|
119aff65 JH |
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 | ||
e332f6d3 JH |
21 | #include <libsigrokdecode/libsigrokdecode.h> |
22 | ||
e0fc5810 JH |
23 | #include <boost/thread/thread.hpp> |
24 | ||
b213ef09 JH |
25 | #include <QDebug> |
26 | ||
119aff65 JH |
27 | #include "decoder.h" |
28 | ||
e0fc5810 JH |
29 | #include <pv/data/logic.h> |
30 | #include <pv/data/logicsnapshot.h> | |
31 | #include <pv/view/logicsignal.h> | |
32 | #include <pv/view/decode/annotation.h> | |
708c5523 | 33 | |
e332f6d3 JH |
34 | using namespace boost; |
35 | using namespace std; | |
36 | ||
119aff65 JH |
37 | namespace pv { |
38 | namespace data { | |
39 | ||
e0fc5810 JH |
40 | const double Decoder::DecodeMargin = 1.0; |
41 | const double Decoder::DecodeThreshold = 0.2; | |
42 | const int64_t Decoder::DecodeChunkLength = 4096; | |
43 | ||
708c5523 JH |
44 | Decoder::Decoder(const srd_decoder *const dec, |
45 | std::map<const srd_probe*, | |
67fe5e9c JH |
46 | boost::shared_ptr<pv::view::Signal> > probes, |
47 | GHashTable *options) : | |
708c5523 | 48 | _decoder(dec), |
e332f6d3 | 49 | _probes(probes), |
67fe5e9c | 50 | _options(options), |
9d20a741 | 51 | _session(NULL), |
e332f6d3 | 52 | _decoder_inst(NULL) |
119aff65 | 53 | { |
e332f6d3 | 54 | init_decoder(); |
e0fc5810 | 55 | begin_decode(); |
119aff65 JH |
56 | } |
57 | ||
640d69ce JH |
58 | Decoder::~Decoder() |
59 | { | |
60 | _decode_thread.interrupt(); | |
61 | _decode_thread.join(); | |
67fe5e9c JH |
62 | |
63 | g_hash_table_destroy(_options); | |
640d69ce JH |
64 | } |
65 | ||
119aff65 JH |
66 | const srd_decoder* Decoder::get_decoder() const |
67 | { | |
68 | return _decoder; | |
69 | } | |
70 | ||
e0fc5810 JH |
71 | const vector< shared_ptr<view::decode::Annotation> > |
72 | Decoder::annotations() const | |
73 | { | |
74 | lock_guard<mutex> lock(_annotations_mutex); | |
75 | return _annotations; | |
76 | } | |
77 | ||
640d69ce JH |
78 | void Decoder::begin_decode() |
79 | { | |
80 | _decode_thread.interrupt(); | |
81 | _decode_thread.join(); | |
82 | ||
e0fc5810 JH |
83 | if (_probes.empty()) |
84 | return; | |
85 | ||
86 | // We get the logic data of the first probe in the list. | |
87 | // This works because we are currently assuming all | |
88 | // LogicSignals have the same data/snapshot | |
89 | shared_ptr<pv::view::Signal> sig = (*_probes.begin()).second; | |
90 | assert(sig); | |
91 | const pv::view::LogicSignal *const l = | |
92 | dynamic_cast<pv::view::LogicSignal*>(sig.get()); | |
93 | assert(l); | |
94 | shared_ptr<data::Logic> data = l->data(); | |
95 | ||
640d69ce | 96 | _decode_thread = boost::thread(&Decoder::decode_proc, this, |
e0fc5810 | 97 | data); |
640d69ce JH |
98 | } |
99 | ||
100 | void Decoder::clear_snapshots() | |
101 | { | |
102 | } | |
103 | ||
e332f6d3 JH |
104 | void Decoder::init_decoder() |
105 | { | |
e0fc5810 JH |
106 | if (!_probes.empty()) |
107 | { | |
108 | shared_ptr<pv::view::LogicSignal> logic_signal = | |
109 | dynamic_pointer_cast<pv::view::LogicSignal>( | |
110 | (*_probes.begin()).second); | |
111 | if (logic_signal) { | |
112 | shared_ptr<pv::data::Logic> data( | |
113 | logic_signal->data()); | |
114 | if (data) { | |
115 | _samplerate = data->get_samplerate(); | |
116 | _start_time = data->get_start_time(); | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
9d20a741 JH |
121 | srd_session_new(&_session); |
122 | assert(_session); | |
123 | ||
124 | _decoder_inst = srd_inst_new(_session, _decoder->id, _options); | |
52292f6c JH |
125 | if(!_decoder_inst) { |
126 | qDebug() << "Failed to initialise decoder"; | |
127 | return; | |
128 | } | |
e332f6d3 | 129 | |
e0fc5810 JH |
130 | _decoder_inst->data_samplerate = _samplerate; |
131 | ||
e332f6d3 JH |
132 | GHashTable *probes = g_hash_table_new_full(g_str_hash, |
133 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
134 | ||
135 | for(map<const srd_probe*, shared_ptr<view::Signal> >:: | |
136 | const_iterator i = _probes.begin(); | |
137 | i != _probes.end(); i++) | |
138 | { | |
139 | shared_ptr<view::Signal> signal((*i).second); | |
140 | GVariant *const gvar = g_variant_new_int32( | |
141 | signal->probe()->index); | |
142 | g_variant_ref_sink(gvar); | |
143 | g_hash_table_insert(probes, (*i).first->id, gvar); | |
144 | } | |
145 | ||
146 | srd_inst_probe_set_all(_decoder_inst, probes); | |
147 | } | |
148 | ||
640d69ce | 149 | void Decoder::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 150 | { |
e0fc5810 JH |
151 | uint8_t chunk[DecodeChunkLength]; |
152 | ||
153 | assert(data); | |
154 | ||
155 | _annotations.clear(); | |
156 | ||
157 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = | |
158 | data->get_snapshots(); | |
159 | if (snapshots.empty()) | |
160 | return; | |
161 | ||
162 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = | |
163 | snapshots.front(); | |
164 | const int64_t sample_count = snapshot->get_sample_count() - 1; | |
165 | double samplerate = data->get_samplerate(); | |
166 | ||
167 | // Show sample rate as 1Hz when it is unknown | |
168 | if (samplerate == 0.0) | |
169 | samplerate = 1.0; | |
170 | ||
9d20a741 JH |
171 | srd_session_config_set(_session, SRD_CONF_NUM_PROBES, |
172 | g_variant_new_uint64(_probes.size())); | |
173 | srd_session_config_set(_session, SRD_CONF_UNITSIZE, | |
174 | g_variant_new_uint64(snapshot->unit_size())); | |
175 | srd_session_config_set(_session, SRD_CONF_SAMPLERATE, | |
176 | g_variant_new_uint64((uint64_t)samplerate)); | |
177 | ||
178 | srd_session_start(_session); | |
e0fc5810 | 179 | |
9d20a741 | 180 | srd_pd_output_callback_add(_session, SRD_OUTPUT_ANN, |
e0fc5810 JH |
181 | Decoder::annotation_callback, this); |
182 | ||
183 | for (int64_t i = 0; | |
184 | !this_thread::interruption_requested() && i < sample_count; | |
185 | i += DecodeChunkLength) | |
186 | { | |
187 | const int64_t chunk_end = min( | |
188 | i + DecodeChunkLength, sample_count); | |
189 | snapshot->get_samples(chunk, i, chunk_end); | |
190 | ||
9d20a741 JH |
191 | if (srd_session_send(_session, i, chunk, chunk_end - i) != |
192 | SRD_OK) | |
e0fc5810 JH |
193 | break; |
194 | } | |
195 | } | |
196 | ||
197 | void Decoder::annotation_callback(srd_proto_data *pdata, void *decoder) | |
198 | { | |
199 | using namespace pv::view::decode; | |
200 | ||
201 | assert(pdata); | |
202 | assert(decoder); | |
203 | ||
204 | Decoder *const d = (Decoder*)decoder; | |
205 | ||
206 | shared_ptr<Annotation> a(new Annotation(pdata)); | |
207 | lock_guard<mutex> lock(d->_annotations_mutex); | |
208 | d->_annotations.push_back(a); | |
9cef9567 JH |
209 | |
210 | d->new_decode_data(); | |
119aff65 JH |
211 | } |
212 | ||
213 | } // namespace data | |
214 | } // namespace pv |