From: Martin Ling Date: Wed, 12 Nov 2014 12:37:22 +0000 (+0000) Subject: bindings: Add packet constructors. X-Git-Tag: libsigrok-0.4.0~783 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=304be4a77186f4524e3894af8279690f28ffd98f;p=libsigrok.git bindings: Add packet constructors. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index bed6bed8..7becfb65 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -221,6 +221,70 @@ shared_ptr Context::create_user_device( new UserDevice(vendor, model, version), UserDevice::Deleter()); } +shared_ptr Context::create_header_packet(Glib::TimeVal start_time) +{ + auto header = g_new(struct sr_datafeed_header, 1); + header->feed_version = 1; + header->starttime.tv_sec = start_time.tv_sec; + header->starttime.tv_usec = start_time.tv_usec; + auto packet = g_new(struct sr_datafeed_packet, 1); + packet->type = SR_DF_HEADER; + packet->payload = header; + return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); +} + +shared_ptr Context::create_meta_packet( + map config) +{ + auto meta = g_new0(struct sr_datafeed_meta, 1); + for (auto input : config) + { + auto key = input.first; + auto value = input.second; + auto output = g_new(struct sr_config, 1); + output->key = key->id(); + output->data = value.gobj(); + g_variant_ref(output->data); + meta->config = g_slist_append(meta->config, output); + } + auto packet = g_new(struct sr_datafeed_packet, 1); + packet->type = SR_DF_META; + packet->payload = meta; + return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); +} + +shared_ptr Context::create_logic_packet( + void *data_pointer, size_t data_length, unsigned int unit_size) +{ + auto logic = g_new(struct sr_datafeed_logic, 1); + logic->length = data_length; + logic->unitsize = unit_size; + logic->data = data_pointer; + auto packet = g_new(struct sr_datafeed_packet, 1); + packet->type = SR_DF_LOGIC; + packet->payload = logic; + return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); +} + +shared_ptr Context::create_analog_packet( + vector > channels, + float *data_pointer, unsigned int num_samples, const Quantity *mq, + const Unit *unit, vector mqflags) +{ + auto analog = g_new0(struct sr_datafeed_analog, 1); + for (auto channel : channels) + analog->channels = g_slist_append(analog->channels, channel->_structure); + analog->num_samples = num_samples; + analog->mq = mq->id(); + analog->unit = unit->id(); + analog->mqflags = QuantityFlag::mask_from_flags(mqflags); + analog->data = data_pointer; + auto packet = g_new(struct sr_datafeed_packet, 1); + packet->type = SR_DF_ANALOG; + packet->payload = analog; + return shared_ptr(new Packet(nullptr, packet), Packet::Deleter()); +} + shared_ptr Context::load_session(string filename) { return shared_ptr( diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 6b4fbfc1..1c996703 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -267,6 +267,19 @@ public: /** Create a new user device. */ shared_ptr create_user_device( string vendor, string model, string version); + /** Create a header packet. */ + shared_ptr create_header_packet(Glib::TimeVal start_time); + /** Create a meta packet. */ + shared_ptr create_meta_packet( + map config); + /** Create a logic packet. */ + shared_ptr create_logic_packet( + void *data_pointer, size_t data_length, unsigned int unit_size); + /** Create an analog packet. */ + shared_ptr create_analog_packet( + vector > channels, + float *data_pointer, unsigned int num_samples, const Quantity *mq, + const Unit *unit, vector mqflags); /** Load a saved session. * @param filename File name string. */ shared_ptr load_session(string filename); @@ -470,6 +483,7 @@ protected: friend class ChannelGroup; friend class Session; friend class TriggerStage; + friend class Context; }; /** A group of channels on a device, which share some configuration */ @@ -741,6 +755,7 @@ protected: friend class Meta; friend class Logic; friend class Analog; + friend class Context; }; /** Abstract base class for datafeed packet payloads */