Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions src/LogansGreatButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const uint16_t DELAY_BEFORE_LONG_PRESS = 750; // How long must a button be
const uint16_t DELAY_BEFORE_BUTTON_HOLD = 1500; // How long before hold is registered
// AND the Time available to activate shift mode.
// This value should be longer then a long press.

unsigned int _ClickType = 0; // Update the type of trigger when the button state changes
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Constuctor
// All in one constructor
Expand Down Expand Up @@ -161,7 +161,10 @@ void LogansGreatButton::onShiftRelease(callBack ptr_onShiftRelease)
{
_CallBackShiftRelease = ptr_onShiftRelease;
}

unsigned int LogansGreatButton::getClickType()
{
return _ClickType;
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Logic Code

Expand Down Expand Up @@ -191,7 +194,11 @@ void LogansGreatButton::checkIfPressedOrReleased()
// This is the first click in this series
DEBUG_PRINT("\n_CallBackActionPressed");
_buttonCurrentState = BUTTON_STATE_PRESSED;
if(_CallBackActionPressed) _CallBackActionPressed();
if(_CallBackActionPressed)
{
_ClickType = _ActionPressed;
_CallBackActionPressed();
}
}
else if (_buttonCurrentState == BUTTON_STATE_MULTI_CLICK)
{
Expand Down Expand Up @@ -244,7 +251,11 @@ void LogansGreatButton::LOOPButtonController()
if (_isStartOfShift)
{
DEBUG_PRINT("\n_CallBackShiftStart");
if(_CallBackShiftStart) _CallBackShiftStart();
if(_CallBackShiftStart)
{
_ClickType = _ShiftStart;
_CallBackShiftStart();
}
_isStartOfShift = false;
}
//DEBUG_PRINT("\n_CallBackShiftContinuous");
Expand All @@ -257,18 +268,31 @@ void LogansGreatButton::LOOPButtonController()
{
DEBUG_PRINT("\n_CallBackHoldStart");
_buttonCurrentState = BUTTON_STATE_HOLD;
if(_CallBackHoldStart) _CallBackHoldStart();
if(_CallBackHoldStart)
{
_ClickType = _HoldStart;
_CallBackHoldStart();
}
_isStartOfHold = false;
}
DEBUG_PRINT(".")
if(_CallBackHoldContinuous) _CallBackHoldContinuous(); // Runs Continuously while button is held down
if(_CallBackHoldContinuous)
{
_ClickType = _HoldContinuous;
_CallBackHoldContinuous(); // Runs Continuously while button is held down
}
}
// 5. OR Acknowlage the long press
else if (isButtonLongPress()) // Long press is slightly shorter then hold, and therefore tested second using an else
{
DEBUG_PRINT("\n_CallBackPressLongStart");
_buttonCurrentState = BUTTON_STATE_LONG_PRESSED;
if(_CallBackPressLongStart) _CallBackPressLongStart();
if(_CallBackPressLongStart)
{
_ClickType = _PressLongStart;
_CallBackPressLongStart();
}

}
}
}
Expand Down Expand Up @@ -329,23 +353,35 @@ void LogansGreatButton::buttonActionReleased()
{
DEBUG_PRINT("\n_CallBackPressLongRelease");
_buttonCurrentState = BUTTON_STATE_WAITING; // Reset the button state ready for next press
if(_CallBackPressLongRelease) _CallBackPressLongRelease(); // This mode may have a release event to undo the prevously held state
if(_CallBackPressLongRelease)
{
_ClickType = _PressLongRelease;
_CallBackPressLongRelease(); // This mode may have a release event to undo the prevously held state
}
}
break;
case BUTTON_STATE_HOLD:
{
DEBUG_PRINT("\n_CallBackHoldRelease");
_isStartOfHold = true; // Reset _isStartOfHold so that is ready to accept the start on a new hold next time
_buttonCurrentState = BUTTON_STATE_WAITING; // Reset the button state ready for next press
if(_CallBackHoldRelease) _CallBackHoldRelease(); // This mode may have a release event to undo the prevously held state
if(_CallBackHoldRelease)
{
_ClickType = _HoldRelease;
_CallBackHoldRelease(); // This mode may have a release event to undo the prevously held state
}
}
break;
case BUTTON_STATE_SHIFT:
{
DEBUG_PRINT("\n_CallBackShiftRelease");
_isStartOfShift = true; // Reset _isStartOfShift so that is ready to accept the start on a new shift next time
_buttonCurrentState = BUTTON_STATE_WAITING; // This needs to be done before _CallBackShiftRelease otherwise isButtonInShiftMode() returns the wrong value when called in _CallBackShiftRelease
if(_CallBackShiftRelease) _CallBackShiftRelease(); // This callback allows a user set release event
if(_CallBackShiftRelease)
{
_ClickType = _ShiftRelease;
_CallBackShiftRelease(); // This callback allows a user set release event
}
}
break;
default: break;
Expand All @@ -362,7 +398,12 @@ void LogansGreatButton::releaseOfShortOrMultiClicks()
{
DEBUG_PRINT("\n_CallBackPressShortRelease");
_buttonCurrentState = BUTTON_STATE_WAITING; // Reset the button state ready for next press
if(_CallBackPressShortRelease) _CallBackPressShortRelease();
if(_CallBackPressShortRelease)
{
_ClickType = _PressShortRelease;
_CallBackPressShortRelease();
}

//_numberOfMultiClicks = 0; // Reset the multiclick counter
}
else
Expand Down
16 changes: 12 additions & 4 deletions src/LogansGreatButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ Created by Logan Krantz 2016/10/15.

#ifndef LogansGreatButton_h
#define LogansGreatButton_h

#define _ActionPressed 1
#define _PressShortRelease 2
#define _PressLongStart 3
#define _PressLongRelease 4
#define _HoldStart 5
#define _HoldContinuous 6
#define _HoldRelease 7
#define _MultiClicks 8
#define _ShiftStart 9
#define _ShiftRelease 10
#include <Arduino.h>
typedef void(*callBack)();

Expand All @@ -25,8 +34,7 @@ extern void onButtonHoldRelease();
extern void onMultiClicks();
extern void onButtonShiftStart();
extern void onButtonShiftRelease();


extern void getClickType();



Expand Down Expand Up @@ -92,7 +100,7 @@ class LogansGreatButton

// Returns the current number of multiclicks
uint16_t getNumberOfMultiClicks();

unsigned int getClickType();

private:
//void interruptButton();
Expand Down