简体   繁体   English

将函数参数保存在结构数组中

[英]Saving functions arguments in array of structures

I'm new to this forum. 我是这个论坛的新手。 Very litlle programing experices. 非常编程的经验。 And english is not my primary language :) 而且英语不是我的主要语言:)

I'm writing C code for microcontroller and for now I have created Graphic menu with array of structures. 我正在为微控制器编写C代码,现在我已经创建了具有结构数组的图形菜单。 In this array I have defined jumps in case of pressed buttons (UP, DOWN, ENTER), menu texts and called functions in case of pressed buttons (if you are on correct submenu you can call functions). 在此数组中,我定义了在按下按钮(上,下,ENTER)的情况下的跳转,菜单文本以及在按下按钮的情况下的被调用函数(如果您位于正确的子菜单上,则可以调用函数)。

Structure definition: 结构定义:

typedef const struct Menu_structure
{
    const char *text;
    unsigned char Menu_points;
    unsigned char Menu_button_UP;
    unsigned char Menu_button_DOWN;
    unsigned char Menu_button_ENTER;
    Menu_Waveform_parameter_t Waveform_parameter_value;
    void (*Menu_function_UP) ();
    void (*Menu_function_DOWN) ();
    void (*Menu_function_ENTER) ();
} MenuEntry;

1:menu tekst, 2:size of current submenu(for refresh purposes), 3:jump in case of UP, 4: jump in case of down, 5: jump in case of enter, 6: parameter enum, 7: function that are called in case of presses buttons. 1:菜单菜单,2:当前子菜单的大小(用于刷新),3:UP时跳转,4:向下时跳转,5:输入时跳转,6:参数枚举,7:该函数在按下按钮的情况下被调用。

Example of array: 数组示例:

MenuEntry menu[] =
{
    {menu_01,   5,  0,  0,  0,  0,              0,  0,  0},         
    {menu_02,   5,  1,  2,  1,  0,              0,  0,  funtion_0}, 
    {menu_03,   5,  1,  3,  6,  0,              0,  0,  0},             
    {menu_04,   5,  2,  4,  11, 0,              0,  0,  0},             
    {menu_05,   5,  3,  4,  1,  0,              0,  0,  funtion_1},
 }

Code for calling function (enter button pressed) 调用功能代码(按下输入按钮)

        case Button_ENTER:      if (menu[Menu_selected].Menu_function_ENTER != 0)
                                {
                                    menu[Menu_selected].Menu_function_ENTER();
                                }
                                break;

Everything works perfectly. 一切正常。 The problem is that my menu is very long. 问题是我的菜单很长。 Which means that I would need a lot of different functions (some of them can be combined with use of arguments). 这意味着我将需要很多不同的功能(其中一些功能可以与参数的使用结合使用)。

Here comes my question. 我的问题来了。 How can I save constant arguments in a array of structures. 如何将常数参数保存在结构数组中。 Any suggestions. 有什么建议么。 Thank you. 谢谢。

In order to accomplish what you are asking for, I would first recommend moving on from C to some higher level language, at least for your User Interface. 为了满足您的要求,我首先建议至少从用户界面的角度出发,从C语言过渡到更高级的语言。 (The rest of your low-level stuff can remain in C.) Nowadays you can find high level languages even in embedded systems, complete with features for interfacing with C. (其余的低级内容可以保留在C中。)如今,即使在嵌入式系统中,您也可以找到高级语言,并具有与C接口的功能。

But let me guess, this is not an option for you. 但是让我猜测,这不是您的选择。

So, the second thing I would recommend in order to accomplish what you are asking for is to use that wonderfully terrible hack of C known as preprocessor macros. 因此,为了完成您要完成的任务,我建议的第二件事是使用被称为预处理器宏的C语言这一可怕的技巧

In C you can define a macro like this: 在C中,您可以这样定义一个宏:

#define MYMACRO(a,b,c,d,e,i) { a, b, c, d, e, 0, 0, 0, i }

So then your table looks as follows: 因此,您的表如下所示:

{
    MYMACRO( menu_01,   5,  0,  0,  0,  0},         
    MYMACRO( menu_02,   5,  1,  2,  1,  funtion_0}, 
    MYMACRO( menu_03,   5,  1,  3,  6,  0},             
    MYMACRO( menu_04,   5,  2,  4,  11, 0},             
    MYMACRO( menu_05,   5,  3,  4,  1,  funtion_1},
}

Of course, you can define various different versions of MYMACRO with different arguments, expanding to use different constants, etc. Knock yourself out! 当然,您可以使用不同的参数定义各种不同的MYMACRO版本,扩展为使用不同的常量,等等。

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

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