Commanders
Arduino buttons/bus library
ButtonsCommanderSwitchOnePin.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 ButtonsCommanderSwitchOnePin::begin(unsigned long inId, int inPin)
18 {
19  this->Pin = Arduino_to_GPIO_pin(inPin);
20  this->Id = inId;
21 
22  pinMode2f(this->Pin, INPUT_PULLUP);
23 }
24 
26 {
27  if (this->Pin != DP_INVALID)
28  {
29  // Initialize first switch state at start
30  this->lastButtonState = digitalRead2f(this->Pin);
31 
33  if (this->lastButtonState == LOW)
34  this->lastSelectedPin = this->Pin;
35  }
36 }
37 
39 {
40  return ButtonsCommanderSwitch::loopOnePin(this->Pin, this->Id, this->lastSelectedPin == DP_INVALID ? UNDEFINED_ID : this->Id,
41  &this->debounceDelay, &this->lastSelectedPin,
42  &this->lastButtonState, &this->lastDebounceTime);
43 }
44 
45 #ifdef COMMANDERS_PRINT_COMMANDERS
46 void ButtonsCommanderSwitchOnePin::printCommander()
47 {
48  Serial.print(F(" SwitchOnePin Pin: "));
49  Serial.print(GPIO_to_Arduino_pin(this->Pin));
50  Serial.print(F(" / Id: "));
51  Serial.print(this->Id);
52  Serial.println(F(""));
53 }
54 #endif
55 #endif
56 #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)
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 inId, int inPin)
#define UNDEFINED_ID
Definition: Events.h:38