From: Soeren Apel Date: Sat, 1 Jul 2017 22:48:31 +0000 (+0200) Subject: Make sigrok::Context semi-global and use libsigrok A2L methods X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=419ec4e11ee29e27566d1c0c17741fcad85dbdf6;p=pulseview.git Make sigrok::Context semi-global and use libsigrok A2L methods --- diff --git a/main.cpp b/main.cpp index f7c722b1..8f495419 100644 --- 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 #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 diff --git a/pv/data/signalbase.cpp b/pv/data/signalbase.cpp index 0f3c8f1b..ee1112d5 100644 --- a/pv/data/signalbase.cpp +++ b/pv/data/signalbase.cpp @@ -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 lsamples; - lsamples.reserve(ConversionBlockSize); + // Create sigrok::Analog instance + float *asamples = new float[ConversionBlockSize]; + uint8_t *lsamples = new uint8_t[ConversionBlockSize]; + + vector > channels; + channels.push_back(channel_); + + vector mq_flags; + const sigrok::Quantity * const mq = sigrok::Quantity::VOLTAGE; + const sigrok::Unit * const unit = sigrok::Unit::VOLT; + + shared_ptr packet = + Session::sr_context->create_analog_packet(channels, + asamples, ConversionBlockSize, mq, unit, mq_flags); + + shared_ptr analog = + dynamic_pointer_cast(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 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(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 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 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(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 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; } } diff --git a/pv/session.cpp b/pv/session.cpp index b99f0cf7..e258f6db 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -91,6 +91,9 @@ using sigrok::Session; using Glib::VariantBase; namespace pv { + +shared_ptr Session::sr_context; + Session::Session(DeviceManager &device_manager, QString name) : device_manager_(device_manager), default_name_(name), diff --git a/pv/session.hpp b/pv/session.hpp index 424bf637..fa96ce64 100644 --- a/pv/session.hpp +++ b/pv/session.hpp @@ -100,6 +100,8 @@ public: Running }; + static shared_ptr sr_context; + public: Session(DeviceManager &device_manager, QString name);