]> sigrok.org Git - pulseview.git/blobdiff - pv/data/logicsegment.cpp
Allow re-loading sessions to work with PD output signals
[pulseview.git] / pv / data / logicsegment.cpp
index a1832576916ae1dabad15ceb464768cb3521a205..354c14f85d8bef035fd3a184757aabfb5d304c0b 100644 (file)
@@ -63,10 +63,24 @@ LogicSegment::LogicSegment(pv::data::Logic& owner, uint32_t segment_id,
 LogicSegment::~LogicSegment()
 {
        lock_guard<recursive_mutex> lock(mutex_);
+
        for (MipMapLevel &l : mip_map_)
                free(l.data);
 }
 
+shared_ptr<const LogicSegment> LogicSegment::get_shared_ptr() const
+{
+       shared_ptr<const Segment> ptr = nullptr;
+
+       try {
+               ptr = shared_from_this();
+       } catch (std::exception& e) {
+               /* Do nothing, ptr remains a null pointer */
+       }
+
+       return ptr ? std::dynamic_pointer_cast<const LogicSegment>(ptr) : nullptr;
+}
+
 template <class T>
 void LogicSegment::downsampleTmain(const T*&in, T &acc, T &prev)
 {
@@ -328,6 +342,7 @@ void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
 
 void LogicSegment::append_payload(void *data, uint64_t data_size)
 {
+       assert(unit_size_ > 0);
        assert((data_size % unit_size_) == 0);
 
        lock_guard<recursive_mutex> lock(mutex_);
@@ -341,13 +356,36 @@ void LogicSegment::append_payload(void *data, uint64_t data_size)
        append_payload_to_mipmap();
 
        if (sample_count > 1)
-               owner_.notify_samples_added(shared_ptr<Segment>(shared_from_this()),
+               owner_.notify_samples_added(SharedPtrToSegment(shared_from_this()),
                        prev_sample_count + 1, prev_sample_count + 1 + sample_count);
        else
-               owner_.notify_samples_added(shared_ptr<Segment>(shared_from_this()),
+               owner_.notify_samples_added(SharedPtrToSegment(shared_from_this()),
                        prev_sample_count + 1, prev_sample_count + 1);
 }
 
+void LogicSegment::append_subsignal_payload(unsigned int index, void *data, uint64_t data_size)
+{
+       static vector<uint8_t> merged_data;  // Using static also places it on the heap
+
+       for (uint64_t i = 0; i < data_size * unit_size_; i++)
+               merged_data.emplace_back(0);
+
+       // Set the bits for this sub-signal where needed
+       // Note: the bytes in *data must either be 0 or 1, nothing else
+       unsigned int index_byte = index / 8;
+       for (uint64_t i = 0; i < data_size; i++) {
+               unsigned int offs = i * unit_size_ + index_byte;
+               uint8_t* data_byte = merged_data.data() + offs;
+               *data_byte |= *((uint8_t*)data + i) << index;
+       }
+
+       if (index == owner_.num_channels() - 1) {
+               // We gathered sample data of all sub-signals, let's append it
+               append_payload(merged_data.data(), merged_data.size());
+               merged_data.clear();
+       }
+}
+
 void LogicSegment::get_samples(int64_t start_sample,
        int64_t end_sample, uint8_t* dest) const
 {
@@ -628,7 +666,7 @@ void LogicSegment::append_payload_to_mipmap()
                else if (unit_size_ == 4)
                        downsampleT<uint32_t>(src_ptr, dest_ptr, count);
                else if (unit_size_ == 8)
-                       downsampleT<uint8_t>(src_ptr, dest_ptr, count);
+                       downsampleT<uint64_t>(src_ptr, dest_ptr, count);
                else
                        downsampleGeneric(src_ptr, dest_ptr, count);
                len_sample -= count;