0

I'll try to explain my need:

  1. I have a generic library I created, called myIOT.h.
  2. This class have a pre-configured MQTT and wifi setup.
  3. Inside this library there is a mqtt_callback function, for all generic commands, such as boot_time and connection_status.
  4. BUT, this library serves in several of my iot device's code, that each one has a few more mqtt commands, that is tailored just for s specific device, and I looking for a way that iot.mqtt_callback(ext_mqtt_func) will get as parameter a local ( which belong to the sketch and not to the library ), that will have the additional code needed.

I'll try to explain more clearly:

void myIOT::mqtt_callback(ext_mqtt_func){
if (a == true ) {
do something
};
else {
ext_mqtt_func()); // calling another fucntion that belongs to sketch and not library
}
Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Jan 11, 2019 at 19:26
2
  • Note that it is better to use if (a) than comparing a boolean with true or false. If you name a better so it is clear it is a boolean it is better maintainable. Commented Jan 11, 2019 at 22:51
  • @MichelKeijzers it is a string compare function to set a MQTT comman Commented Jan 12, 2019 at 6:00

1 Answer 1

1

Yes, you can do it.

You pass function pointer as function parameter.

See this stackoverflow question:

https://stackoverflow.com/questions/2582161/c-function-pointer-as-parameter

answered Jan 11, 2019 at 19:35
1
  • thank you, it works great. Since it have to a string using a MQTT, this ext_fuct needs as well to pass this string - and the explanation above is for a no-arg function, so I did upgrade it to be typedef void (*cb_func)(char msg1[50]) that passes its string ( and it works great ), but I would rather pass it as a pointer to that string ( which fails ) Commented Jan 12, 2019 at 6:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.