X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2Fclasses.cpp;h=d0e2dd41499324923698a998fb92cf0c21474cea;hb=bb0c52719e43d4f81b63dbeea906ff89183cde21;hp=5e07c2c6f8744ad32ea0e388497bb851e2b536f2;hpb=b6b4f03e404e06430eca810ad2bf7ccc84262b6e;p=libsigrok.git diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 5e07c2c6..d0e2dd41 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -157,8 +157,7 @@ string Context::lib_version() map> Context::drivers() { map> result; - for (const auto &entry: _drivers) - { + for (const auto &entry: _drivers) { const auto &name = entry.first; const auto &driver = entry.second; result.insert({name, driver->share_owned_by(shared_from_this())}); @@ -169,8 +168,7 @@ map> Context::drivers() map> Context::input_formats() { map> result; - for (const auto &entry: _input_formats) - { + for (const auto &entry: _input_formats) { const auto &name = entry.first; const auto &input_format = entry.second; result.insert({name, input_format->share_owned_by(shared_from_this())}); @@ -181,8 +179,7 @@ map> Context::input_formats() map> Context::output_formats() { map> result; - for (const auto &entry: _output_formats) - { + for (const auto &entry: _output_formats) { const auto &name = entry.first; const auto &output_format = entry.second; result.insert({name, output_format->share_owned_by(shared_from_this())}); @@ -213,12 +210,9 @@ static int call_log_callback(void *cb_data, int loglevel, auto *const callback = static_cast(cb_data); - try - { + try { (*callback)(LogLevel::get(loglevel), message.get()); - } - catch (Error e) - { + } catch (Error e) { return e.result; } @@ -281,8 +275,7 @@ shared_ptr Context::create_meta_packet( map config) { auto meta = g_new0(struct sr_datafeed_meta, 1); - for (const auto &input : config) - { + for (const auto &input : config) { const auto &key = input.first; const auto &value = input.second; auto *const output = g_new(struct sr_config, 1); @@ -446,16 +439,14 @@ vector> Driver::scan( map options) { /* Initialise the driver if not yet done. */ - if (!_initialized) - { + if (!_initialized) { check(sr_driver_init(_parent->_structure, _structure)); _initialized = true; } /* Translate scan options to GSList of struct sr_config pointers. */ GSList *option_list = nullptr; - for (const auto &entry : options) - { + for (const auto &entry : options) { const auto &key = entry.first; const auto &value = entry.second; auto *const config = g_new(struct sr_config, 1); @@ -473,8 +464,7 @@ vector> Driver::scan( /* Create device objects. */ vector> result; - for (GSList *device = device_list; device; device = device->next) - { + for (GSList *device = device_list; device; device = device->next) { auto *const sdi = static_cast(device->data); shared_ptr hwdev { new HardwareDevice{shared_from_this(), sdi}, @@ -570,15 +560,13 @@ Device::Device(struct sr_dev_inst *structure) : Configurable(sr_dev_inst_driver_get(structure), structure, nullptr), _structure(structure) { - for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) - { + for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) { auto *const ch = static_cast(entry->data); unique_ptr channel {new Channel{ch}}; _channels.insert(make_pair(ch, move(channel))); } - for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) - { + for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) { auto *const cg = static_cast(entry->data); unique_ptr group {new ChannelGroup{this, cg}}; _channel_groups.insert(make_pair(group->name(), move(group))); @@ -586,7 +574,8 @@ Device::Device(struct sr_dev_inst *structure) : } Device::~Device() -{} +{ +} string Device::vendor() const { @@ -632,8 +621,7 @@ map> Device::channel_groups() { map> result; - for (const auto &entry: _channel_groups) - { + for (const auto &entry: _channel_groups) { const auto &name = entry.first; const auto &channel_group = entry.second; result.insert({name, channel_group->share_owned_by(get_shared_from_this())}); @@ -768,7 +756,7 @@ vector> ChannelGroup::channels() return result; } -Trigger::Trigger(shared_ptr context, string name) : +Trigger::Trigger(shared_ptr context, string name) : _structure(sr_trigger_new(name.c_str())), _context(move(context)) { @@ -812,7 +800,7 @@ TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : TriggerStage::~TriggerStage() { } - + int TriggerStage::number() const { return _structure->stage; @@ -1212,6 +1200,11 @@ void *Analog::data_pointer() return _structure->data; } +void Analog::get_data_as_float(float *dest) +{ + check(sr_analog_to_float(_structure, dest)); +} + unsigned int Analog::num_samples() const { return _structure->num_samples; @@ -1227,6 +1220,58 @@ vector> Analog::channels() return result; } +unsigned int Analog::unitsize() const +{ + return _structure->encoding->unitsize; +} + +bool Analog::is_signed() const +{ + return _structure->encoding->is_signed; +} + +bool Analog::is_float() const +{ + return _structure->encoding->is_float; +} + +bool Analog::is_bigendian() const +{ + return _structure->encoding->is_bigendian; +} + +int Analog::digits() const +{ + return _structure->encoding->digits; +} + +bool Analog::is_digits_decimal() const +{ + return _structure->encoding->is_digits_decimal; +} + +shared_ptr Analog::scale() +{ + unique_ptr scale; + scale.reset(new Rational(&(_structure->encoding->scale))); + + if (scale) + return scale->share_owned_by(shared_from_this()); + else + throw Error(SR_ERR_NA); +} + +shared_ptr Analog::offset() +{ + unique_ptr offset; + offset.reset(new Rational(&(_structure->encoding->offset))); + + if (offset) + return offset->share_owned_by(shared_from_this()); + else + throw Error(SR_ERR_NA); +} + const Quantity *Analog::mq() const { return Quantity::get(_structure->meaning->mq); @@ -1242,6 +1287,36 @@ vector Analog::mq_flags() const return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); } +Rational::Rational(const struct sr_rational *structure) : + _structure(structure) +{ +} + +Rational::~Rational() +{ +} + +shared_ptr Rational::share_owned_by(shared_ptr _parent) +{ + return static_pointer_cast( + ParentOwned::share_owned_by(_parent)); +} + +int64_t Rational::numerator() const +{ + return _structure->p; +} + +uint64_t Rational::denominator() const +{ + return _structure->q; +} + +float Rational::value() const +{ + return (float)(_structure->p) / (float)(_structure->q); +} + InputFormat::InputFormat(const struct sr_input_module *structure) : _structure(structure) { @@ -1274,8 +1349,7 @@ map> InputFormat::options() { map> result; - if (const struct sr_option **options = sr_input_options_get(_structure)) - { + if (const struct sr_option **options = sr_input_options_get(_structure)) { shared_ptr option_array {options, &sr_input_options_free}; for (int i = 0; options[i]; i++) { @@ -1305,8 +1379,7 @@ Input::Input(shared_ptr context, const struct sr_input *structure) : shared_ptr Input::device() { - if (!_device) - { + if (!_device) { auto sdi = sr_input_dev_inst_get(_structure); if (!sdi) throw Error(SR_ERR_NA); @@ -1320,7 +1393,7 @@ void Input::send(void *data, size_t length) { auto gstr = g_string_new_len(static_cast(data), length); auto ret = sr_input_send(_structure, gstr); - g_string_free(gstr, false); + g_string_free(gstr, true); check(ret); } @@ -1396,6 +1469,28 @@ vector Option::values() const return result; } +Glib::VariantBase Option::parse_string(string value) +{ + enum sr_datatype dt; + Glib::VariantBase dflt = default_value(); + GVariant *tmpl = dflt.gobj(); + + if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_UINT64)) { + dt = SR_T_UINT64; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_STRING)) { + dt = SR_T_STRING; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_BOOLEAN)) { + dt = SR_T_BOOL; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_DOUBLE)) { + dt = SR_T_FLOAT; + } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_INT32)) { + dt = SR_T_INT32; + } else { + throw Error(SR_ERR_BUG); + } + return ConfigKey::parse_string(value, dt); +} + OutputFormat::OutputFormat(const struct sr_output_module *structure) : _structure(structure) { @@ -1428,8 +1523,7 @@ map> OutputFormat::options() { map> result; - if (const struct sr_option **options = sr_output_options_get(_structure)) - { + if (const struct sr_option **options = sr_output_options_get(_structure)) { shared_ptr option_array {options, &sr_output_options_free}; for (int i = 0; options[i]; i++) { @@ -1492,14 +1586,11 @@ string Output::receive(shared_ptr packet) { GString *out; check(sr_output_send(_structure, packet->_structure, &out)); - if (out) - { + if (out) { auto result = string(out->str, out->str + out->len); g_string_free(out, true); return result; - } - else - { + } else { return string(); } }