-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAikoEvents.h
61 lines (47 loc) · 1.41 KB
/
AikoEvents.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef AikoEvents_h
#define AikoEvents_h
#include "AikoTiming.h"
#include "AikoCallback.h"
using namespace std;
namespace Aiko {
struct EventHandler {
Callback callback_;
unsigned int period_;
long countdown_;
struct EventHandler* next_;
void fire() { callback_(); }
};
class EventHandlerList {
public:
EventHandlerList() { flush(); }
void add(EventHandler* handler);
void flush();
EventHandler* next();
void remove(EventHandler* handler);
void resetIterator();
private:
EventHandler* firstHandler_;
EventHandler* nextHandler_;
EventHandler* handlerBefore(EventHandler* handler);
EventHandler* last();
};
class EventManager {
public:
EventManager() { reset(); }
void addHandler(EventHandler* handler);
void addHandler(void (*handler)(), unsigned int interval, unsigned int delay = 0);
void addOneShotHandler(void (*handler)(), unsigned int delay);
void loop(unsigned long time = Timing.millis());
void removeHandler(EventHandler* handler);
void reset();
private:
void start(unsigned long time);
void loopRepeatingHandler(EventHandler* handler);
void loopOneShotHandler(EventHandler* handler);
bool isRunning_;
unsigned long lastLoopTime_;
EventHandlerList handlerList_;
};
extern EventManager Events;
};
#endif