Commanders
Arduino buttons/bus library
ButtonsCommander.cpp
1 /*************************************************************
2 project: <Commanders>
3 author: <Thierry PARIS>
4 description: <Buttons Commander>
5 *************************************************************/
6 
7 #include <Commanders.h>
8 #ifdef NO_BUTTONSCOMMANDER
9 #pragma message ("Commanders : No Buttons commander !")
10 #else
11 #ifdef VISUALSTUDIO
12 #include <stdarg.h>
13 #endif
14 
15 #ifdef COMMANDERS_DEBUG_MODE
16 #define CHECK(val, text) CheckIndex(val, F(text))
17 #else
18 #define CHECK(val, text)
19 #endif
20 
21 ButtonsCommanderClass *ButtonsCommanderClass::pButtonsCommander;
22 
24 {
25  this->pFirstButton = NULL;
26 }
27 
29 {
30 #ifdef COMMANDERS_DEBUG_MODE
31  Serial.println(F(" ButtonsCommander begin"));
32 #endif
33 }
34 
36 {
37  if (pButtonsCommander == NULL)
38  pButtonsCommander = new ButtonsCommanderClass();
39 
40  return pButtonsCommander->Add(inButton);
41 }
42 
44 {
45  if (this->pFirstButton == NULL)
46  {
47  this->pFirstButton = inButton;
48  return inButton;
49  }
50 
51  ButtonsCommanderButton *pCurr = this->pFirstButton;
52 
53  while (pCurr->GetNextButton() != NULL)
54  pCurr = pCurr->GetNextButton();
55 
56  pCurr->SetNextButton(inButton);
57 
58  return inButton;
59 }
60 
62 {
63  ButtonsCommanderButton *pCurr = this->pFirstButton;
64 
65  while (pCurr != NULL)
66  {
67  ButtonsCommanderButton *pButton = pCurr->GetFromId(inId);
68  if (pButton != NULL)
69  return pButton;
70  pCurr = pCurr->GetNextButton();
71  }
72 
73  return 0;
74 }
75 
76 static ButtonsCommanderButton *pCurrentLoopButton = NULL;
77 
79 {
80  ButtonsCommanderButton *pCurr = this->pFirstButton;
81 
82  while (pCurr != NULL)
83  {
84  pCurr->beforeFirstLoop();
85  pCurr = pCurr->GetNextButton();
86  }
87 }
88 
90 {
91  if (pCurrentLoopButton != NULL)
92  pCurrentLoopButton = pCurrentLoopButton->GetNextButton();
93 
94  if (pCurrentLoopButton == NULL)
95  pCurrentLoopButton = this->pFirstButton;
96 
97  if (pCurrentLoopButton == NULL)
98  return UNDEFINED_ID;
99 
100  unsigned long ID = pCurrentLoopButton->loop();
101 
102  if (ID == UNDEFINED_ID)
103  return UNDEFINED_ID;
104 
105  pCurrentLoopButton->EndLoop();
106  return ID;
107 }
108 
109 #ifdef COMMANDERS_PRINT_COMMANDERS
110 void ButtonsCommanderClass::printCommander()
111 {
112  Serial.println(F("Commander: ButtonsCommander "));
113 
114  ButtonsCommanderButton *pCurr = this->pFirstButton;
115 
116  while (pCurr != NULL)
117  {
118  pCurr->printCommander();
119  pCurr = pCurr->GetNextButton();
120  }
121 }
122 #endif
123 
124 #endif
virtual unsigned long loop()
#define UNDEFINED_ID
Definition: Events.h:38
virtual ButtonsCommanderButton * GetFromId(unsigned long inId)
static ButtonsCommanderButton * AddButton(ButtonsCommanderButton *inpButton)
void SetNextButton(ButtonsCommanderButton *inpButton)
ButtonsCommanderButton * GetFromId(unsigned long inId) const
ButtonsCommanderButton * Add(ButtonsCommanderButton *inpButton)
ButtonsCommanderButton * GetNextButton() const