-
Notifications
You must be signed in to change notification settings - Fork 1k
Wire add support for callback_function_t callbacks #1833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @hitech95 , |
I'm testing a solution, for now I have created a class that overrides the two methods above. The code I'm working with: class MTwoWire : public TwoWire
{
private:
std::function<void(int)> user_onReceive;
std::function<void(void)> user_onRequest;
public:
MTwoWire() : TwoWire(){};
MTwoWire(uint32_t sda, uint32_t scl) : TwoWire(sda, scl){};
void onReceive(void (*)(int));
void onRequest(void (*)(void));
#ifdef __cplusplus
#include <functional>
typedef std::function<void(int)> cb_function_receive_t;
typedef std::function<void(void)> cb_function_request_t;
void onReceive(cb_function_receive_t callback);
void onRequest(cb_function_request_t callback);
#endif
};
void MTwoWire::onReceive(cb_function_receive_t function)
{
user_onReceive = function;
}
void MTwoWire::onReceive(void (*function)(int))
{
cb_function_receive_t _c = function;
onReceive(_c);
}
// sets function called on slave read
void MTwoWire::onRequest(cb_function_request_t function)
{
user_onRequest = function;
}
void MTwoWire::onRequest(void (*function)(void))
{
cb_function_request_t _c = function;
onRequest(_c);
} My (future) test case: _i2c_bus->onReceive(std::bind(&I2Client::onHandleReceive, this, std::placeholders::_1));
_i2c_bus->onRequest(std::bind(&I2Client::onHandleRequest, this)); |
@fpistm POC works, probably more tests are needed but so far so good. Do you prefere a PR from/to main/master or you prefere a PR for a stable release? The patch is really simple, working out a draft PR right now. |
PR will be merge in main. master is deprecated. |
Is your feature request/improvement related to a problem? Please describe.
The Wire methods onReceive and onRequest dont allow to pass a function via std::bind.
This would result to have to use a singleton to have access the hanlders if the I2C client code is an object like a library.
A solution similar to the attachInterrupt would solve the issue.
Describe the solution you'd like
Something like this (similar to what we do with
attachInterrupt()
)My current workaround is to have multiple definitions of the static handling methods
each pair call the right object handling methods.
This is not the best solution with
callback_function_t
I could just pass the Wire object to class instanceand it will self manage.Another solution would be to pass the object pointer to the registration method.
This approach is used by PacketSerial:
https://github.com/bakercp/PacketSerial/blob/master/src/PacketSerial.h#L372
The text was updated successfully, but these errors were encountered: