簡體   English   中英

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

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

我正在 ESP32 上開發 C++ 項目。 我想在其中使用 esp_console + argtable3(C 庫)。 我正在嘗試在我的成員函數中使用 argtable3。 為此,我使用全局指針為我的成員函數創建回調函數。 我確信我的類只會被實例化一次,所以我認為可以創建回調函數。

問題是 argtable 沒有將用戶輸入的參數返回給我。 它成功地檢查了它們(參數的數量及其類型),但它給我的數據是隨機的。 我已經在成員函數之外測試了我的代碼,它運行良好。 但我想在成員函數中使用它來訪問我對象的其他部分。 這是我的代碼:

// 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
    }
}

我使用回調成員函數的方法好嗎? 知道我的問題是什么以及我如何解決它嗎?

預先感謝您的回答。

在復制/粘貼在互聯網上找到的非常簡單的示例代碼后,我終於找到了問題所在:

我在"myclass.h"之后包含了<argtable3/argtable3.h>

一個愚蠢的錯誤花了我將近2天的時間......

但是,如果有人解釋了為什么包含順序允許我編譯程序但生成“損壞的”二進制文件,請隨時回答!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM