使用此 Arduino 库的非静态成员函数

发布于 2025-01-17 22:40:29 字数 2002 浏览 6 评论 0原文

在Arduino项目(C ++)中,我使用 liquidmenu 库我很难将此课程整合到我自己的。对于实际情况,提供的示例太简单了。

某些未编译的代码:

main.cpp

const int rs = 3, en = 0, d4 = 4, d5 = 30, d6 = 12, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Menu menu(&lcd);

void setup() { }

void loop() { }

class Menu
{
public: 
    Menu(LiquidCrystal *lcd);        

private:
    int _myvar;

    LiquidCrystal *_lcd;
    LiquidMenu _menu;
    LiquidScreen _screenSetCC1;

    LiquidLine line1_setupCC1;
    LiquidLine line2_setupCC1;

    void editIntPlus();
};

CPP

Menu::Menu(LiquidCrystal *lcd) : _lcd(lcd), _menu(*_lcd),
    line1_setupCC1(0, 0, "Set CC #1"),
    line2_setupCC1(0, 1, _myvar)
{
    
    line2_setupCC1.attach_function(1, editIntPlus);

    _screenSetCC1.add_line(line1_setupCC1);
    _screenSetCC1.add_line(line2_setupCC1);
    _menu.add_screen(_screenSetCC1);
}

void Menu::editIntPlus()
{
    _myvar++;
}

第一个错误是:

错误:非静态成员函数的使用无效'void菜单:: editintplus()'

但是我无法使该函数静态,否则我将无法再访问_MYVAR示例违反任何编程的基本规则。

第二个问题。在我的菜单中,我要设置数十个int var。 我真的不想创建很多功能来增加或减少每个var!

如何检索,edituintplus(),当前var被编辑?目标是将相同的功能附加到所有VAR。

我看到

  1. 无法声明EditIntplusfriend

    朋友: void EditIntplus();

导致:

 include/menu.h:82:5: error: expected primary-expression before 'void'
     void editIntPlus();
     ^~~~
  1. 如前所述,我不想使我的功能static否则我将无法再访问类成员

In an Arduino project (C++) I use the LiquidMenu library and I have difficulties to integrate this class in my own. The example provided are too simple for a real scenario.

Some code that does not compile:

main.cpp

const int rs = 3, en = 0, d4 = 4, d5 = 30, d6 = 12, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Menu menu(&lcd);

void setup() { }

void loop() { }

header

class Menu
{
public: 
    Menu(LiquidCrystal *lcd);        

private:
    int _myvar;

    LiquidCrystal *_lcd;
    LiquidMenu _menu;
    LiquidScreen _screenSetCC1;

    LiquidLine line1_setupCC1;
    LiquidLine line2_setupCC1;

    void editIntPlus();
};

cpp

Menu::Menu(LiquidCrystal *lcd) : _lcd(lcd), _menu(*_lcd),
    line1_setupCC1(0, 0, "Set CC #1"),
    line2_setupCC1(0, 1, _myvar)
{
    
    line2_setupCC1.attach_function(1, editIntPlus);

    _screenSetCC1.add_line(line1_setupCC1);
    _screenSetCC1.add_line(line2_setupCC1);
    _menu.add_screen(_screenSetCC1);
}

void Menu::editIntPlus()
{
    _myvar++;
}

first error is:

error: invalid use of non-static member function 'void Menu::editIntPlus()'

But I cannot make that function static, otherwise I cannot access to _myvar anymore.
The examples are not useful because they put everything outside any class and it's against any basic rule of programming.

Second question. In my menu I have dozens of int vars to set.
I really don't want to create a lot of functions to increase or decrease each single var!

How to retrieve, inside editIntPlus(), the current var being edited? The goal is to attach the same function to all the vars.

I saw this answer but:

  1. I cannot declare editIntPlus as friend:

    friend:
    void editIntPlus();

leads to:

 include/menu.h:82:5: error: expected primary-expression before 'void'
     void editIntPlus();
     ^~~~
  1. as said before, I don't want to make my function static otherwise I cannot access anymore to the class members

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘑菇王子 2025-01-24 22:40:29

根据 user17732522 的建议,我分叉了库并进行了这些更改:

LiquidMenu.h

void (*_function[MAX_FUNCTIONS])(void *); ///< Pointers to the functions
void (*_context[MAX_FUNCTIONS]); ///< Pointers to the contexts

LiquidLine.cpp

bool LiquidLine::attach_function(uint8_t number, void (*function)(void *), void* context) {

...
_context[number - 1] = context;

...
(*_function[number - 1])(_context[number - 1]);

和我的代码(在 user17732522 建议后更正):

void editIntPlus(void *context);

line2_setupCC1.attach_function(1, [](void* context){ static_cast<Menu*>(context)->editIntPlus(context); }, this);

Following the suggestion of user17732522 I forked the library making these changes:

LiquidMenu.h

void (*_function[MAX_FUNCTIONS])(void *); ///< Pointers to the functions
void (*_context[MAX_FUNCTIONS]); ///< Pointers to the contexts

LiquidLine.cpp

bool LiquidLine::attach_function(uint8_t number, void (*function)(void *), void* context) {

...
_context[number - 1] = context;

...
(*_function[number - 1])(_context[number - 1]);

and in my code (corrected after user17732522 suggestion):

void editIntPlus(void *context);

line2_setupCC1.attach_function(1, [](void* context){ static_cast<Menu*>(context)->editIntPlus(context); }, this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文