2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <libsigrokdecode/libsigrokdecode.h>
23 #include <boost/thread/thread.hpp>
31 #include <pv/data/logic.h>
32 #include <pv/data/logicsnapshot.h>
33 #include <pv/view/logicsignal.h>
34 #include <pv/view/decode/annotation.h>
36 using namespace boost;
42 const double Decoder::DecodeMargin = 1.0;
43 const double Decoder::DecodeThreshold = 0.2;
44 const int64_t Decoder::DecodeChunkLength = 4096;
46 Decoder::Decoder(const srd_decoder *const dec,
47 std::map<const srd_probe*,
48 boost::shared_ptr<pv::view::LogicSignal> > probes,
49 GHashTable *options) :
61 _decode_thread.interrupt();
62 _decode_thread.join();
64 g_hash_table_destroy(_options);
67 const srd_decoder* Decoder::get_decoder() const
72 const vector< shared_ptr<view::decode::Annotation> >
73 Decoder::annotations() const
75 lock_guard<mutex> lock(_mutex);
79 QString Decoder::error_message()
81 lock_guard<mutex> lock(_mutex);
82 return _error_message;
85 void Decoder::begin_decode()
87 _decode_thread.interrupt();
88 _decode_thread.join();
93 // We get the logic data of the first probe in the list.
94 // This works because we are currently assuming all
95 // LogicSignals have the same data/snapshot
96 shared_ptr<pv::view::LogicSignal> sig = (*_probes.begin()).second;
98 shared_ptr<data::Logic> data = sig->data();
100 _decode_thread = boost::thread(&Decoder::decode_proc, this,
104 void Decoder::clear_snapshots()
108 void Decoder::init_decoder()
110 if (!_probes.empty())
112 shared_ptr<pv::view::LogicSignal> logic_signal =
113 dynamic_pointer_cast<pv::view::LogicSignal>(
114 (*_probes.begin()).second);
116 shared_ptr<pv::data::Logic> data(
117 logic_signal->data());
119 _start_time = data->get_start_time();
120 _samplerate = data->get_samplerate();
121 if (_samplerate == 0.0)
128 void Decoder::decode_proc(shared_ptr<data::Logic> data)
130 srd_session *session;
131 uint8_t chunk[DecodeChunkLength];
135 _annotations.clear();
137 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
138 data->get_snapshots();
139 if (snapshots.empty())
142 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
144 const int64_t sample_count = snapshot->get_sample_count() - 1;
146 // Create the session
147 srd_session_new(&session);
150 srd_session_config_set(session, SRD_CONF_NUM_PROBES,
151 g_variant_new_uint64(_probes.size()));
152 srd_session_config_set(session, SRD_CONF_UNITSIZE,
153 g_variant_new_uint64(snapshot->unit_size()));
154 srd_session_config_set(session, SRD_CONF_SAMPLERATE,
155 g_variant_new_uint64((uint64_t)_samplerate));
157 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
158 Decoder::annotation_callback, this);
160 // Create the decoder instance
161 srd_decoder_inst *const decoder_inst = srd_inst_new(
162 session, _decoder->id, _options);
164 _error_message = tr("Failed to initialise decoder");
168 decoder_inst->data_samplerate = _samplerate;
171 GHashTable *const probes = g_hash_table_new_full(g_str_hash,
172 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
174 for(map<const srd_probe*, shared_ptr<view::LogicSignal> >::
175 const_iterator i = _probes.begin();
176 i != _probes.end(); i++)
178 shared_ptr<view::Signal> signal((*i).second);
179 GVariant *const gvar = g_variant_new_int32(
180 signal->probe()->index);
181 g_variant_ref_sink(gvar);
182 g_hash_table_insert(probes, (*i).first->id, gvar);
185 srd_inst_probe_set_all(decoder_inst, probes);
188 srd_session_start(session);
191 !this_thread::interruption_requested() && i < sample_count;
192 i += DecodeChunkLength)
194 const int64_t chunk_end = min(
195 i + DecodeChunkLength, sample_count);
196 snapshot->get_samples(chunk, i, chunk_end);
198 if (srd_session_send(session, i, chunk, chunk_end - i) !=
200 _error_message = tr("Failed to initialise decoder");
205 // Destroy the session
206 srd_session_destroy(session);
209 void Decoder::annotation_callback(srd_proto_data *pdata, void *decoder)
211 using namespace pv::view::decode;
216 Decoder *const d = (Decoder*)decoder;
218 shared_ptr<Annotation> a(new Annotation(pdata));
219 lock_guard<mutex> lock(d->_mutex);
220 d->_annotations.push_back(a);
222 d->new_decode_data();