]> sigrok.org Git - pulseview.git/blobdiff - pv/data/decodesignal.cpp
Implement MathSignal
[pulseview.git] / pv / data / decodesignal.cpp
index 28157794208c6fbf4a7b65be20b76773bb21b189..a17ea693aaa89d67ca3859751529e84b75b42eb2 100644 (file)
@@ -56,7 +56,8 @@ DecodeSignal::DecodeSignal(pv::Session &session) :
        srd_session_(nullptr),
        logic_mux_data_invalid_(false),
        stack_config_changed_(true),
-       current_segment_id_(0)
+       current_segment_id_(0),
+       error_message_("")
 {
        connect(&session_, SIGNAL(capture_state_changed(int)),
                this, SLOT(on_capture_state_changed(int)));
@@ -395,18 +396,20 @@ int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const
 
        for (const decode::DecodeChannel& ch : channels_)
                if (ch.assigned_signal) {
+                       if (!ch.assigned_signal->logic_data())
+                               return 0;
+
                        no_signals_assigned = false;
 
                        const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
-                       if (!logic_data || logic_data->logic_segments().empty())
+                       if (logic_data->logic_segments().empty())
                                return 0;
 
-                       try {
-                               const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id);
-                               count = min(count, (int64_t)segment->get_sample_count());
-                       } catch (out_of_range&) {
+                       if (segment_id >= logic_data->logic_segments().size())
                                return 0;
-                       }
+
+                       const shared_ptr<LogicSegment> segment = logic_data->logic_segments()[segment_id];
+                       count = min(count, (int64_t)segment->get_sample_count());
                }
 
        return (no_signals_assigned ? 0 : count);
@@ -513,18 +516,14 @@ void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest,
 uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id,
        const Decoder* dec, uint32_t bin_class_id) const
 {
-       if (segments_.size() == 0)
+       if ((segments_.size() == 0) || (segment_id >= segments_.size()))
                return 0;
 
-       try {
-               const DecodeSegment *segment = &(segments_.at(segment_id));
+       const DecodeSegment *segment = &(segments_[segment_id]);
 
-               for (const DecodeBinaryClass& bc : segment->binary_classes)
-                       if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
-                               return bc.chunks.size();
-       } catch (out_of_range&) {
-               // Do nothing
-       }
+       for (const DecodeBinaryClass& bc : segment->binary_classes)
+               if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
+                       return bc.chunks.size();
 
        return 0;
 }
