forked from apps-cs/apps-lcd-opencv-simul
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicker.hpp
More file actions
45 lines (38 loc) · 735 Bytes
/
Copy pathTicker.hpp
File metadata and controls
45 lines (38 loc) · 735 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <thread>
class Ticker
{
private:
std::thread t;
int delay;
void (*callback)() = nullptr;
public:
void attach(void (*callback)(), std::chrono::microseconds delay)
{
this->callback = callback;
this->delay = std::chrono::duration_cast<std::chrono::microseconds>(delay).count();
}
void detach()
{
callback = nullptr;
}
void loop()
{
while (1)
{
usleep(this->delay);
if (this->callback)
this->callback();
}
}
Ticker()
{
t = std::thread(&Ticker::loop, this);
}
~Ticker()
{
t.detach();
}
};