I am new to C++ & I need to call functions of one class from another class. And so that the classes do not know anything about each other. How to do it in Arduino environment?
class Encoder {
using CallBack2 = void( * )(const int, const int);
using CallBack0 = void( * )();
private:
CallBack2 callbackUpDown;
CallBack0 callbackTab;
CallBack0 callbackLongPress;
public:
Encoder() {}
void Setup(CallBack2 updown, CallBack0 tab, CallBack0 cbLongPress) {
callbackUpDown = updown;
callbackTab = tab;
callbackLongPress = cbLongPress;
}
void Loop() {
if (true) {
callbackUpDown(-1, 300);
callbackTab();
callbackLongPress();
}
}
};
class Menu {
public:
void UpDown(const int direction,
const int count) {}
void Tab() {}
};
class RTC {
public:
void Setup() {}
void Toggle() {}
};
class Display {
public:
void Toggle() {}
};
Encoder encoder = Encoder();
RTC rtc = RTC();
Menu menu = Menu();
Display display = Display();
void setup() {
rtc.Setup();
encoder.Setup(menu.UpDown, menu.Tab, []{display.Toggle();rtc.Toggle();});
}
void loop() {
encoder.Loop();
}
Output:
In file included from sun.ino:47:0:
callback.h: In function 'void setup()':
callback.h:52:75: error: invalid use of non-static member function 'void Menu::UpDown(int, int)'
encoder.Setup(menu.UpDown, menu.Tab, []{display.Toggle(); rtc.Toggle();});
^
callback.h:28:7: note: declared here
void UpDown(const int direction, const int count) {}
^~~~~~
Thank you!