How to use Callbacks in classes

Reinosoft
Posts: 2
Joined: Mon Aug 12, 2019 8:18 pm

How to use Callbacks in classes

Postby Reinosoft » Mon Aug 12, 2019 8:36 pm

Yesterday i created with a lot of struggle an example on how to use callbacks in my librarys.
(I think i still don't understand what's really going on...)

I tested the code with an Arduino and a STM and everything works fine.
No i'm trying to convert it to the ESP32 but i can't get it to work.

I'm trying for several hours now, watched a few library examples but I don't understand how the callbacks work exactly in the ESP.

Can anyone help me with a simple example or explanation?

Code: Select all

 extern "C" {
typedef int (*callbackFunctionArgument) (const int arg1, const int arg2);

typedef void (*callbackFunction)(void);
} 

class testcallback {
public:
//{ 
void voidCallback(callbackFunction newFunction()) {
Serial.println("VOIDCALLBACK::INIT!");
_voidCallback = newFunction;
}
void RunvoidCallBack() {
_voidCallback();
}
//}

//{
int FunctionArgument1(callbackFunctionArgument newFunction(int arg1, int arg2)) {
Serial.println("FUNCTIONARGUMENT1::INIT!");
_FunctionArgument1 = newFunction;
}
void RunCallbackArg1(int value1, int value2) {
Serial.println("Return value from local function 1:" + String(_FunctionArgument1(value1,value2)));
}
//}
//{
int FunctionArgument2(callbackFunctionArgument newFunction(int arg1, int arg2)) {
Serial.println("FUNCTIONARGUMENT2::INIT!");
_FunctionArgument2 = newFunction;
}
void RunCallbackArg2(int value1, int value2) {
Serial.println("Return value from local function 2:" + String(_FunctionArgument2(value1,value2)));
}
//}


private:
callbackFunction _voidCallback;

callbackFunctionArgument _FunctionArgument1;
callbackFunctionArgument _FunctionArgument2;

};

testcallback d;
void setup() {
Serial.begin(9600);

d.voidCallback(CallbackVOID);
d.FunctionArgument1(CallbackINT1); //pass the callback to local functions
d.FunctionArgument2(CallbackINT2); //pass the callback to local function

d.RunvoidCallBack();
delay(2000);
d.RunCallbackArg1(8,16);
delay(2000);
d.RunCallbackArg2(1,2);

}
void CallbackVOID() {
Serial.println("\nCallbackVOID entered!");
}
int CallbackINT1(int a, int b) {
Serial.println("\nCallbackINT1: a=" + String(a) + "/b=" + String(b));
return 32000;
}
int CallbackINT2(int a, int b ) {
Serial.println("\nCallbackINT2: a=" + String(a) + "/b=" + String(b));
return 16;
}

void loop() {
// put your main code here, to run repeatedly:

}

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to use Callbacks in classes

Postby chegewara » Wed Aug 14, 2019 9:08 pm

When you create function where you pass typedef-ined callback function then you do it without arguments, because typedef already know how many and what kind arguments has this function.

Code: Select all

typedef int (*callbackFunctionArgument) (const int arg1, const int arg2);

typedef void (*callbackFunction)(void);


class testcallback {
public:
    //{ 
    void voidCallback(callbackFunction newFunction) {
      Serial.println("VOIDCALLBACK::INIT!");
      _voidCallback = newFunction;
    }
    void RunvoidCallBack() {
      _voidCallback();
    }
    //}

    //{
    int FunctionArgument1(callbackFunctionArgument newFunction) {
    Serial.println("FUNCTIONARGUMENT1::INIT!");
    _FunctionArgument1 = newFunction;
    }
    void RunCallbackArg1(int value1, int value2) {
    Serial.println("Return value from local function 1:" + String(_FunctionArgument1(value1,value2)));
    }
    //}
    //{
    int FunctionArgument2(callbackFunctionArgument newFunction) {
      Serial.println("FUNCTIONARGUMENT2::INIT!");
      _FunctionArgument2 = newFunction;
    }
    void RunCallbackArg2(int value1, int value2) {
      Serial.println("Return value from local function 2:" + String(_FunctionArgument2(value1,value2)));
    }
    //}


private:
    callbackFunction _voidCallback;

    callbackFunctionArgument _FunctionArgument1;
    callbackFunctionArgument _FunctionArgument2;

};

testcallback d;
void setup() {
    Serial.begin(115200);

    d.voidCallback(CallbackVOID);
    d.FunctionArgument1(CallbackINT1); //pass the callback to local functions
    d.FunctionArgument2(CallbackINT2); //pass the callback to local function

    d.RunvoidCallBack();
    delay(2000);
    d.RunCallbackArg1(8,16);
    delay(2000);
    d.RunCallbackArg2(1,2);

}
void CallbackVOID() {
    Serial.println("\nCallbackVOID entered!");
}
int CallbackINT1(int a, int b) {
    Serial.println("\nCallbackINT1: a=" + String(a) + "/b=" + String(b));
    return 32000;
}
int CallbackINT2(int a, int b ) {
    Serial.println("\nCallbackINT2: a=" + String(a) + "/b=" + String(b));
    return 16;
}

void loop() {
// put your main code here, to run repeatedly:

}

Reinosoft
Posts: 2
Joined: Mon Aug 12, 2019 8:18 pm

Re: How to use Callbacks in classes

Postby Reinosoft » Thu Aug 15, 2019 9:34 am

Hi,

Thank you very much for your help!

Regards,

Reinald

Who is online

Users browsing this forum: Majestic-12 [Bot] and 70 guests