Hello everyone, my question is very simple but as i am not a programming expert i couldn't find the correct solution. I have this code and declaring a function and using its reference as an argument, and every class object has different functions.
class MyClass{
public:
uint64_t irCode;
char btCode;
void (*fun)(void);
void init(uint64_t ir,char bt, void(*f)(void)){
irCode = ir;
btCode = bt;
fun = f;
}
}myclass[2];
void f1();
void f2();
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
myclass[0].init(0x000016,'a',&f1);
myclass[1].init(0x000018,'b',&f2);
}
void loop() {
myclass[0].fun();
myclass[1].fun();
}
void f1(){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(250);
}
void f2(){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
is it possible to declare function inside the init function argument like
myclass[0].init(0x000016,'a',void f1{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(250);
}
);
i already checked online and c++ 11 supports it but couldn't find the arduino example.
thank you all guys, lambda declaration saves me. for future visitors i am leaving the final code, though the functions do the same thing, it explains the idea.
class MyClass{
public:
uint64_t irCode;
char btCode;
void (*fun)(int);
void init(uint64_t ir,char bt, void(*f)(int)){
irCode = ir;
btCode = bt;
fun = f;
}
}myclass[2];
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
myclass[0].init(0x000016,'a',[](int a){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(a);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(a);
}
);
myclass[1].init(0x000032,'b',[](int b){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(b);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(b);
}
);
}
void loop() {
myclass[0].fun(250);
myclass[1].fun(750);
}
Defining a type for your function pointer would tighten up the code a little:
class MyClass{
using FunctionPtr = void (*)(int);
public:
uint64_t irCode;
char btCode;
FunctionPtr fun;
void init(uint64_t ir,char bt, FunctionPtr f){
irCode = ir;
btCode = bt;
fun = f;
}
}myclass[2];
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
myclass[0].init(0x000016,'a',[](int a){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(a);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(a);
}
);
myclass[1].init(0x000032,'b',[](int b){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(b);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(b);
}
);
}
void loop() {
myclass[0].fun(250);
myclass[1].fun(750);
}