Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7299d67

Browse files
authored
removed the long constructor
The constructor was very long I now changed it to a simple constructor where you just setup the desired button pin. Then the user can simply select what callback it needs. No need anymore to setup ALL the callbacks before you can use this library.
1 parent 3675b90 commit 7299d67

1 file changed

Lines changed: 72 additions & 32 deletions

File tree

src/LogansGreatButton.cpp

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,7 @@ const uint16_t DELAY_BEFORE_BUTTON_HOLD = 1500; // How long before hold is
4646
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4747
// Constuctor
4848

49-
LogansGreatButton::LogansGreatButton(uint8_t buttonPin,
50-
callBack onActionPressed,
51-
callBack onPressShortRelease,
52-
callBack onPressLongStart,
53-
callBack onPressLongRelease,
54-
callBack onHoldStart,
55-
callBack onHoldContinuous,
56-
callBack onHoldRelease,
57-
callBack onMultiClick,
58-
callBack onShiftStart,
59-
callBack onShiftRelease)
49+
LogansGreatButton::LogansGreatButton(uint8_t buttonPin)
6050
{
6151
// Setup Button Output and interrupts
6252
_PIN_BUTTON = buttonPin;
@@ -66,22 +56,72 @@ LogansGreatButton::LogansGreatButton(uint8_t buttonPin,
6656

6757

6858
// Setup CallBackFunctions
69-
_CallBackActionPressed = onActionPressed;
70-
_CallBackPressShortRelease = onPressShortRelease;
71-
_CallBackPressLongStart = onPressLongStart;
72-
_CallBackPressLongRelease = onPressLongRelease;
73-
_CallBackHoldStart = onHoldStart;
74-
_CallBackHoldContinuous = onHoldContinuous;
75-
_CallBackHoldRelease = onHoldRelease;
76-
_CallBackMultiClicks = onMultiClick;
77-
_CallBackShiftStart = onShiftStart;
78-
_CallBackShiftRelease = onShiftRelease;
59+
_CallBackActionPressed = 0;
60+
_CallBackPressShortRelease = 0;
61+
_CallBackPressLongStart = 0;
62+
_CallBackPressLongRelease = 0;
63+
_CallBackHoldStart = 0;
64+
_CallBackHoldContinuous = 0;
65+
_CallBackHoldRelease = 0;
66+
_CallBackMultiClicks = 0;
67+
_CallBackShiftStart = 0;
68+
_CallBackShiftRelease = 0;
7969

8070
// Re-assigning Start Booleans avoids errors for some reason
8171
_isStartOfHold = true;
8272
_isStartOfShift = true;
8373
}
8474

75+
void LogansGreatButton::onActionPressed(callBack ptr_onActionPressed)
76+
{
77+
_CallBackActionPressed = ptr_onActionPressed;
78+
}
79+
80+
void LogansGreatButton::onPressShortRelease(callBack ptr_onPressShortRelease)
81+
{
82+
_CallBackPressShortRelease = ptr_onPressShortRelease;
83+
}
84+
85+
void LogansGreatButton::onPressLongStart(callBack ptr_onPressLongStart)
86+
{
87+
_CallBackPressLongStart = ptr_onPressLongStart;
88+
}
89+
90+
void LogansGreatButton::onPressLongRelease(callBack ptr_onPressLongRelease)
91+
{
92+
_CallBackPressLongRelease = ptr_onPressLongRelease;
93+
}
94+
95+
void LogansGreatButton::onHoldStart(callBack ptr_onHoldStart)
96+
{
97+
_CallBackHoldStart = ptr_onHoldStart;
98+
}
99+
100+
void LogansGreatButton::onHoldContinuous(callBack ptr_onHoldContinuous)
101+
{
102+
_CallBackHoldContinuous = ptr_onHoldContinuous;
103+
}
104+
105+
void LogansGreatButton::onHoldRelease(callBack ptr_onHoldRelease)
106+
{
107+
_CallBackHoldRelease = ptr_onHoldRelease;
108+
}
109+
110+
void LogansGreatButton::onMultiClick(callBack ptr_onMultiClick)
111+
{
112+
_CallBackMultiClicks = ptr_onMultiClick;
113+
}
114+
115+
void LogansGreatButton::onShiftStart(callBack ptr_onShiftStart)
116+
{
117+
_CallBackShiftStart = ptr_onShiftStart;
118+
}
119+
120+
void LogansGreatButton::onShiftRelease(callBack ptr_onShiftRelease)
121+
{
122+
_CallBackShiftRelease = ptr_onShiftRelease;
123+
}
124+
85125
void LogansGreatButton::checkIfPressedOrReleased()
86126
{
87127
boolean currentState = digitalRead(_PIN_BUTTON);
@@ -108,7 +148,7 @@ void LogansGreatButton::checkIfPressedOrReleased()
108148
// This is the first click in this series
109149
DEBUG_PRINT("\n_CallBackActionPressed");
110150
_buttonCurrentState = BUTTON_STATE_PRESSED;
111-
_CallBackActionPressed();
151+
if(_CallBackActionPressed) _CallBackActionPressed();
112152
}
113153
else if (_buttonCurrentState == BUTTON_STATE_MULTI_CLICK)
114154
{
@@ -161,7 +201,7 @@ void LogansGreatButton::LOOPButtonController()
161201
if (_isStartOfShift)
162202
{
163203
DEBUG_PRINT("\n_CallBackShiftStart");
164-
_CallBackShiftStart();
204+
if(_CallBackShiftStart) _CallBackShiftStart();
165205
_isStartOfShift = false;
166206
}
167207
//DEBUG_PRINT("\n_CallBackShiftContinuous");
@@ -174,18 +214,18 @@ void LogansGreatButton::LOOPButtonController()
174214
{
175215
DEBUG_PRINT("\n_CallBackHoldStart");
176216
_buttonCurrentState = BUTTON_STATE_HOLD;
177-
_CallBackHoldStart();
217+
if(_CallBackHoldStart) _CallBackHoldStart();
178218
_isStartOfHold = false;
179219
}
180220
DEBUG_PRINT(".")
181-
_CallBackHoldContinuous(); // Runs Continuously while button is held down
221+
if(_CallBackHoldContinuous) _CallBackHoldContinuous(); // Runs Continuously while button is held down
182222
}
183223
// 5. OR Acknowlage the long press
184224
else if (isButtonLongPress()) // Long press is slightly shorter then hold, and therefore tested second using an else
185225
{
186226
DEBUG_PRINT("\n_CallBackPressLongStart");
187227
_buttonCurrentState = BUTTON_STATE_LONG_PRESSED;
188-
_CallBackPressLongStart();
228+
if(_CallBackPressLongStart) _CallBackPressLongStart();
189229
}
190230
}
191231
}
@@ -246,23 +286,23 @@ void LogansGreatButton::buttonActionReleased()
246286
{
247287
DEBUG_PRINT("\n_CallBackPressLongRelease");
248288
_buttonCurrentState = BUTTON_STATE_WAITING; // Reset the button state ready for next press
249-
_CallBackPressLongRelease(); // This mode may have a release event to undo the prevously held state
289+
if(_CallBackPressLongRelease) _CallBackPressLongRelease(); // This mode may have a release event to undo the prevously held state
250290
}
251291
break;
252292
case BUTTON_STATE_HOLD:
253293
{
254294
DEBUG_PRINT("\n_CallBackHoldRelease");
255295
_isStartOfHold = true; // Reset _isStartOfHold so that is ready to accept the start on a new hold next time
256296
_buttonCurrentState = BUTTON_STATE_WAITING; // Reset the button state ready for next press
257-
_CallBackHoldRelease(); // This mode may have a release event to undo the prevously held state
297+
if(_CallBackHoldRelease) _CallBackHoldRelease(); // This mode may have a release event to undo the prevously held state
258298
}
259299
break;
260300
case BUTTON_STATE_SHIFT:
261301
{
262302
DEBUG_PRINT("\n_CallBackShiftRelease");
263303
_isStartOfShift = true; // Reset _isStartOfShift so that is ready to accept the start on a new shift next time
264304
_buttonCurrentState = BUTTON_STATE_WAITING; // This needs to be done before _CallBackShiftRelease otherwise isButtonInShiftMode() returns the wrong value when called in _CallBackShiftRelease
265-
_CallBackShiftRelease(); // This callback allows a user set release event
305+
if(_CallBackShiftRelease) _CallBackShiftRelease(); // This callback allows a user set release event
266306
}
267307
break;
268308
default: break;
@@ -279,7 +319,7 @@ void LogansGreatButton::releaseOfShortOrMultiClicks()
279319
{
280320
DEBUG_PRINT("\n_CallBackPressShortRelease");
281321
_buttonCurrentState = BUTTON_STATE_WAITING; // Reset the button state ready for next press
282-
_CallBackPressShortRelease();
322+
if(_CallBackPressShortRelease) _CallBackPressShortRelease();
283323
//_numberOfMultiClicks = 0; // Reset the multiclick counter
284324
}
285325
else
@@ -293,7 +333,7 @@ void LogansGreatButton::releaseOfShortOrMultiClicks()
293333
Serial.println(debugStr);
294334
}
295335
#endif // DEBUG_BUTTON
296-
_CallBackMultiClicks();
336+
if(_CallBackMultiClicks) _CallBackMultiClicks();
297337
//_numberOfMultiClicks = 0; // Reset the multiclick counter
298338
}
299339
}
@@ -344,7 +384,7 @@ boolean LogansGreatButton::isButtonReleased()
344384
// {
345385
// _lastButtonPressTime = _millisAtStartOfLoop; // Button Press accepted, reset time
346386
// _buttonCurrentState = BUTTON_STATE_PRESSED;
347-
// _CallBackActionPressed();
387+
// if(_CallBackActionPressed) _CallBackActionPressed();
348388
// }
349389
// }
350390
//

0 commit comments

Comments
 (0)