]> sigrok.org Git - pulseview.git/commitdiff
Session: Made update_signals non-destructive
authorJoel Holdsworth <redacted>
Sat, 28 Mar 2015 12:54:41 +0000 (12:54 +0000)
committerJoel Holdsworth <redacted>
Thu, 28 May 2015 14:59:04 +0000 (15:59 +0100)
pv/session.cpp

index e0096e9cd1245c3b233e65c6afc529b004ac9f91..70637437e08b5ad1ea867e58ae4573956cc30bb9 100644 (file)
@@ -352,9 +352,6 @@ void Session::update_signals(shared_ptr<Device> device)
        assert(device);
        assert(capture_state_ == Stopped);
 
-       // Clear the decode traces
-       decode_traces_.clear();
-
        // Detect what data types we will receive
        auto channels = device->channels();
        unsigned int logic_channel_count = std::count_if(
@@ -366,8 +363,10 @@ void Session::update_signals(shared_ptr<Device> device)
        {
                lock_guard<mutex> data_lock(data_mutex_);
 
-               logic_data_.reset();
-               if (logic_channel_count != 0) {
+               if (logic_channel_count == 0) {
+                       logic_data_.reset();
+               } else if (!logic_data_ ||
+                       logic_data_->num_channels() != logic_channel_count) {
                        logic_data_.reset(new data::Logic(
                                logic_channel_count));
                        assert(logic_data_);
@@ -378,37 +377,55 @@ void Session::update_signals(shared_ptr<Device> device)
        {
                unique_lock<shared_mutex> lock(signals_mutex_);
 
+               unordered_set< shared_ptr<view::Signal> > prev_sigs(signals_);
                signals_.clear();
 
                for (auto channel : device->channels()) {
                        shared_ptr<view::Signal> signal;
 
-                       switch(channel->type()->id()) {
-                       case SR_CHANNEL_LOGIC:
-                               signal = shared_ptr<view::Signal>(
-                                       new view::LogicSignal(*this, device,
-                                               channel, logic_data_));
-                               break;
-
-                       case SR_CHANNEL_ANALOG:
-                       {
-                               shared_ptr<data::Analog> data(
-                                       new data::Analog());
-                               signal = shared_ptr<view::Signal>(
-                                       new view::AnalogSignal(
-                                               *this, channel, data));
-                               break;
-                       }
-
-                       default:
-                               assert(0);
-                               break;
+                       // Find the channel in the old signals
+                       const auto iter = std::find_if(
+                               prev_sigs.cbegin(), prev_sigs.cend(),
+                               [&](const shared_ptr<view::Signal> &s) {
+                                       return s->channel() == channel;
+                               });
+                       if (iter != prev_sigs.end()) {
+                               // Copy the signal from the old set to the new
+                               signal = *iter;
+                               auto logic_signal = dynamic_pointer_cast<
+                                       view::LogicSignal>(signal);
+                               if (logic_signal)
+                                       logic_signal->set_logic_data(
+                                               logic_data_);
+                       } else {
+                               // Create a new signal
+                               switch(channel->type()->id()) {
+                               case SR_CHANNEL_LOGIC:
+                                       signal = shared_ptr<view::Signal>(
+                                               new view::LogicSignal(*this,
+                                                       device, channel,
+                                                       logic_data_));
+                                       break;
+
+                               case SR_CHANNEL_ANALOG:
+                               {
+                                       shared_ptr<data::Analog> data(
+                                               new data::Analog());
+                                       signal = shared_ptr<view::Signal>(
+                                               new view::AnalogSignal(
+                                                       *this, channel, data));
+                                       break;
+                               }
+
+                               default:
+                                       assert(0);
+                                       break;
+                               }
                        }
 
                        assert(signal);
                        signals_.insert(signal);
                }
-
        }
 
        signals_changed();