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->lastSelectedPin = DP_INVALID;
15 }
16 
17 void ButtonsCommanderSwitchTwoPins::begin(unsigned long inId1, int inPin1, unsigned long inId2, int inPin2)
18 {
19  this->Pin1 = Arduino_to_GPIO_pin(inPin1);
20  this->Id = inId1;
21  this->Pin2 = Arduino_to_GPIO_pin(inPin2);
22  this->Id2 = inId2;
23 
24  pinMode2f(this->Pin1, INPUT_PULLUP);
25  pinMode2f(this->Pin2, INPUT_PULLUP);
26 }
27 
29 {
30  if (this->Pin1 != DP_INVALID)
31  {
32  // Initialize first switch state at start
33  int reading = digitalRead2f(this->Pin1);
34 
35  if (reading == LOW)
36  {
37  this->lastSelectedPin = this->Pin1;
39  }
40  else
41  {
43  reading = digitalRead2f(this->Pin2);
44  if (reading == LOW)
45  this->lastSelectedPin = this->Pin2;
47  }
48 
49  this->lastButtonState = reading;
50  }
51 }
52 
54 {
55  unsigned long haveFound = ButtonsCommanderSwitch::loopOnePin(this->Pin1, this->Id, this->GetId(this->lastSelectedPin),
56  &this->debounceDelay, &this->lastSelectedPin,
57  &this->lastButtonState, &this->lastDebounceTime);
58 
59  if (haveFound != UNDEFINED_ID)
60  return haveFound;
61 
62  return ButtonsCommanderSwitch::loopOnePin(this->Pin2, this->Id2, this->GetId(this->lastSelectedPin),
63  &this->debounceDelay, &this->lastSelectedPin,
64  &this->lastButtonState, &this->lastDebounceTime);
65 }
66 
67 #ifdef COMMANDERS_PRINT_COMMANDERS
68 void ButtonsCommanderSwitchTwoPins::printCommander()
69 {
70  Serial.print(F(" SwitchTwoPins Pin1: "));
71  Serial.print(GPIO_to_Arduino_pin(this->Pin1));
72  Serial.print(F(" / Id1: "));
73  Serial.print(this->Id);
74  Serial.print(F(" / Pin2: "));
75  Serial.print(GPIO_to_Arduino_pin(this->Pin2));
76  Serial.print(F(" / Id2: "));
77  Serial.print(this->Id2);
78  Serial.println(F(""));
79 }
80 #endif
81 #endif
82 #endif
static unsigned long loopOnePin(GPIO_pin_t inPin, unsigned long inId, unsigned long inPreviousId, unsigned long *apDebounceDelay, GPIO_pin_t *apLastSelectedPin, int *apLastButtonState, unsigned long *apLastDebounceTime, bool inSendEvent = true)
unsigned long GetId() const
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