-
Notifications
You must be signed in to change notification settings - Fork 103
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
Library JetsonGPIO - add_event_detect #25
Comments
If JetsonGPIO could use lambda objects or custom callable objects as callback, But unfortunately current version of JetsonGPIO can only use function pointers as callback, for example, something like this static MyGPIO* pGpio = nullptr;
static void Snap(int iPin)
{
if(pGpio == nullptr)
return;
//...
};
//---------------------------------------------------------------------------
bool MyGPIO::LoadGPIO()
{
pGpio = this;
int iPin = 18;
GPIO::setup(iPin, GPIO::IN);
GPIO::add_event_detect(iPin, GPIO::Edge::RISING, Snap, 50);
} In the future, I want to make JetsonGPIO supports std::function as callback type, I don't know how to implement If somebody has any idea for this issue, please let me know. |
Ok, I came up with an idea to support custom type callback! Now any object that satisfies the following requirements can be used as callback functions.
So you don't need to use ugly global variables for this! For example: // define callback object
class MyCallback
{
public:
explicit MyCallback(MyGPIO& myGPIO) : myGPIO(myGPIO) {}
MyCallback(const MyCallback&) = default; // Copy-constructible
void operator()(int channel) // Callable
{
// do something with myGPIO
}
bool operator==(const MyCallback& other) const // Equality-comparable
{
// equality compare example:
return myGPIO == other.myGPIO;
}
private:
MyGPIO& myGPIO;
};
//------
bool MyGPIO::LoadGPIO()
{
// create callback object
MyCallback my_callback(*this);
int iPin = 18;
GPIO::setup(iPin, GPIO::IN);
GPIO::add_event_detect(iPin, GPIO::Edge::RISING, my_callback, 50);
} Or you can also make MyGPIO class into callback itself if you want. |
I'm having some trouble implementing the custom type callback class you posted. Here's my code:
I'm not passing in the GPIO controller object for now to avoid a circular include issue I've been having and using placeholders instead. However, when I change
Is this the expected behaviour? Am I doing something silly? I've tried it on the latest release and master branches |
In the earlier version of the library(at the moment I posted this example), the channel type in the callback was So in your code, the class GpioCallback {
using CallbackType = int;
using Callback = std::function<void(const CallbackType&)>;
/*...*/
// the argument type of the operator() MUST be const std::string&
void operator()(const std::string& channel)
{
// cast the channel to int by std::stoi
callback_(std::stoi(channel));
}
/*...*/
}; The support for callbacks with |
Thanks for the quick response. Your suggestions worked and we can use callbacks with different signatures, but still using |
@yassiezar |
Hi all!
i'm using "add_event_detect" to detect a button click signal to snap photos. But i can't retrieve the context of my class.
A few lines of my code:
//---------------------------------------------------------------------------
void Snap(int iPin, void* ptr)
{
MyGPIO* pGpio = (MyGPIO*)ptr;
};
¿Any idea?
thanks!!
The text was updated successfully, but these errors were encountered: