Commit 06f4a1d9 authored by Eric Duminil's avatar Eric Duminil
Browse files

Adding name to callbacks

parent 130d5dd4
......@@ -7,20 +7,19 @@ namespace sensor_commands {
// A callback contains both a function and a pointer to arbitrary data
// that will be passed as argument to the function.
struct Callback {
Callback(void (*f)(void*) = 0, void *d = 0) :
function(f), data(d) {
Callback(const char *s = 0, void (*f)(void*) = 0, void *d = 0) :
name(s), function(f), data(d) {
}
const char *name;
void (*function)(void*);
void *data;
};
Callback callbacks[MAX_CALLBACKS];
char callback_names[MAX_CALLBACKS][MAX_COMMAND_SIZE];
void defineCallback(const char *command, void (*f)(void*), void *d) {
if (callbacks_count < MAX_CALLBACKS) {
callbacks[callbacks_count] = Callback(f, d);
strlcpy(callback_names[callbacks_count], command, MAX_COMMAND_SIZE);
callbacks[callbacks_count] = Callback(command, f, d);
callbacks_count++;
} else {
Serial.println(F("Too many callbacks have been defined."));
......@@ -34,7 +33,7 @@ namespace sensor_commands {
// Test all the callbacks.
for (uint8_t i = 0; i < callbacks_count; i++) {
Serial.print("Trying '");
Serial.print(callback_names[i]);
Serial.print(callbacks[i].name);
Serial.println("'");
callbacks[i].function(callbacks[i].data);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment