Commanders
Arduino buttons/bus library
ButtonsCommanderSwitchTwoPins.cpp
1 /*************************************************************
2 project: <Commanders>
3 author: <Thierry PARIS>
4 description: <Switch button with one pin only with debounce.>
5 *************************************************************/
6 
7 #include <Commanders.h>
8 #ifndef NO_BUTTONSCOMMANDER
9 #ifndef NO_BUTTONSCOMMANDERSWITCH
10 
12 {
13  this->debounceDelay = 50;
14 }
15 
16 void ButtonsCommanderSwitchTwoPins::begin(unsigned long inId1, int inPin1, unsigned long inId2, int inPin2)
17 {
18  this->Pin1 = Arduino_to_GPIO_pin(inPin1);
19  this->Id = inId1;
20  this->lastButtonState1 = HIGH;
21  this->lastDebounceTime1 = 0;
22  this->Pin2 = Arduino_to_GPIO_pin(inPin2);
23  this->Id2 = inId2;
24  this->lastButtonState2 = HIGH;
25  this->lastDebounceTime2 = 0;
26 
27  pinMode2f(this->Pin1, INPUT_PULLUP);
28  pinMode2f(this->Pin2, INPUT_PULLUP);
29 }
30 
32 {
33  if (this->Pin1 != DP_INVALID)
34  {
35  // Initialize first switch state at start
36  int reading = digitalRead2f(this->Pin1);
37  this->lastButtonState1 = reading;
38 
39  if (reading == LOW)
40  {
42  }
43  else
44  {
46  reading = digitalRead2f(this->Pin2);
47  this->lastButtonState2 = reading;
49  }
50  }
51 }
52 
54 {
55  unsigned long haveFound = ButtonsCommanderSwitch::loopOnePin(this->Id, this->Pin1, this->Id, this->debounceDelay, &this->lastButtonState1, &this->lastDebounceTime1);
56 
57  if (haveFound != UNDEFINED_ID)
58  return haveFound;
59 
60  return ButtonsCommanderSwitch::loopOnePin(this->Id2, this->Pin2, this->Id2, this->debounceDelay, &this->lastButtonState2, &this->lastDebounceTime2);
61 }
62 
63 #ifdef COMMANDERS_PRINT_COMMANDERS
64 void ButtonsCommanderSwitchTwoPins::printCommander()
65 {
66  Serial.print(F(" SwitchTwoPins Pin1: "));
67  Serial.print(GPIO_to_Arduino_pin(this->Pin1));
68  Serial.print(F(" / Id1: "));
69  Serial.print(this->Id);
70  Serial.print(F(" / Pin2: "));
71  Serial.print(GPIO_to_Arduino_pin(this->Pin2));
72  Serial.print(F(" / Id2: "));
73  Serial.print(this->Id2);
74  Serial.print(F(" / Debounce delay: "));
75  Serial.print(this->debounceDelay);
76  Serial.println(F(""));
77 }
78 #endif
79 #endif
80 #endif
static unsigned long loopOnePin(unsigned long inId, GPIO_pin_t inPin, unsigned long inPreviousId, unsigned long inDebounceDelay, byte *inpLastPinState, unsigned long *inpLastDebounceTime, bool inSendEvent = true)
static unsigned long RaiseEvent(unsigned long inId, COMMANDERS_EVENT_TYPE inEvent = COMMANDERS_EVENT_MOVEPOSITIONID, int inData = 0)
Definition: Commanders.cpp:27
void begin(unsigned long inId1, int inPin1, unsigned long inId2, int inPin2)
#define UNDEFINED_ID
Definition: Events.h:38