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