]> sigrok.org Git - pulseview.git/commitdiff
Make sigrok::Context semi-global and use libsigrok A2L methods
authorSoeren Apel <redacted>
Sat, 1 Jul 2017 22:48:31 +0000 (00:48 +0200)
committerUwe Hermann <redacted>
Wed, 5 Jul 2017 22:37:08 +0000 (00:37 +0200)
main.cpp
pv/data/signalbase.cpp
pv/session.cpp
pv/session.hpp

index f7c722b1aec63a6e47d440a565bb23824fadd461..8f495419fee7467de77c4074c32927ce658de919 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -36,6 +36,7 @@
 #include "pv/application.hpp"
 #include "pv/devicemanager.hpp"
 #include "pv/mainwindow.hpp"
+#include "pv/session.hpp"
 #ifdef ANDROID
 #include <libsigrokandroidutils/libsigrokandroidutils.h>
 #include "android/assetreader.hpp"
@@ -162,6 +163,8 @@ int main(int argc, char *argv[])
 
        // Initialise libsigrok
        context = sigrok::Context::create();
+       pv::Session::sr_context = context;
+
 #ifdef ANDROID
        context->set_resource_reader(&asset_reader);
 #endif
index 0f3c8f1be1d32b6beb316488c2067d77b70011f4..ee1112d5d5260a67b745a8656590ca023fbeffdb 100644 (file)
@@ -221,22 +221,6 @@ void SignalBase::restore_settings(QSettings &settings)
        set_conversion_type((ConversionType)settings.value("conversion_type").toInt());
 }
 
-uint8_t SignalBase::convert_a2l_threshold(float threshold, float value)
-{
-       return (value >= threshold) ? 1 : 0;
-}
-
-uint8_t SignalBase::convert_a2l_schmitt_trigger(float lo_thr, float hi_thr,
-       float value, uint8_t &state)
-{
-       if (value < lo_thr)
-               state = 0;
-       else if (value > hi_thr)
-               state = 1;
-
-       return state;
-}
-
 bool SignalBase::conversion_is_a2l() const
 {
        return ((channel_type_ == AnalogChannel) &&
@@ -274,10 +258,25 @@ void SignalBase::conversion_thread_proc(QObject* segment)
                                float min_v, max_v;
                                tie(min_v, max_v) = asegment->get_min_max();
 
-                               float* asamples = new float[ConversionBlockSize];
-                               vector<uint8_t> lsamples;
-                               lsamples.reserve(ConversionBlockSize);
+                               // Create sigrok::Analog instance
+                               float *asamples = new float[ConversionBlockSize];
+                               uint8_t *lsamples = new uint8_t[ConversionBlockSize];
+
+                               vector<shared_ptr<sigrok::Channel> > channels;
+                               channels.push_back(channel_);
+
+                               vector<const sigrok::QuantityFlag*> mq_flags;
+                               const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE;
+                               const sigrok::Unit * const unit = sigrok::Unit::VOLT;
+
+                               shared_ptr<sigrok::Packet> packet =
+                                       Session::sr_context->create_analog_packet(channels,
+                                       asamples, ConversionBlockSize, mq, unit, mq_flags);
+
+                               shared_ptr<sigrok::Analog> analog =
+                                       dynamic_pointer_cast<sigrok::Analog>(packet->payload());
 
+                               // Convert
                                uint64_t i = start_sample;
 
                                if (conversion_type_ == A2LConversionByTreshold) {
@@ -286,19 +285,26 @@ void SignalBase::conversion_thread_proc(QObject* segment)
                                        // Convert as many sample blocks as we can
                                        while ((end_sample - i) > ConversionBlockSize) {
                                                asegment->get_samples(i, i + ConversionBlockSize, asamples);
-                                               for (uint32_t j = 0; j < ConversionBlockSize; j++)
-                                                       lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
-                                               lsegment->append_payload(lsamples.data(), lsamples.size());
+
+                                               shared_ptr<sigrok::Logic> logic =
+                                                       analog->get_logic_via_threshold(threshold, lsamples);
+
+                                               lsegment->append_payload(logic->data_pointer(), logic->data_length());
+
                                                samples_added(lsegment, i, i + ConversionBlockSize);
                                                i += ConversionBlockSize;
-                                               lsamples.clear();
                                        }
 
-                                       // Convert remaining samples
+                                       // Re-create sigrok::Analog and convert remaining samples
+                                       packet = Session::sr_context->create_analog_packet(channels,
+                                               asamples, end_sample - i, mq, unit, mq_flags);
+
+                                       analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
+
                                        asegment->get_samples(i, end_sample, asamples);
-                                       for (uint32_t j = 0; j < (end_sample - i); j++)
-                                               lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
-                                       lsegment->append_payload(lsamples.data(), lsamples.size());
+                                       shared_ptr<sigrok::Logic> logic =
+                                               analog->get_logic_via_threshold(threshold, lsamples);
+                                       lsegment->append_payload(logic->data_pointer(), logic->data_length());
                                        samples_added(lsegment, i, end_sample);
                                }
 
@@ -311,25 +317,35 @@ void SignalBase::conversion_thread_proc(QObject* segment)
                                        // Convert as many sample blocks as we can
                                        while ((end_sample - i) > ConversionBlockSize) {
                                                asegment->get_samples(i, i + ConversionBlockSize, asamples);
-                                               for (uint32_t j = 0; j < ConversionBlockSize; j++)
-                                                       lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
-                                               lsegment->append_payload(lsamples.data(), lsamples.size());
+
+                                               shared_ptr<sigrok::Logic> logic =
+                                                       analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
+                                                               &state, lsamples);
+
+                                               lsegment->append_payload(logic->data_pointer(), logic->data_length());
+
                                                samples_added(lsegment, i, i + ConversionBlockSize);
                                                i += ConversionBlockSize;
-                                               lsamples.clear();
                                        }
 
-                                       // Convert remaining samples
+                                       // Re-create sigrok::Analog and convert remaining samples
+                                       packet = Session::sr_context->create_analog_packet(channels,
+                                               asamples, end_sample - i, mq, unit, mq_flags);
+
+                                       analog = dynamic_pointer_cast<sigrok::Analog>(packet->payload());
+
                                        asegment->get_samples(i, end_sample, asamples);
-                                       for (uint32_t j = 0; j < (end_sample - i); j++)
-                                               lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
-                                       lsegment->append_payload(lsamples.data(), lsamples.size());
+                                       shared_ptr<sigrok::Logic> logic =
+                                               analog->get_logic_via_schmitt_trigger(lo_thr, hi_thr,
+                                                       &state, lsamples);
+                                       lsegment->append_payload(logic->data_pointer(), logic->data_length());
                                        samples_added(lsegment, i, end_sample);
                                }
 
                                // If acquisition is ongoing, start-/endsample may have changed
                                end_sample = asegment->get_sample_count();
 
+                               delete[] lsamples;
                                delete[] asamples;
                        }
                }
index b99f0cf73827b56b3f9c641dfa22e94dadef01f7..e258f6db8e189fb410bcd696d123f31fb295f40e 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),
index 424bf6375cd0d14850df3714ad0d1f41c87cc9b4..fa96ce641bd418f4ca7448986d1d66cf10ecc538 100644 (file)
@@ -100,6 +100,8 @@ public:
                Running
        };
 
+       static shared_ptr<sigrok::Context> sr_context;
+
 public:
        Session(DeviceManager &device_manager, QString name);