forked from Toshik/TickerScheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TickerScheduler.h
89 lines (74 loc) · 1.68 KB
/
TickerScheduler.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef TICKERSCHEDULER_H
#define TICKERSCHEDULER_H
#include <Arduino.h>
#include <stdint.h>
#ifdef ARDUINO_ARCH_AVR
class Ticker
{
typedef void(*ticker_callback_t)(bool*);
private:
bool is_attached = false;
uint32_t period = 0;
uint32_t last_tick = 0;
ticker_callback_t callback;
bool *callback_argument;
public:
void Tick()
{
if (is_attached && millis() - last_tick >= period)
{
callback(callback_argument);
last_tick = millis();
}
}
void detach()
{
this->is_attached = true;
}
template<typename TArg> void attach_ms(uint32_t milliseconds, void(*callback)(TArg), TArg arg)
{
this->period = milliseconds;
this->callback = callback;
this->callback_argument = arg;
this->is_attached = true;
}
};
#endif
#ifdef ARDUINO_ARCH_ESP8266
#include <Ticker.h>
#include <functional>
#endif
void tickerFlagHandle(volatile bool * flag);
#ifdef _GLIBCXX_FUNCTIONAL
typedef std::function<void(void)> tscallback_t;
#else
typedef void(*tscallback_t)(void);
#endif
struct TickerSchedulerItem
{
Ticker t;
volatile bool flag = false;
tscallback_t cb;
uint32_t period;
volatile bool is_used = false;
};
class TickerScheduler
{
private:
uint8_t size;
TickerSchedulerItem *items = NULL;
void handleTicker(tscallback_t, volatile bool * flag);
static void handleTickerFlag(volatile bool * flag);
public:
TickerScheduler(uint8_t size);
~TickerScheduler();
bool add(uint8_t i, uint32_t period, tscallback_t, bool shouldFireNow = false);
bool remove(uint8_t i);
bool enable(uint8_t i);
bool disable(uint8_t i);
bool changePeriod(uint8_t i, uint32_t period);
void enableAll();
void disableAll();
void update();
};
#endif