]> sigrok.org Git - pulseview.git/blobdiff - pv/data/decodesignal.cpp
Re-use DecodeTrace::ChannelSelector as DecodeChannel
[pulseview.git] / pv / data / decodesignal.cpp
index 253e3a987c6dbeb59e1f6344deff7d76c5aa4692..6bc691abb5d49a29185a6b404c80bb88f22a548f 100644 (file)
@@ -42,6 +42,8 @@ DecodeSignal::DecodeSignal(shared_ptr<pv::data::DecoderStack> decoder_stack) :
 {
        set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
 
 {
        set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
 
+       update_channel_list();
+
        connect(decoder_stack_.get(), SIGNAL(new_annotations()),
                this, SLOT(on_new_annotations()));
 }
        connect(decoder_stack_.get(), SIGNAL(new_annotations()),
                this, SLOT(on_new_annotations()));
 }
@@ -70,12 +72,14 @@ void DecodeSignal::stack_decoder(srd_decoder *decoder)
        assert(decoder);
        assert(decoder_stack);
        decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
        assert(decoder);
        assert(decoder_stack);
        decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
+       update_channel_list();
        decoder_stack_->begin_decode();
 }
 
 void DecodeSignal::remove_decoder(int index)
 {
        decoder_stack_->remove(index);
        decoder_stack_->begin_decode();
 }
 
 void DecodeSignal::remove_decoder(int index)
 {
        decoder_stack_->remove(index);
+       update_channel_list();
        decoder_stack_->begin_decode();
 }
 
        decoder_stack_->begin_decode();
 }
 
@@ -104,6 +108,33 @@ QString DecodeSignal::error_message() const
        return decoder_stack_->error_message();
 }
 
        return decoder_stack_->error_message();
 }
 
+const list<data::DecodeChannel> DecodeSignal::get_channels() const
+{
+       return channels_;
+}
+
+void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
+{
+       for (data::DecodeChannel &ch : channels_)
+               if (ch.id == channel_id)
+                       ch.assigned_signal = signal;
+
+       channels_updated();
+
+       decoder_stack_->begin_decode();
+}
+
+void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
+{
+       for (data::DecodeChannel &ch : channels_)
+               if (ch.id == channel_id)
+                       ch.initial_pin_state = init_state;
+
+       channels_updated();
+
+       decoder_stack_->begin_decode();
+}
+
 vector<Row> DecodeSignal::visible_rows() const
 {
        return decoder_stack_->get_visible_rows();
 vector<Row> DecodeSignal::visible_rows() const
 {
        return decoder_stack_->get_visible_rows();
@@ -118,6 +149,68 @@ void DecodeSignal::get_annotation_subset(
                start_sample, end_sample);
 }
 
                start_sample, end_sample);
 }
 
+void DecodeSignal::update_channel_list()
+{
+       list<data::DecodeChannel> prev_channels = channels_;
+       channels_.clear();
+
+       uint16_t id = 0;
+
+       // Copy existing entries, create new as needed
+       for (shared_ptr<Decoder> decoder : decoder_stack_->stack()) {
+               const srd_decoder* srd_d = decoder->decoder();
+               const GSList *l;
+
+               // Mandatory channels
+               for (l = srd_d->channels; l; l = l->next) {
+                       const struct srd_channel *const pdch = (struct srd_channel *)l->data;
+                       bool ch_added = false;
+
+                       // Copy but update ID if this channel was in the list before
+                       for (data::DecodeChannel ch : prev_channels)
+                               if (ch.pdch_ == pdch) {
+                                       ch.id = id++;
+                                       channels_.push_back(ch);
+                                       ch_added = true;
+                                       break;
+                               }
+
+                       if (!ch_added) {
+                               // Create new entry without a mapped signal
+                               data::DecodeChannel ch = {id++, false, nullptr,
+                                       QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
+                                       SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
+                               channels_.push_back(ch);
+                       }
+               }
+
+               // Optional channels
+               for (l = srd_d->opt_channels; l; l = l->next) {
+                       const struct srd_channel *const pdch = (struct srd_channel *)l->data;
+                       bool ch_added = false;
+
+                       // Copy but update ID if this channel was in the list before
+                       for (data::DecodeChannel ch : prev_channels)
+                               if (ch.pdch_ == pdch) {
+                                       ch.id = id++;
+                                       channels_.push_back(ch);
+                                       ch_added = true;
+                                       break;
+                               }
+
+                       if (!ch_added) {
+                               // Create new entry without a mapped signal
+                               data::DecodeChannel ch = {id++, true, nullptr,
+                                       QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
+                                       SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
+                               channels_.push_back(ch);
+                       }
+               }
+       }
+
+       channels_updated();
+}
+
 void DecodeSignal::on_new_annotations()
 {
        // Forward the signal to the frontend
 void DecodeSignal::on_new_annotations()
 {
        // Forward the signal to the frontend