简体   繁体   English

ESP32 > 在 C++ 项目中使用 esp_console + argtable3

[英]ESP32 > using esp_console + argtable3 in a C++ project

I'am developping a C++ project on an ESP32.我正在 ESP32 上开发 C++ 项目。 I'd like to use esp_console + argtable3 (C libraries) in it.我想在其中使用 esp_console + argtable3(C 库)。 I'm trying to use argtable3 in my members functions.我正在尝试在我的成员函数中使用 argtable3。 To do so, I'm creating callback functions to my members functions with a global pointer.为此,我使用全局指针为我的成员函数创建回调函数。 I'm sure my class is going to be instanced only once so I assume it's ok to create callback functions.我确信我的类只会被实例化一次,所以我认为可以创建回调函数。

The problem is that argtable isn't giving me back the parameters entered by the user.问题是 argtable 没有将用户输入的参数返回给我。 It checks for them successfully (number of args and their type) but the data it gives me back is random.它成功地检查了它们(参数的数量及其类型),但它给我的数据是随机的。 I've tested my code outside of members functions and it works well.我已经在成员函数之外测试了我的代码,它运行良好。 But I want to use it inside members functions to access other parts of my object.但我想在成员函数中使用它来访问我对象的其他部分。 Here is my code :这是我的代码:

// Pointer for my callback functions
MyClass * _callback;

struct arg_int *argInt;
struct arg_end *endPage;

// My callback function (GLOBAL)
int _setInt(int argc, char *argv[])
{
    return _callback->setInt(argc, argv);
}

// Tab of struct for argtable lib (GLOBAL)
void *setInt_argtable[] =
{
    argInt = arg_int1(NULL, NULL, "<0-12>", "Integer argument"),
    endInt = arg_end(10)
};

// Function I'm calling back
int MyClass::setInt(int argc, char *argv[])
{
    int nerrors = arg_parse(argc,argv,setInt_argtable);
    if (nerrors > 0)
    {
        arg_print_errors(stdout, endPage, "myprog");
        return 0;
    }
    printf("argc = %d\n", argc);                // argc gives the correct number of args
    printf("argv[0] = %s\n", argv[0]);          // argv[0] gives the correct command name
    printf("argv[1] = %s\n", argv[1]);          // argv[1] gives the correct value
    printf("argInt->ival[0] = %d\n", argInt->ival[0]);  // argInt->ival[0] gives random value
    return 0;
}

void MyClass::main(void)
{
    // Callback pointer initialisation
    _callback = this;

    /* Initializing the console */
    esp_console_config_t console_config
    {
        256,
        8,
        atoi(LOG_COLOR_CYAN),
        0
    };
    ESP_ERROR_CHECK( esp_console_init(&console_config) );

    /* Configure linenoise line completion library */
    /* Enable multiline editing. If not set, long commands will scroll within
    * single line.
    */
    linenoiseSetMultiLine(1);

    /* Tell linenoise where to get command completions and hints */
    linenoiseSetCompletionCallback(&esp_console_get_completion);
    linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);

    /* Set command history size */
    linenoiseHistorySetMaxLen(100);


    esp_console_register_help_command();

    //
    // Feeding my console with argtable parameters
    //

    esp_console_cmd_t consoleCmd;
    consoleCmd.command  = "setInt";
    consoleCmd.func     = &_setInt;
    consoleCmd.help     = "Trying to set a integer argument";
    consoleCmd.argtable = setInt_argtable;
    esp_console_cmd_register(&consoleCmd);

    /* Main loop */
    while(true)
    {
        // Getting command from user
    }
}

Is my approach of using callback member function good ?我使用回调成员函数的方法好吗? Any idea of what is my problem and how I could solve it ?知道我的问题是什么以及我如何解决它吗?

Thanks in advance for your answers.预先感谢您的回答。

After being copying/pasting very simple sample codes found on internet, I finally found what was the problem :在复制/粘贴在互联网上找到的非常简单的示例代码后,我终于找到了问题所在:

I was including <argtable3/argtable3.h> after "myclass.h"我在"myclass.h"之后包含了<argtable3/argtable3.h>

It took me almost 2 days for a dumb error...一个愚蠢的错误花了我将近2天的时间......

But if somebody has an explanation about why the inclusion order was allowing me to compile the program but making a "corrupted" binary, feel free to answer !但是,如果有人解释了为什么包含顺序允许我编译程序但生成“损坏的”二进制文件,请随时回答!

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

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