Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
added custom buttons (e.g. on PCF8574)
added PCF8574 example
long press and release method combo fix
  • Loading branch information
serek4 committed Oct 2, 2020
2 parents 0dbceb1 + 9a2294d commit 0390c73
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
78 changes: 78 additions & 0 deletions examples/pcf8574-demo/pcf8574-demo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <PCF8574.h>
#include <debouncedButton.h>

#define LED1 13
#define LED2 14

boolean led1State = HIGH;
boolean led2State = HIGH;

PCF8574 keypad(0x20);
button button1(true, keypad.digitalInput.p0);
button button2(true, keypad.digitalInput.p1);
int mode = 0;
char *modeStr[6] = {"press", "repeat", "long press", "release", "hold", "press"};

void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, led1State);
digitalWrite(LED2, led2State);
keypad.pinMode(P0, INPUT_PULLUP);
keypad.pinMode(P1, INPUT_PULLUP);
keypad.begin();
Serial.begin(115200);
}

void loop() {
keypad.digitalReadAll();
if (button1.press()) {
mode++;
Serial.println(modeStr[mode]);
digitalWrite(LED1, HIGH);
delay(100);
digitalWrite(LED1, LOW);
}
switch (mode) {
case 0:
if (button2.press()) {
Serial.println("button 2 press");
led2State = !led2State;
digitalWrite(LED2, led2State);
}
break;
case 1:
if (button2.repeat()) {
Serial.println("button 2 repeat");
led2State = !led2State;
digitalWrite(LED2, led2State);
}
break;
case 2:
if (button2.longPress()) {
Serial.println("button 2 long press");
led2State = !led2State;
digitalWrite(LED2, led2State);
}
break;
case 3:
if (button2.release()) {
Serial.println("button 2 release");
led2State = !led2State;
digitalWrite(LED2, led2State);
}
break;
case 4:
if (button2.hold()) {
Serial.println("button 2 hold");
led2State = true;
} else {
led2State = false;
}
digitalWrite(LED2, led2State);
break;
default:
mode = 0;
break;
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=debounced button
version=1.3.4
version=1.4.1
author=serek4
maintainer=serek4
sentence=buttons library with debounce
Expand Down
35 changes: 31 additions & 4 deletions src/debouncedButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,46 @@
#include "debouncedButton.h"

button::button(int pin, DEBOUNCERANGE debounceTimer)
: _pin(pin)
: _customButton(false)
, _pinStatus(nullptr)
, _pin(pin)
, _debounceTimer(debounceTimer)
, _active(LOW) {
pinMode(pin, INPUT_PULLUP);
}

button::button(int pin, bool active, DEBOUNCERANGE debounceTimer)
: _pin(pin)
: _customButton(false)
, _pinStatus(nullptr)
, _pin(pin)
, _debounceTimer(debounceTimer)
, _active(active) {
pinMode(pin, INPUT);
}

button::button(bool customButton, uint8_t &pinStatus, DEBOUNCERANGE debounceTimer)
: _customButton(customButton)
, _pinStatus(&pinStatus)
, _debounceTimer(debounceTimer)
, _active(LOW) {
}

button::button(bool customButton, uint8_t &pinStatus, bool active, DEBOUNCERANGE debounceTimer)
: _customButton(customButton)
, _pinStatus(&pinStatus)
, _debounceTimer(debounceTimer)
, _active(active) {
}

bool button::_readButtonStatus() {
if (digitalRead(_pin) == _active ? HIGH : LOW) {
return true;
if (_customButton) {
if (*_pinStatus == _active ? HIGH : LOW) {
return true;
}
} else {
if (digitalRead(_pin) == _active ? HIGH : LOW) {
return true;
}
}
return false;
}
Expand Down Expand Up @@ -108,6 +132,9 @@ bool button::longPress(int pressDelay) {
#endif
} else if (_buttonPressed && !_readButtonStatus()) { // debounce
_buttonPressed++;
if (!_buttonPressed) {
_longPressLock = false;
}
}
return _press;
}
Expand Down
6 changes: 6 additions & 0 deletions src/debouncedButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* @param pin pin number
* @param customButton flag for button not on uC pin (e.g. PCF8574)
* @param pinStatus reference to custom button state
* @param active active state, LOW or HIGH
* @param debounceTimer pseudo timer for debouncing,
* higher number shorter debounce time, default 1,
Expand All @@ -23,6 +25,8 @@ class button {
public:
button(int pin, DEBOUNCERANGE debounceTimer = 1);
button(int pin, bool active, DEBOUNCERANGE debounceTimer = 1);
button(bool customButton, uint8_t &pinStatus, DEBOUNCERANGE debounceTimer = 1);
button(bool customButton, uint8_t &pinStatus, bool active, DEBOUNCERANGE debounceTimer = 1);
bool press();
bool repeat(int repeatSpeed1 = 400, int repeatSpeed2 = 100, int repeatSpeed2delay = 1500);
bool longPress(int pressDelay = 2000);
Expand All @@ -41,5 +45,7 @@ class button {
#if DEBUG
unsigned long _releaseTime = 0;
#endif
bool _customButton;
uint8_t *_pinStatus;
bool _readButtonStatus();
};

0 comments on commit 0390c73

Please sign in to comment.