Skip to content

Commit 33d7db7

Browse files
expand button config
1 parent 93aaa9f commit 33d7db7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Button/Button.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
#include "Button.h"
99

10-
Button::Button(int btn_pin, int pin_mode) {
10+
Button::Button(int btn_pin, int pin_mode, int last_state, int active_state) {
1111
_btnPin = btn_pin;
1212
_pinMode = pin_mode;
13-
_lastBtnState = LOW;
13+
_lastBtnState = last_state;
14+
_activeState = active_state;
1415
}
1516

1617
void Button::begin(Method callback) {
@@ -30,14 +31,14 @@ void Button::read() {
3031
if (val != _lastBtnState) {
3132
// reset the debouncing timer
3233
_lastDebounceTime = millis();
34+
3335
}
3436
if ((millis() - _lastDebounceTime) > _debounceDelay) {
3537
// if the button state has changed
3638
if (val != _currentBtnState) {
3739
_currentBtnState = val;
3840

39-
// only toggle if the new button state is HIGH
40-
if (_currentBtnState == HIGH) {
41+
if (_currentBtnState == _activeState) {
4142
_callback.callback();
4243
}
4344
}

Button/Button.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class Button
1414
{
1515
public:
16-
Button(int btn_pin, int pin_mode = INPUT);
16+
Button(int btn_pin, int pin_mode = INPUT, int last_state = LOW, int active_state = HIGH);
1717
void loop();
1818
void begin(Method callback);
1919
void read();
@@ -23,6 +23,7 @@ class Button
2323
int _pinMode;
2424
int _currentBtnState;
2525
int _lastBtnState;
26+
int _activeState;
2627
Method _callback;
2728
unsigned long _lastDebounceTime = 0;
2829
unsigned long _debounceDelay = 50;

0 commit comments

Comments
 (0)