]> sigrok.org Git - pulseview.git/blobdiff - pv/session.cpp
Rework new segment notification mechanism
[pulseview.git] / pv / session.cpp
index b99f0cf73827b56b3f9c641dfa22e94dadef01f7..dd06cfed360a2bd6a45e65577d2441e30404639e 100644 (file)
@@ -91,6 +91,9 @@ using sigrok::Session;
 using Glib::VariantBase;
 
 namespace pv {
+
+shared_ptr<sigrok::Context> Session::sr_context;
+
 Session::Session(DeviceManager &device_manager, QString name) :
        device_manager_(device_manager),
        default_name_(name),
@@ -619,8 +622,6 @@ void Session::register_view(shared_ptr<views::ViewBase> view)
                                switch (signalbase->type()) {
                                case data::SignalBase::AnalogChannel:
                                case data::SignalBase::LogicChannel:
-                               case data::SignalBase::A2LChannel:
-                                       break;
                                case data::SignalBase::DecodeChannel:
 #ifdef ENABLE_DECODE
                                        trace_view->add_decode_signal(
@@ -676,6 +677,18 @@ double Session::get_samplerate() const
        return samplerate;
 }
 
+int Session::get_segment_count() const
+{
+       int min_val = INT_MAX;
+
+       // Find the lowest common number of segments
+       for (shared_ptr<data::SignalData> data : all_signal_data_)
+               if (data->get_segment_count() < min_val)
+                       min_val = data->get_segment_count();
+
+       return (min_val != INT_MAX) ? min_val : 0;
+}
+
 const unordered_set< shared_ptr<data::SignalBase> > Session::signalbases() const
 {
        return signalbases_;
@@ -887,6 +900,13 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
 
        out_of_memory_ = false;
 
+       {
+               lock_guard<recursive_mutex> lock(data_mutex_);
+               cur_logic_segment_.reset();
+               cur_analog_segments_.clear();
+       }
+       highest_segment_id_ = -1;
+
        try {
                device_->start();
        } catch (Error e) {
@@ -938,9 +958,39 @@ void Session::free_unused_memory()
        }
 }
 
+void Session::signal_new_segment()
+{
+       int new_segment_id = 1;
+
+       if ((cur_logic_segment_ != nullptr) || !cur_analog_segments_.empty()) {
+
+               // Determine new frame/segment number, assuming that all
+               // signals have the same number of frames/segments
+               if (cur_logic_segment_) {
+                       new_segment_id = logic_data_->get_segment_count();
+               } else {
+                       shared_ptr<sigrok::Channel> any_channel =
+                               (*cur_analog_segments_.begin()).first;
+
+                       shared_ptr<data::SignalBase> base = signalbase_from_channel(any_channel);
+                       assert(base);
+
+                       shared_ptr<data::Analog> data(base->analog_data());
+                       assert(data);
+
+                       new_segment_id = data->get_segment_count();
+               }
+       }
+
+       if (new_segment_id > highest_segment_id_) {
+               highest_segment_id_ = new_segment_id;
+               new_segment(highest_segment_id_);
+       }
+}
+
 void Session::feed_in_header()
 {
-       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+       // Nothing to do here for now
 }
 
 void Session::feed_in_meta(shared_ptr<Meta> meta)
@@ -989,12 +1039,26 @@ void Session::feed_in_trigger()
 
 void Session::feed_in_frame_begin()
 {
-       if (cur_logic_segment_ || !cur_analog_segments_.empty())
-               frame_began();
+       frame_began_ = true;
+}
+
+void Session::feed_in_frame_end()
+{
+       {
+               lock_guard<recursive_mutex> lock(data_mutex_);
+               cur_logic_segment_.reset();
+               cur_analog_segments_.clear();
+       }
+
+       if (frame_began_)
+               frame_began_ = false;
 }
 
 void Session::feed_in_logic(shared_ptr<Logic> logic)
 {
+       if (!cur_samplerate_)
+               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+
        lock_guard<recursive_mutex> lock(data_mutex_);
 
        if (!logic_data_) {
@@ -1013,11 +1077,7 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
                        *logic_data_, logic->unit_size(), cur_samplerate_);
                logic_data_->push_segment(cur_logic_segment_);
 
-               // @todo Putting this here means that only listeners querying
-               // for logic will be notified. Currently the only user of
-               // frame_began is DecoderStack, but in future we need to signal
-               // this after both analog and logic sweeps have begun.
-               frame_began();
+               signal_new_segment();
        }
 
        cur_logic_segment_->append_payload(logic);
@@ -1027,6 +1087,9 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
 
 void Session::feed_in_analog(shared_ptr<Analog> analog)
 {
+       if (!cur_samplerate_)
+               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+
        lock_guard<recursive_mutex> lock(data_mutex_);
 
        const vector<shared_ptr<Channel>> channels = analog->channels();
@@ -1069,6 +1132,8 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
 
                        // Push the segment into the analog data.
                        data->push_segment(segment);
+
+                       signal_new_segment();
                }
 
                assert(segment);
@@ -1089,8 +1154,6 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
 void Session::data_feed_in(shared_ptr<sigrok::Device> device,
        shared_ptr<Packet> packet)
 {
-       static bool frame_began = false;
-
        (void)device;
 
        assert(device);
@@ -1110,11 +1173,6 @@ void Session::data_feed_in(shared_ptr<sigrok::Device> device,
                feed_in_trigger();
                break;
 
-       case SR_DF_FRAME_BEGIN:
-               feed_in_frame_begin();
-               frame_began = true;
-               break;
-
        case SR_DF_LOGIC:
                try {
                        feed_in_logic(dynamic_pointer_cast<Logic>(packet->payload()));
@@ -1133,20 +1191,25 @@ void Session::data_feed_in(shared_ptr<sigrok::Device> device,
                }
                break;
 
+       case SR_DF_FRAME_BEGIN:
+               feed_in_frame_begin();
+               break;
+
        case SR_DF_FRAME_END:
+               feed_in_frame_end();
+               break;
+
        case SR_DF_END:
-       {
+               // Strictly speaking, this is performed when a frame end marker was
+               // received, so there's no point doing this again. However, not all
+               // devices use frames, and for those devices, we need to do it here.
                {
                        lock_guard<recursive_mutex> lock(data_mutex_);
                        cur_logic_segment_.reset();
                        cur_analog_segments_.clear();
                }
-               if (frame_began) {
-                       frame_began = false;
-                       frame_ended();
-               }
                break;
-       }
+
        default:
                break;
        }