2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 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, see <http://www.gnu.org/licenses/>.
22 #include <libsigrokcxx/libsigrokcxx.hpp>
23 #include <libsigrokdecode/libsigrokdecode.h>
25 #include "decoder.hpp"
27 #include <pv/data/signalbase.hpp>
28 #include <pv/data/decodesignal.hpp>
30 using pv::data::DecodeChannel;
38 Decoder::Decoder(const srd_decoder *const dec) :
46 for (auto& option : options_)
47 g_variant_unref(option.second);
50 const srd_decoder* Decoder::decoder() const
55 bool Decoder::shown() const
60 void Decoder::show(bool show)
65 const vector<DecodeChannel*>& Decoder::channels() const
70 void Decoder::set_channels(vector<DecodeChannel*> channels)
75 const map<string, GVariant*>& Decoder::options() const
80 void Decoder::set_option(const char *id, GVariant *value)
87 bool Decoder::have_required_channels() const
89 for (DecodeChannel *ch : channels_)
90 if (!ch->assigned_signal && !ch->is_optional)
96 srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) const
98 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
99 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
101 for (const auto& option : options_) {
102 GVariant *const value = option.second;
103 g_variant_ref(value);
104 g_hash_table_replace(opt_hash, (void*)g_strdup(
105 option.first.c_str()), value);
108 srd_decoder_inst *const decoder_inst = srd_inst_new(
109 session, decoder_->id, opt_hash);
110 g_hash_table_destroy(opt_hash);
115 // Setup the channels
116 GArray *const init_pin_states = g_array_sized_new(FALSE, TRUE,
117 sizeof(uint8_t), channels_.size());
119 g_array_set_size(init_pin_states, channels_.size());
121 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
122 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
124 for (DecodeChannel *ch : channels_) {
125 init_pin_states->data[ch->id] = ch->initial_pin_state;
127 GVariant *const gvar = g_variant_new_int32(ch->id); // id = bit position
128 g_variant_ref_sink(gvar);
129 // key is channel name, value is bit position in each sample
130 g_hash_table_insert(channels, ch->pdch_->id, gvar);
133 srd_inst_channel_set_all(decoder_inst, channels);
135 srd_inst_initial_pins_set_all(decoder_inst, init_pin_states);
136 g_array_free(init_pin_states, TRUE);
141 } // namespace decode