]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2013 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <cassert> | |
21 | ||
22 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
23 | #include <libsigrokdecode/libsigrokdecode.h> | |
24 | ||
25 | #include "decoder.hpp" | |
26 | ||
27 | #include <pv/data/signalbase.hpp> | |
28 | #include <pv/data/decodesignal.hpp> | |
29 | ||
30 | using pv::data::DecodeChannel; | |
31 | using std::set; | |
32 | using std::map; | |
33 | using std::shared_ptr; | |
34 | using std::string; | |
35 | ||
36 | namespace pv { | |
37 | namespace data { | |
38 | namespace decode { | |
39 | ||
40 | Decoder::Decoder(const srd_decoder *const dec) : | |
41 | decoder_(dec), | |
42 | shown_(true) | |
43 | { | |
44 | } | |
45 | ||
46 | Decoder::~Decoder() | |
47 | { | |
48 | for (auto& option : options_) | |
49 | g_variant_unref(option.second); | |
50 | } | |
51 | ||
52 | const srd_decoder* Decoder::decoder() const | |
53 | { | |
54 | return decoder_; | |
55 | } | |
56 | ||
57 | bool Decoder::shown() const | |
58 | { | |
59 | return shown_; | |
60 | } | |
61 | ||
62 | void Decoder::show(bool show) | |
63 | { | |
64 | shown_ = show; | |
65 | } | |
66 | ||
67 | const vector<DecodeChannel*>& Decoder::channels() const | |
68 | { | |
69 | return channels_; | |
70 | } | |
71 | ||
72 | void Decoder::set_channels(vector<DecodeChannel*> channels) | |
73 | { | |
74 | channels_ = channels; | |
75 | } | |
76 | ||
77 | const map<string, GVariant*>& Decoder::options() const | |
78 | { | |
79 | return options_; | |
80 | } | |
81 | ||
82 | void Decoder::set_option(const char *id, GVariant *value) | |
83 | { | |
84 | assert(value); | |
85 | g_variant_ref(value); | |
86 | options_[id] = value; | |
87 | } | |
88 | ||
89 | bool Decoder::have_required_channels() const | |
90 | { | |
91 | for (DecodeChannel *ch : channels_) | |
92 | if (!ch->assigned_signal && !ch->is_optional) | |
93 | return false; | |
94 | ||
95 | return true; | |
96 | } | |
97 | ||
98 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) const | |
99 | { | |
100 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
101 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
102 | ||
103 | for (const auto& option : options_) { | |
104 | GVariant *const value = option.second; | |
105 | g_variant_ref(value); | |
106 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
107 | option.first.c_str()), value); | |
108 | } | |
109 | ||
110 | srd_decoder_inst *const decoder_inst = srd_inst_new( | |
111 | session, decoder_->id, opt_hash); | |
112 | g_hash_table_destroy(opt_hash); | |
113 | ||
114 | if (!decoder_inst) | |
115 | return nullptr; | |
116 | ||
117 | // Setup the channels | |
118 | GArray *const init_pin_states = g_array_sized_new(FALSE, TRUE, | |
119 | sizeof(uint8_t), channels_.size()); | |
120 | ||
121 | g_array_set_size(init_pin_states, channels_.size()); | |
122 | ||
123 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, | |
124 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
125 | ||
126 | for (DecodeChannel *ch : channels_) { | |
127 | init_pin_states->data[ch->id] = ch->initial_pin_state; | |
128 | ||
129 | GVariant *const gvar = g_variant_new_int32(ch->id); // id = bit position | |
130 | g_variant_ref_sink(gvar); | |
131 | // key is channel name, value is bit position in each sample | |
132 | g_hash_table_insert(channels, ch->pdch_->id, gvar); | |
133 | } | |
134 | ||
135 | srd_inst_channel_set_all(decoder_inst, channels); | |
136 | ||
137 | srd_inst_initial_pins_set_all(decoder_inst, init_pin_states); | |
138 | g_array_free(init_pin_states, TRUE); | |
139 | ||
140 | return decoder_inst; | |
141 | } | |
142 | ||
143 | } // namespace decode | |
144 | } // namespace data | |
145 | } // namespace pv |