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