[英]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.