@@ -533,18 +532,17 @@ void DecodeSignal::get_binary_data_chunk(uint32_t segment_id,
        const  Decoder* dec, uint32_t bin_class_id, uint32_t chunk_id,
        const vector<uint8_t> **dest, uint64_t *size)
 {
-       try {
-               const DecodeSegment *segment = &(segments_.at(segment_id));
+       if (segment_id >= segments_.size())
+               return;
 
-               for (const DecodeBinaryClass& bc : segment->binary_classes)
-                       if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) {
-                               if (dest) *dest = &(bc.chunks.at(chunk_id).data);
-                               if (size) *size = bc.chunks.at(chunk_id).data.size();
-                               return;
-                       }
-       } catch (out_of_range&) {
-               // Do nothing
-       }
+       const DecodeSegment *segment = &(segments_[segment_id]);
+
+       for (const DecodeBinaryClass& bc : segment->binary_classes)
+               if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) {
+                       if (dest) *dest = &(bc.chunks.at(chunk_id).data);
+                       if (size) *size = bc.chunks.at(chunk_id).data.size();
+                       return;
+               }
 }
 
 void DecodeSignal::get_merged_binary_data_chunks_by_sample(uint32_t segment_id,
@@ -553,39 +551,38 @@ void DecodeSignal::get_merged_binary_data_chunks_by_sample(uint32_t segment_id,
 {
        assert(dest != nullptr);
 
-       try {
-               const DecodeSegment *segment = &(segments_.at(segment_id));
-
-               const DecodeBinaryClass* bin_class = nullptr;
-               for (const DecodeBinaryClass& bc : segment->binary_classes)
-                       if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
-                               bin_class = &bc;
-
-               // Determine overall size before copying to resize dest vector only once
-               uint64_t size = 0;
-               uint64_t matches = 0;
-               for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
-                       if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
-                               size += chunk.data.size();
-                               matches++;
-                       }
-               dest->resize(size);
-
-               uint64_t offset = 0;
-               uint64_t matches2 = 0;
-               for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
-                       if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
-                               memcpy(dest->data() + offset, chunk.data.data(), chunk.data.size());
-                               offset += chunk.data.size();
-                               matches2++;
-
-                               // Make sure we don't overwrite memory if the array grew in the meanwhile
-                               if (matches2 == matches)
-                                       break;
-                       }
-       } catch (out_of_range&) {
-               // Do nothing
-       }
+       if (segment_id >= segments_.size())
+               return;
+
+       const DecodeSegment *segment = &(segments_[segment_id]);
+
+       const DecodeBinaryClass* bin_class = nullptr;
+       for (const DecodeBinaryClass& bc : segment->binary_classes)
+               if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
+                       bin_class = &bc;
+
+       // Determine overall size before copying to resize dest vector only once
+       uint64_t size = 0;
+       uint64_t matches = 0;
+       for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
+               if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
+                       size += chunk.data.size();
+                       matches++;
+               }
+       dest->resize(size);
+
+       uint64_t offset = 0;
+       uint64_t matches2 = 0;
+       for (const DecodeBinaryDataChunk& chunk : bin_class->chunks)
+               if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) {
+                       memcpy(dest->data() + offset, chunk.data.data(), chunk.data.size());
+                       offset += chunk.data.size();
+                       matches2++;
+
+                       // Make sure we don't overwrite memory if the array grew in the meanwhile
+                       if (matches2 == matches)
+                               break;
+               }
 }
 
 void DecodeSignal::get_merged_binary_data_chunks_by_offset(uint32_t segment_id,
@@ -594,54 +591,52 @@ void DecodeSignal::get_merged_binary_data_chunks_by_offset(uint32_t segment_id,
 {
        assert(dest != nullptr);
 
-       try {
-               const DecodeSegment *segment = &(segments_.at(segment_id));
-
-               const DecodeBinaryClass* bin_class = nullptr;
-               for (const DecodeBinaryClass& bc : segment->binary_classes)
-                       if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
-                               bin_class = &bc;
-
-               // Determine overall size before copying to resize dest vector only once
-               uint64_t size = 0;
-               uint64_t offset = 0;
-               for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
-                       if (offset >= start)
-                               size += chunk.data.size();
-                       offset += chunk.data.size();
-                       if (offset >= end)
-                               break;
-               }
-               dest->resize(size);
-
-               offset = 0;
-               uint64_t dest_offset = 0;
-               for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
-                       if (offset >= start) {
-                               memcpy(dest->data() + dest_offset, chunk.data.data(), chunk.data.size());
-                               dest_offset += chunk.data.size();
-                       }
-                       offset += chunk.data.size();
-                       if (offset >= end)
-                               break;
+       if (segment_id >= segments_.size())
+               return;
+
+       const DecodeSegment *segment = &(segments_[segment_id]);
+
+       const DecodeBinaryClass* bin_class = nullptr;
+       for (const DecodeBinaryClass& bc : segment->binary_classes)
+               if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
+                       bin_class = &bc;
+
+       // Determine overall size before copying to resize dest vector only once
+       uint64_t size = 0;
+       uint64_t offset = 0;
+       for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
+               if (offset >= start)
+                       size += chunk.data.size();
+               offset += chunk.data.size();
+               if (offset >= end)
+                       break;
+       }
+       dest->resize(size);
+
+       offset = 0;
+       uint64_t dest_offset = 0;
+       for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) {
+               if (offset >= start) {
+                       memcpy(dest->data() + dest_offset, chunk.data.data(), chunk.data.size());
+                       dest_offset += chunk.data.size();
                }
-       } catch (out_of_range&) {
-               // Do nothing
+               offset += chunk.data.size();
+               if (offset >= end)
+                       break;
        }
 }
 
 const DecodeBinaryClass* DecodeSignal::get_binary_data_class(uint32_t segment_id,
        const Decoder* dec, uint32_t bin_class_id) const
 {
-       try {
-               const DecodeSegment *segment = &(segments_.at(segment_id));
+       if (segment_id >= segments_.size())
+               return nullptr;
 
-               for (const DecodeBinaryClass& bc : segment->binary_classes)
-                       if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
-                               return &bc;
-       } catch (out_of_range&) {
-               // Do nothing
-       }
+       const DecodeSegment *segment = &(segments_[segment_id]);
+
+       for (const DecodeBinaryClass& bc : segment->binary_classes)
+               if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id))
+                       return &bc;
 
        return nullptr;
 }
@@ -649,14 +644,12 @@ const DecodeBinaryClass* DecodeSignal::get_binary_data_class(uint32_t segment_id
 const deque<const Annotation*>* DecodeSignal::get_all_annotations_by_segment(
        uint32_t segment_id) const
 {
-       try {
-               const DecodeSegment *segment = &(segments_.at(segment_id));
-               return &(segment->all_annotations);
-       } catch (out_of_range&) {
-               // Do nothing
-       }
+       if (segment_id >= segments_.size())
+               return nullptr;
 
-       return nullptr;
+       const DecodeSegment *segment = &(segments_[segment_id]);
+
+       return &(segment->all_annotations);
 }
 
 void DecodeSignal::save_settings(QSettings &settings) const
@@ -822,7 +815,7 @@ void DecodeSignal::restore_settings(QSettings &settings)
                QString assigned_signal_name = settings.value("assigned_signal_name").toString();
 
                for (const shared_ptr<data::SignalBase>& signal : signalbases)
-                       if (signal->name() == assigned_signal_name)
+                       if ((signal->name() == assigned_signal_name) && (signal->type() != SignalBase::DecodeChannel))
                                channel->assigned_signal = signal.get();
 
                channel->initial_pin_state = settings.value("initial_pin_state").toInt();
@@ -866,7 +859,7 @@ uint32_t DecodeSignal::get_input_segment_count() const
        return (no_signals_assigned ? 0 : count);
 }
 
-uint32_t DecodeSignal::get_input_samplerate(uint32_t segment_id) const
+double DecodeSignal::get_input_samplerate(uint32_t segment_id) const
 {
        double samplerate = 0;
 
@@ -1136,7 +1129,6 @@ void DecodeSignal::logic_mux_proc()
                                logic_mux_data_->push_segment(output_segment);
 
                                output_segment->set_samplerate(get_input_samplerate(segment_id));
-
                        } else {
                                // All segments have been processed
                                logic_mux_data_invalid_ = false;