-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimerEvents.h
54 lines (40 loc) · 1.36 KB
/
TimerEvents.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
#ifndef TIMER_EVENTS_H
#define TIMER_EVENTS_H
#include <Arduino.h>
#define MAX_NUMBER_OF_EVENTS 32
typedef void (*TimerEventHandler)();
class TimerEvent {
public:
TimerEvent(const char* n, unsigned long p, TimerEventHandler onExecute, bool e, long d=-1, TimerEventHandler onFinish=nullptr);
TimerEvent();
const char* name;
unsigned long period;
TimerEventHandler onExecutePtr = nullptr;
TimerEventHandler onFinishPtr = nullptr;
bool isEnabled;
long duration;
unsigned long firstCalled = 0;
int index = -1;
void execute() { if (onExecutePtr != nullptr) onExecutePtr(); }
void finish() { if (onFinishPtr != nullptr) onFinishPtr(); }
void setEventCallback(TimerEventHandler &cbPtr) { onExecutePtr = cbPtr; }
void setFinishCallback(TimerEventHandler &cbPtr) { onFinishPtr = cbPtr; }
void enable() { isEnabled = true; }
void disable() { isEnabled = false; }
};
class TimerEventsClass {
public:
TimerEventsClass();
void add(TimerEvent* event);
void update(TimerEvent* event);
void tasks();
void printTasking(Stream* out);
int getNumberOfEvents() { return numberOfEvents; }
private:
int numberOfEvents;
TimerEvent* events[MAX_NUMBER_OF_EVENTS];
unsigned long timeouts[MAX_NUMBER_OF_EVENTS];
unsigned long durations[MAX_NUMBER_OF_EVENTS];
};
extern TimerEventsClass timerEvents;
#endif