简体   繁体   English

为 C++ function 编写 GNU Octave 包装器

[英]Writing a GNU Octave wrapper for a C++ function

I'm trying to edit an GNU Octave wrapper for a C++ function to use it with raspberry pi.我正在尝试为 C++ function 编辑 GNU Octave 包装器以将其与树莓派一起使用。 I have zero experience with C++ or Octave prior to this (have experience with python and matlab) so I am struggling a little on understanding how this works.在此之前,我对 C++ 或 Octave 的经验为零(对 python 和 matlab 有经验)所以我在理解它是如何工作的方面有点挣扎。

Someone has already written a partial GNU Octave wrapper for a C++ function library as you see here如您在此处看到的,有人已经为 C++ function 库编写了部分 GNU Octave 包装器

What I am trying to do is to add additional functions to this wrapper.我想要做的是向这个包装器添加额外的功能。 For example, I would like to edit the block shown here:例如,我想编辑此处显示的块:

DEFUN_DLD (bcm2835_gpio_fsel, args, nargout,
  "-*- texinfo -*-\n\
@deftypefn  {} bcm2835_gpio_fsel (@var{pin}, @var{mode})\n\
TDOD: document me!\n\
@end deftypefn")
{
  octave_value_list retval;
  int nargin = args.length ();

  if (nargin != 2)
    print_usage ();

  if (! init_ret)
    error ("bcm2835 not initialized");

  int pin = args(0).int_value();
  int mode = args(1).int_value();

  bcm2835_gpio_fsel (pin, mode);
  return retval;
}

The purpose is to allow the function bcm2835_gpio_fsel to be able to take a string input.目的是让 function bcm2835_gpio_fsel 能够接受字符串输入。 (you can see here for the C++ library) I think the parts I need to change are probably int mode = args.int_value(); (您可以在此处查看 C++ 库)我认为我需要更改的部分可能是int mode = args.int_value(); to something like string mode = args(1);类似于string mode = args(1); . .

There are several things that I am curious about:有几件事我很好奇:

  1. Is my above thinking correct?我的上述想法正确吗? (re: taking in string input vs int input) (re:接受字符串输入与 int 输入)
  2. What is the octave_value_list retval; octave_value_list retval;是什么? that is written on the top of this code block?写在这个代码块的顶部? I read that "The return type of functions defined with DEFUN_DLD is always octave_value_list."我读到“用 DEFUN_DLD 定义的函数的返回类型始终是 octave_value_list。” but I'm not quite sure I understand what this means.但我不太确定我明白这意味着什么。 So it seems like you are initializing a variable that's called retval with type octave_value_list.因此,您似乎正在初始化一个名为 retval 且类型为 octave_value_list 的变量。 Is this some form of an array?这是某种形式的数组吗? Not sure...没有把握...

Thanks a ton!万分感谢!

I will answer question 2 first:我先回答问题2:

  1. What is the octave_value_list retval; octave_value_list retval 是什么? that is written on the top of this code block?写在这个代码块的顶部? I read that "The return type of functions defined with DEFUN_DLD is always octave_value_list."我读到“用 DEFUN_DLD 定义的函数的返回类型始终是 octave_value_list。” but I'm not quite sure I understand what this means.但我不太确定我明白这意味着什么。 So it seems like you are initializing a variable that's called retval with type octave_value_list.因此,您似乎正在初始化一个名为 retval 且类型为 octave_value_list 的变量。 Is this some form of an array?这是某种形式的数组吗? Not sure...没有把握...

The octave_value_list is a list of octave_value . octave_value_listoctave_value的列表。 An octave_value is a type that wraps anything that you will handle in the Octave interpreter (command-line interface). octave_value是一种包装您将在 Octave 解释器(命令行界面)中处理的任何内容的类型。 So, when in Octave you call:因此,当您在 Octave 中调用时:

[a, b] = foobar (x, y, z);

The function foobar will receive an octave_value_list with three elements ( octave_value ), and return an octave_value_list with two arguments. function foobar将接收一个包含三个元素 ( octave_value_list ) 的octave_value ,并返回一个包含两个octave_value_list的 octave_value_list。

When you call:你打电话时:

a = foobar (x);

Then the function will still receive and return octave_value_list s, each with one element.然后 function 仍将接收并返回octave_value_list ,每个都有一个元素。

If you're not looking at the Octave C++ headers, then the best alternative is to look at Octave's doxygen documentation .如果您没有查看 Octave C++ 标头,那么最好的选择是查看Octave 的 doxygen 文档 There, you can get an almost complete list of methods for octave_value .在那里,您可以获得octave_value的几乎完整的方法列表

Now, it's easier to answer your first question:现在,更容易回答您的第一个问题:

  1. Is my above thinking correct?我的上述想法正确吗? (re: taking in string input vs int input) (re:接受字符串输入与 int 输入)

Kind of.有点儿。 You're right in that you can get a string from an argument but you're doing it wrong.你是对的,你可以从一个参数中得到一个字符串,但你做错了。 string mode = args(1); will fail because args(1) returns an octave_value .将失败,因为args(1)返回一个octave_value Instead, you need to do std::string mode = args(1).string_value();相反,您需要执行std::string mode = args(1).string_value();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM