-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdSequence.cpp
More file actions
180 lines (159 loc) · 3.28 KB
/
CmdSequence.cpp
File metadata and controls
180 lines (159 loc) · 3.28 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* CmdSequence.cpp
*
* Created on: 10.03.2014
* Author: niklausd
*/
#include "SpinTimer.h"
#include "CmdAdapter.h"
#include "CmdSequence.h"
#include "Cmd.h"
//-----------------------------------------------------------------------------
class CmdSeqTimerAction : public SpinTimerAction
{
public:
CmdSeqTimerAction(CmdSequence* cmdSeq)
: m_cmdSeq(cmdSeq)
{ }
void timeExpired()
{
if (0 != m_cmdSeq)
{
m_cmdSeq->execNextCmd();
}
}
private:
CmdSequence* m_cmdSeq;
};
//-----------------------------------------------------------------------------
CmdSequence::CmdSequence(CmdAdapter* adapter)
: m_isRunning(false)
, m_firstCmd(0)
, m_currentCmd(0)
, m_cmdListIter(0)
, m_adapter(adapter)
, m_timer(new SpinTimer(0, new CmdSeqTimerAction(this), SpinTimer::IS_NON_RECURRING))
{ }
CmdSequence::~CmdSequence()
{
delete m_timer->action();
m_timer->attachAction(0);
delete m_timer;
m_timer = 0;
}
void CmdSequence::attachAdapter(CmdAdapter* adapter)
{
m_adapter = adapter;
}
void CmdSequence::start()
{
if (!isRunning())
{
m_currentCmd = m_firstCmd;
execCmd();
}
}
void CmdSequence::stop()
{
m_timer->cancel();
if (0 != adapter())
{
adapter()->stopAction();
}
m_isRunning = false;
}
bool CmdSequence::isRunning()
{
return m_isRunning;
}
Cmd* CmdSequence::currentCmd()
{
return m_currentCmd;
}
void CmdSequence::execNextCmd()
{
if (0 != m_timer)
{
m_timer->cancel(); // stop timer for the case the current command has not been finished by timeout
}
if (0 != m_currentCmd)
{
m_currentCmd->leave(); // leave the current command
m_currentCmd = m_currentCmd->next(); // proceed to the next command
}
execCmd();
}
void CmdSequence::execCmd()
{
if ((0 != m_currentCmd) && (0 != m_timer))
{
m_isRunning = true;
if (m_currentCmd->getTime() >= 0)
{
// time = 0: do not wait, immediately proceed to the next command of the sequence since the timer will expire immediately
// time > 0: wait in this command the specified time [ms]
unsigned long int currentCmdTime = static_cast<unsigned long int>(m_currentCmd->getTime());
m_timer->start(currentCmdTime);
}
// else
// {
// // time < 0: wait forever in this command, since the timer is not started,
// // => this makes the sequence only proceed to the next command on an explicit call to the method CmdSequence::execNextCmd().
// }
m_currentCmd->execute();
}
else
{
m_isRunning = false;
if (0 != m_timer)
{
m_timer->cancel();
}
m_currentCmd = m_firstCmd;
}
}
void CmdSequence::attach(Cmd* cmd)
{
if (0 == m_firstCmd)
{
m_firstCmd = cmd;
}
else
{
Cmd* next = m_firstCmd;
while (next->next() != 0)
{
next = next->next();
}
next->setNext(cmd);
}
}
void CmdSequence::detach(Cmd* cmd)
{
if (m_firstCmd == cmd)
{
m_firstCmd = cmd->next();
}
else
{
Cmd* next = m_firstCmd;
while ((next != 0) && next->next() != cmd)
{
next = next->next();
}
if (next != 0)
{
next->setNext(cmd->next());
}
}
}
Cmd* CmdSequence::getFirstCmd()
{
m_cmdListIter = m_firstCmd;
return m_cmdListIter;
}
Cmd* CmdSequence::getNextCmd()
{
m_cmdListIter = m_cmdListIter->next();
return m_cmdListIter;
}