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  this->Pin1 = DP_INVALID;
15  this->Pin2 = DP_INVALID;
16 }
17 
18 void ButtonsCommanderSwitchTwoPins::begin(unsigned long inId1, int inPin1, unsigned long inId2, int inPin2)
19 {
20  this->Pin1 = Arduino_to_GPIO_pin(inPin1);
21  this->Id = inId1;
22  this->lastButtonState1 = HIGH;
23  this->lastDebounceTime1 = 0;
24 
25  this->Pin2 = Arduino_to_GPIO_pin(inPin2);
26  this->Id2 = inId2;
27  this->lastButtonState2 = HIGH;
28  this->lastDebounceTime2 = 0;
29 
30  pinMode2f(this->Pin1, INPUT_PULLUP);
31  pinMode2f(this->Pin2, INPUT_PULLUP);
32 }
33 
35 {
36  if (this->Pin1 != DP_INVALID)
37  {
38  // Initialize first switch state at start
39  int reading = digitalRead2f(this->Pin1);
40  this->lastButtonState1 = reading;
41 
42  if (reading == LOW)
43  {
45  }
46  else
47  {
49  reading = digitalRead2f(this->Pin2);
50  this->lastButtonState2 = reading;
52  }
53  }
54 }
55 
57 {
58  bool changed = ButtonsCommanderSwitch::HavePinStateChanged(this->Pin1, this->debounceDelay, &this->lastButtonState1, &this->lastDebounceTime1);
59  if (changed)
60  Serial.println(F("1 changed !"));
61 
62  if (changed == true && this->lastButtonState1 == LOW)
63  {
64  Serial.println(F("this->lastButtonState1 = LOW"));
67  return this->Id;
68  }
69 
70  changed = ButtonsCommanderSwitch::HavePinStateChanged(this->Pin2, this->debounceDelay, &this->lastButtonState2, &this->lastDebounceTime2);
71  if (changed)
72  Serial.println(F("2 changed !"));
73 
74  if (changed == true && this->lastButtonState2 == LOW)
75  {
76  Serial.println(F("this->lastButtonState2 = LOW"));
79  return this->Id2;
80  }
81 
82  return UNDEFINED_ID;
83 }
84 
85 #ifdef COMMANDERS_PRINT_COMMANDERS
86 void ButtonsCommanderSwitchTwoPins::printCommander()
87 {
88  Serial.print(F(" SwitchTwoPins Pin1: "));
89  Serial.print(GPIO_to_Arduino_pin(this->Pin1));
90  Serial.print(F(" / Id1: "));
91  Serial.print(this->Id);
92  Serial.print(F(" / Pin2: "));
93  Serial.print(GPIO_to_Arduino_pin(this->Pin2));
94  Serial.print(F(" / Id2: "));
95  Serial.print(this->Id2);
96  Serial.print(F(" / Debounce delay: "));
97  Serial.print(this->debounceDelay);
98  Serial.println(F(""));
99 }
100 #endif
101 #endif
102 #endif
static bool HavePinStateChanged(GPIO_pin_t inPin, unsigned long inDebounceDelay, byte *inpCurrentPinState, unsigned long *inpLastDebounceTime)
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