Commanders
Arduino buttons/bus library
ButtonsCommanderPush.cpp
1 /*************************************************************
2 project: <Commanders>
3 author: <Thierry PARIS>
4 description: <Push button with debounce.>
5 *************************************************************/
6 
7 #include <Commanders.h>
8 #include <stdint.h>
9 #ifndef NO_BUTTONSCOMMANDER
10 #ifndef NO_BUTTONSCOMMANDERPUSH
11 
13 {
14  this->buttonPin = (GPIO_pin_t)DP_INVALID;
15  this->lastButtonState = HIGH;
16  this->lastDebounceTime = 0;
17  this->debounceDelay = 50;
18 }
19 
20 void ButtonsCommanderPush::begin(unsigned long inId, int inButtonPin, COMMANDERS_EVENT_TYPE inEventType, int inData)
21 {
22  this->Id = inId;
23  this->buttonPin = Arduino_to_GPIO_pin(inButtonPin);
24 
25  pinMode2f(this->buttonPin, INPUT_PULLUP);
26 
27  if (inId != UNDEFINED_ID)
28  this->AddEvent(inId, inEventType, inData);
29 }
30 
31 // Returns the index of the new added position.
32 void ButtonsCommanderPush::AddEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEventType, int inData)
33 {
34  Event *pEvent = new Event();
35  pEvent->Id = inId;
36  pEvent->EventType = inEventType;
37  pEvent->Data = inData;
38  this->Events.AddItem(pEvent);
39 }
40 
42 {
43  if (this->Events.pFirst == NULL)
44  {
45 #ifdef COMMANDERS_DEBUG_MODE
46  if (this->debounceDelay != UINT32_MAX) // If the error message has not been yet shown...
47  {
48  Serial.println(F("This push button have no ID defined : call begin() !"));
49  // use it as a debug flag !
50  this->debounceDelay = UINT32_MAX; // The error message has been shown...
51  }
52 #endif
53  return UNDEFINED_ID;
54  }
55 
56  unsigned long foundID = UNDEFINED_ID;
57 
58  if (this->buttonPin == DP_INVALID)
59  return foundID;
60 
61  // read the state of the switch into a local variable:
62  int reading = digitalRead2f(this->buttonPin);
63 
64  // check to see if you just pressed the button
65  // (i.e. the input went from LOW to HIGH), and you've waited
66  // long enough since the last press to ignore any noise:
67 
68  // If the switch changed, due to noise or pressing:
69  if (reading != this->lastButtonState)
70  {
71  // reset the debouncing timer
72  this->lastDebounceTime = millis();
73  }
74 
75  if (this->lastDebounceTime > 0 && (millis() - this->lastDebounceTime) > this->debounceDelay)
76  {
77  // whatever the reading is at, it's been there for longer
78  // than the debounce delay, so take it as the actual current state:
79 
80  // if the button state has changed:
81  if (reading == this->lastButtonState)
82  {
83  // only toggle the state if the new button state is LOW
84  if (reading == LOW)
85  {
86  foundID = this->Events.pCurrentItem->Obj->Id;
87  Commanders::RaiseEvent(foundID,
88  this->Events.pCurrentItem->Obj->EventType,
89  this->Events.pCurrentItem->Obj->Data);
90 
91  this->Events.NextCurrent();
92  }
93  }
94  this->lastDebounceTime = 0;
95  }
96 
97  // save the reading. Next time through the loop,
98  // it'll be the lastButtonState:
99  this->lastButtonState = reading;
100  return foundID;
101 }
102 
103 #ifdef COMMANDERS_PRINT_COMMANDERS
104 void ButtonsCommanderPush::printCommander()
105 {
106  Serial.print(F(" Push - Pin :"));
107  Serial.println(this->GetPin());
108 
109  CMDRSCHAINEDLISTITEM<Event> *pCurr = this->Events.pFirst;
110  while (pCurr != NULL)
111  {
112  Serial.print(F(" Event Id: "));
113  Serial.print(pCurr->Obj->Id);
114  Serial.print(F(" / Event type: "));
116  Commanders::printEventData(pCurr->Obj->EventType, pCurr->Obj->Data);
117  Serial.print(F(" / Debounce delay: "));
118  Serial.print(this->debounceDelay);
119  Serial.println(F(""));
120 
121  pCurr = pCurr->pNext;
122  }
123 }
124 #endif
125 #endif
126 #endif
CMDRSCHAINEDLISTITEM * pNext
Definition: Chain.hpp:13
static void printEventData(COMMANDERS_EVENT_TYPE inEventType, int inEventData)
COMMANDERS_EVENT_TYPE
Definition: Events.h:25
void AddItem(T *input)
Definition: Chain.hpp:61
CMDRSCHAINEDLISTITEM< T > * pCurrentItem
Definition: Chain.hpp:39
#define UNDEFINED_ID
Definition: Events.h:38
void begin(unsigned long inId, int inButtonPin, COMMANDERS_EVENT_TYPE inEventType = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
void NextCurrent()
Definition: Chain.hpp:85
void AddEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEventType = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
COMMANDERS_EVENT_TYPE EventType
CMDRSCHAINEDLISTITEM< T > * pFirst
Definition: Chain.hpp:37
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27
unsigned long Id
static void printEventType(COMMANDERS_EVENT_TYPE inEventType, bool inDataFollow)