I want to pass a function as "preProcessor Function" to another function - In my other projects I've done this with the std::bind() function but this is not working in my case (using Arduino ModbusMaster.h)
These functions need a pre/postProcessor function passed:
void ModbusMaster::preTransmission(void (*)());
void ModbusMaster::postTransmission(void (*)());
I've created a class for my project and create a new Modbus Object in it:
ModbusMaster _node;
Now I want to use the "normal" post and preprocessor functions used by Arduino ModbusMaster Lib but if I want to pass my functions:
void OR_WE::preTransmission(void)
{
digitalWrite(this->derePin, 1);
}
void OR_WE::postTransmission(void)
{
digitalWrite(this->derePin, 0);
}
to the ModbusMaster Object, the following error appears if I want to use std::bind()
void OR_WE::begin(Stream &serial, uint8_t slave)
{
_node.begin(slave, serial);
if(this->manualDere)
{
_node.postTransmission(postTransmission); //normal passing
_node.preTransmission(std::bind(&OR_WE::preTransmission, this)); //binded
}
}
Error:
Error (postTransmission): .pio\libdeps\nodemcu\OR_WE_Energy_Meter\src\OR_WE.cpp:36:28: error: invalid use of non-static member function 'void OR_WE::postTransmission()'
Error (preTransmission): .pio\libdeps\nodemcu\OR_WE_Energy_Meter\src\OR_WE.cpp:37:36: error: cannot convert 'std::_Bind_helper<false, void (OR_WE::*)(), OR_WE*>::type' to 'void (*)()'
When comparing with my other Projects I've seen that there is a difference btw. the arguments definition:
The ModbusMaster Libary defined as Argument:
void preTransmission(void (*)());
void postTransmission(void (*)());
IN CPP File of ModbusMaster Lib:
void ModbusMaster::preTransmission**(void (*preTransmission)())**
void ModbusMaster::postTransmission(void (*postTransmission)())
In my Projects it looks like:
typedef std::function<void()> webService;
void registerNewService(const char* url, webService handler, bool restartServerAfterAdd = false);
And there I can pass the function with
std::bind(&className::functionname, this));
I can see a difference but I don't understand it and google is barely a help for me in this case... could anyone explain the difference for me and give a way to pass the function ![]()
My Code is hosted on GitHub if anyone can help me and need more information (I hope that anything relevant is included in my question ^^)
Link: https://github.com/j54j6/OR_WE_Energy_Meter-w-NonAutomaticFC/tree/master/src
--
j54j6