-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAikoEvents.h
64 lines (49 loc) · 1.54 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
62
63
64
#ifndef AikoEvents_h
#define AikoEvents_h
// Aiko is (c) Andy Gelme and Pete Yandell https://github.com/geekscape/aiko_arduino_legacy
#include "Arduino.h"
#include "AikoCallback.h"
using namespace std;
namespace Aiko {
struct EventHandler {
Callback callback_;
unsigned int period_;
long countdown_;
bool in_ISR;
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, bool in_ISR = 0, unsigned int delay = 0);
void addOneShotHandler(void (*handler)(), unsigned int delay);
void loop(bool run_ISR=false, unsigned long time = 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