sensor_commands.cpp 835 Bytes
Newer Older
1
2
3
#include "sensor_commands.h"

namespace sensor_commands {
4
5
  const uint8_t MAX_CALLBACKS = 20;
  uint8_t callbacks_count = 0;
6
7
8
9
10
11
12
13
14
15
// 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) {
    }
    void (*function)(void*);
    void *data;
  };

16
  Callback callbacks[MAX_CALLBACKS];
17

18
19
20
21
22
23
  void defineCallback(void (*f)(void*), void *d) {
    if (callbacks_count < MAX_CALLBACKS) {
      callbacks[callbacks_count++] = Callback(f, d);
    } else {
      Serial.println("OH NOOEEEESSS!!!! TOO MANY CALLBACKS");
    }
24
25
26
27
  }

  void run() {
    // Test all the callbacks.
28
    for (uint8_t i = 0; i < callbacks_count; i++) {
29
30
31
32
      callbacks[i].function(callbacks[i].data);
    }
  }
}