adafruit / Adafruit_BluefruitLE_nRF51

Arduino library for nRF51822-based Adafruit Bluefruit LE modules
197 stars 122 forks source link

Context aware callback methods. #39

Open murrellrr opened 6 years ago

murrellrr commented 6 years ago

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.

murrellrr commented 6 years ago

Context aware UART callback tested and successful.