Accessories
Arduino for motors and lights library.
ActionsStack.cpp
1 /*************************************************************
2 project: <Accessories>
3 author: <Thierry PARIS>
4 description: <Class for a actions stack>
5 *************************************************************/
6 
7 #include "ActionsStack.hpp"
8 
9 Action::Action(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData)
10 {
11  this->Id = inId;
12  this->Event = inEvent;
13  this->Data = inData;
14 }
15 
17 bool ActionsStack::FillingStack(false);
18 
20 {
21  this->size = inSize;
22  this->pList = new Action*[inSize];
23 
24  for (int i = 0; i < this->size; i++)
25  {
26  this->pList[i] = NULL;
27  }
28 }
29 
30 // Returns the index of the new added action.
31 unsigned char ActionsStack::Add(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData)
32 {
33  for (int i = 0; i < this->size; i++)
34  {
35  if (this->pList[i] == NULL)
36  {
37 #ifdef ACCESSORIES_DEBUG_MODE
38  Serial.print(F("Action "));
39  Serial.print(i);
40  Serial.println(F(" added !"));
41 #endif
42  this->pList[i] = new Action(inId, inEvent, inData);
43  return i;
44  }
45  }
46 
47  return this->size + 1; // action lost, the stack is full !
48 }
49 
51 {
52  for (int i = 0; i < this->size; i++)
53  if (this->pList[i] != NULL)
54  return i;
55 
56  return 255; // no action to execute
57 }
58 
59 // Returns the index of the new added action.
60 void ActionsStack::Delete(int inIndex)
61 {
62 #ifdef ACCESSORIES_DEBUG_MODE
63  Serial.print(F("Action "));
64  Serial.print(inIndex);
65  Serial.println(F(" deleted !"));
66 #endif
67  if (this->pList[inIndex] != NULL)
68  {
69  delete this->pList[inIndex];
70  this->pList[inIndex] = NULL;
71  }
72 }
73 
74 // Returns the index of the new added action.
76 {
77  for (int i = 0; i < this->size; i++)
78  this->Delete(i);
79 }
80 
81 // Returns the number of stacked actions.
83 {
84  int count = 0;
85 
86  for (int i = 0; i < this->size; i++)
87  {
88  if (this->pList[i] != NULL)
89  count++;
90  }
91 
92  return count;
93 }
int GetNumber() const
void Delete(int inIndex)
unsigned char Add(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData = 0)
ActionsStack(int inSize)
static bool FillingStack
#define ACTION_STACK_SIZE
Definition: Accessories.h:351
static ActionsStack Actions
ACCESSORIES_EVENT_TYPE Event
unsigned long Id
unsigned char GetActionToExecute()
Action(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData = 0)
Definition: ActionsStack.cpp:9