Skip to content
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

Context aware callback methods. #39

Open
wants to merge 29 commits into
base: development
Choose a base branch
from
Open

Context aware callback methods. #39

wants to merge 29 commits into from

Conversation

murrellrr
Copy link

To better facilitate C++/OO I've updated the Adafruit_BluefruitLE_nRF51 libraries for my personal use to include a context pointer (void*) that is passed in all callback methods. This is similar to the POSIX pthread_create method.

This allows for a more OO style handling of callbacks. Example:

Somewhere, in a setup method far, far, away.
ble.setCallbackContext(this);
ble.setConnectCallback(SomeClass::connect);
ble.setDisconnectCallback(SomeClass::disconnect);

SomeClass.h
// class definition
public:
static void connect(void* context) {
((SomeClass*)context)->deviceConnected();
}

static void disconnect(void* context) {
	((SomeClass*)context)->deviceDisconnected();
}

// class definition

SomeClass.cpp
void SomeClass::deviceConnected() {
// do something
}

   void SomeClass::deviceDisconnected() {
       // do something
   }

I've effectively modified all the call back function to support this behavior while still maintaining the original API for backward compatibility while the old API is deprecated and removed, should you choose.

I've also marked the deprecated methods as @deprecated in the doxygen comments. Furthermore, I split up the responsibilities of registering a GATT callback function pointer and the GATT events to get call backs for. To do this I've added the following:

ble.enableBleGattCallback(chars_idx);
ble.disableBleGattCallback(chars_idx);

I have only tested the connect and disconnect callbacks. I'm testing the UART soon. It does compile though.

Thanks for all the help and dedication for the API, it's much appreciated. The least I could do is give back a little.

hathach and others added 29 commits December 7, 2016 17:20
.. in order for it to work in the context of flow control with hardware serial
added pinMode declaration for the RTS pin..
This allows a pointer to an object to be passed in while registering for callbacks. In turn, when the callback is invoked, the pointer will be included. This allows an instance added information, including an instance handler, for the more C++ types. This pattern is similar to the pattern followed in POSIX for thread creation.
Need to double check to see if system_events can only happen one at a time.
Will change when deprecated methods are removed.
Updated the GATT calback mechanism to separate setting the callback function pointer and enabling attributes. This is a cleaner method in the callback context world.
@murrellrr
Copy link
Author

Context aware UART callback tested and successful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants