Created
February 12, 2019 03:51
-
-
Save devendranaga/583ae347fe7b1fac89462e3a81290235 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void (*event_callback)(void *user_priv, uint8_t *datap, size_t datap_len); | |
static void *user_priv; | |
int register_event_callback(void *user_ptr) | |
{ | |
event_callback = my_event_callback; | |
user_priv = user_ptr; | |
return 0; | |
} | |
// running for ever loop | |
void run() | |
{ | |
for ( ; ; ) { | |
if (an_event) { | |
uint8_t datap[100]; | |
datap_len = event_data_get(datap); | |
// call the callback along with user's private data pointer | |
if (event_callback) | |
event_callback(user_priv, datap, datap_len); | |
} | |
} | |
} | |
// .. caller code.. | |
// caller's private data | |
struct my_struct { | |
int some_data; | |
}; | |
void my_event_callback(void *user_priv, uint8_t *datap, size_t datap_len) | |
{ | |
struct my_struct *my = user_priv; | |
} | |
struct my_struct *my = calloc(1, sizeof(struct my_struct)); | |
if (!my) { | |
return -1; | |
} | |
// C takes care of type conversion automatically unlike the C++ | |
register_event_callback(my); | |
// let the main loop run and this calls our event callback | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment