-
Notifications
You must be signed in to change notification settings - Fork 5
/
Timers.h
40 lines (34 loc) · 987 Bytes
/
Timers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* 16-bit timer 1 used for Sensorino timekeeping as well as arbitrary
* timeouts.
*
* Licensed under AGPLv3.
*/
#include <stdint.h>
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
class GenCallback {
public:
virtual void call(void) = 0;
};
class Timers {
Timers(void);
static class init { public: init(void) { begin(); } } initializer;
public:
static void begin(void);
static uint32_t now(void);
static uint32_t millis(void);
static void delay(uint16_t msecs);
static void setTimeout(void (*callback)(void), uint32_t timeout);
static void setTimeout(GenCallback *callback, uint32_t timeout);
};
#define setObjTimeout(method, timeout) \
setTimeout(new Callback<typeof(*this)>(this, &method), timeout)
template <typename T>
class Callback : public GenCallback {
T *obj;
void (T::*method)(void);
public:
Callback(T *nobj, void (T::*nmethod)(void)) : obj(nobj), method(nmethod) {}
void call(void) { (obj->*method)(); }
};