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->lastButtonState = HIGH;
15  this->lastDebounceTime = 0;
16  this->Pin = DP_INVALID;
17 }
18 
19 void ButtonsCommanderSwitchOnePin::begin(unsigned long inId, int inPin)
20 {
21  this->Pin = Arduino_to_GPIO_pin(inPin);
22  this->Id = inId;
23 
24  pinMode2f(this->Pin, INPUT_PULLUP);
25 }
26 
28 {
29  if (this->Pin != DP_INVALID)
30  {
31  // Initialize first switch state at start
32  this->lastButtonState = digitalRead2f(this->Pin);
33 
35  }
36 }
37 
39 {
40  bool changed = ButtonsCommanderSwitch::HavePinStateChanged(this->Pin, this->debounceDelay, &this->lastButtonState, &this->lastDebounceTime);
41 
42  if (changed == true)
43  {
45 
46  if (this->lastButtonState == LOW)
47  return this->Id;
48  }
49 
50  return UNDEFINED_ID;
51 }
52 
53 #ifdef COMMANDERS_PRINT_COMMANDERS
54 void ButtonsCommanderSwitchOnePin::printCommander()
55 {
56  Serial.print(F(" SwitchOnePin Pin: "));
57  Serial.print(GPIO_to_Arduino_pin(this->Pin));
58  Serial.print(F(" / Id: "));
59  Serial.print(this->Id);
60  Serial.print(F(" / Debounce delay: "));
61  Serial.print(this->debounceDelay);
62  Serial.println(F(""));
63 }
64 #endif
65 #endif
66 #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 inId, int inPin)
#define UNDEFINED_ID
Definition: Events.h:38