Remove DecodeStack dependency from decode binding wrapper
[pulseview.git] / pv / data / decodesignal.cpp
index 6bc691abb5d49a29185a6b404c80bb88f22a548f..a557e1c16ebd601f8d2c65bd189ab544e0250faa 100644 (file)
@@ -36,13 +36,16 @@ using pv::data::decode::Row;
 namespace pv {
 namespace data {
 
-DecodeSignal::DecodeSignal(shared_ptr<pv::data::DecoderStack> decoder_stack) :
+DecodeSignal::DecodeSignal(shared_ptr<pv::data::DecoderStack> decoder_stack,
+       const unordered_set< shared_ptr<data::SignalBase> > &all_signals) :
        SignalBase(nullptr, SignalBase::DecodeChannel),
-       decoder_stack_(decoder_stack)
+       decoder_stack_(decoder_stack),
+       all_signals_(all_signals)
 {
        set_name(QString::fromUtf8(decoder_stack_->stack().front()->decoder()->name));
 
        update_channel_list();
+       auto_assign_signals();
 
        connect(decoder_stack_.get(), SIGNAL(new_annotations()),
                this, SLOT(on_new_annotations()));
@@ -72,7 +75,11 @@ void DecodeSignal::stack_decoder(srd_decoder *decoder)
        assert(decoder);
        assert(decoder_stack);
        decoder_stack_->push(make_shared<data::decode::Decoder>(decoder));
+
+       // Include the newly created decode channels in the channel list
        update_channel_list();
+
+       auto_assign_signals();
        decoder_stack_->begin_decode();
 }
 
@@ -103,6 +110,11 @@ bool DecodeSignal::toggle_decoder_visibility(int index)
        return state;
 }
 
+void DecodeSignal::begin_decode()
+{
+       decoder_stack_->begin_decode();
+}
+
 QString DecodeSignal::error_message() const
 {
        return decoder_stack_->error_message();
@@ -113,6 +125,19 @@ const list<data::DecodeChannel> DecodeSignal::get_channels() const
        return channels_;
 }
 
+void DecodeSignal::auto_assign_signals()
+{
+       // Try to auto-select channels that don't have signals assigned yet
+       for (data::DecodeChannel &ch : channels_) {
+               if (ch.assigned_signal)
+                       continue;
+
+               for (shared_ptr<data::SignalBase> s : all_signals_)
+                       if (s->logic_data() && (ch.name.toLower().contains(s->name().toLower())))
+                               ch.assigned_signal = s.get();
+       }
+}
+
 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
 {
        for (data::DecodeChannel &ch : channels_)
@@ -135,6 +160,44 @@ void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int in
        decoder_stack_->begin_decode();
 }
 
+int64_t DecodeSignal::sample_count() const
+{
+       shared_ptr<Logic> data;
+       shared_ptr<data::SignalBase> signalbase;
+
+       // We get the logic data of the first channel in the list.
+       // This works because we are currently assuming all
+       // LogicSignals have the same data/segment
+       for (const shared_ptr<Decoder> &dec : decoder_stack_->stack())
+               if (dec && !dec->channels().empty() &&
+                       ((signalbase = (*dec->channels().begin()).second)) &&
+                       ((data = signalbase->logic_data())))
+                       break;
+
+       if (!data || data->logic_segments().empty())
+               return 0;
+
+       const shared_ptr<LogicSegment> segment = data->logic_segments().front();
+       assert(segment);
+
+       return (int64_t)segment->get_sample_count();
+}
+
+double DecodeSignal::samplerate() const
+{
+       return decoder_stack_->samplerate();
+}
+
+const pv::util::Timestamp& DecodeSignal::start_time() const
+{
+       return decoder_stack_->start_time();
+}
+
+int64_t DecodeSignal::samples_decoded() const
+{
+       return decoder_stack_->samples_decoded();
+}
+
 vector<Row> DecodeSignal::visible_rows() const
 {
        return decoder_stack_->get_visible_rows();