-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnixartsTaskManager.cpp
78 lines (70 loc) · 1.33 KB
/
OnixartsTaskManager.cpp
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
#include "OnixartsTaskManager.h"
using namespace Onixarts::Tools::TaskManager;
Task::Task( TaskExecuteDelegate executeDelegate, unsigned long taskInterval, byte taskState, byte repeatCount )
: m_repeatIndex(0)
#ifndef OA_NO_CALLBACKS
,m_startDelegate( NULL)
, m_stopDelegate(NULL)
#endif
{
#ifndef OA_NO_CALLBACKS
m_executeDelegate = executeDelegate;
#endif
m_taskInterval = taskInterval;
m_state = taskState;
m_repeatCount = max(0, repeatCount);
}
void Task::Update(unsigned long now)
{
if(m_state == TaskState::Running )
{
if( now - m_lastExecutionTime > m_taskInterval )
{
m_lastExecutionTime = now;
Execute();
}
}
}
void Task::Execute()
{
OnExecute();
#ifndef OA_NO_CALLBACKS
if( m_executeDelegate != NULL )
m_executeDelegate();
#endif
if( m_repeatCount > 0 )
{
m_repeatIndex++;
if( m_repeatIndex == m_repeatCount )
Stop();
}
}
void Task::Start()
{
if (m_state == TaskState::Stopped)
Restart();
}
void Task::Restart()
{
m_lastExecutionTime = millis();
m_state = TaskState::Running;
m_repeatIndex = 0;
OnStart();
#ifndef OA_NO_CALLBACKS
if( m_startDelegate != NULL )
m_startDelegate();
#endif
}
void Task::Stop()
{
if (m_state == TaskState::Running)
{
m_state = TaskState::Stopped;
m_repeatIndex = 0;
OnStop();
#ifndef OA_NO_CALLBACKS
if (m_stopDelegate != NULL)
m_stopDelegate();
#endif
}
}