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