Skip to content

Commit 42becd9

Browse files
committed
Updated PushEventButton to allow for capturing both push and release.
Updated PushEventButton to allow for capturing both push and release.
1 parent 79a09a4 commit 42becd9

6 files changed

Lines changed: 67 additions & 4 deletions

File tree

documentation/Documentation.pptx

1.47 KB
Binary file not shown.
16.6 KB
Loading
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
This example demonstrates how the PushEventButton captures each individual
3+
button press as discrete events. It does not matter how long the button is
4+
pressed down or released. Only the transition between pressed and released
5+
is captured.
6+
7+
The button should be wired such that when pressed, the "buttonPin" is
8+
connected to ground.
9+
10+
The LED should be wired with the "ledPin" to the positive lead and the
11+
negative lead should be connected to ground. A current limiting resistor
12+
should be used.
13+
*/
14+
15+
#include "PushEventButton.h"
16+
17+
// Change these if your button or LED are on other pins.
18+
int buttonPin = 8;
19+
int ledPin = 9;
20+
21+
// The button will automatically configure the button pin.
22+
// Because the "CAPTUREBOTH" option is used, both the down push
23+
// and the release are captured and used to flash the LED.
24+
PushEventButton button(buttonPin, PushEventButton::CAPTUREBOTH);
25+
26+
void setup()
27+
{
28+
// Setup the output LED.
29+
pinMode(ledPin, OUTPUT);
30+
digitalWrite(ledPin, LOW);
31+
}
32+
33+
void loop()
34+
{
35+
// This will return true only once for each button push.
36+
// This example is triggered on the press of the button. As soon as the button is pressed down
37+
// this returns true. It does not matter how long the button is held down, the light flashes
38+
// for the same amount of time. Nothing happens on the button release.
39+
bool buttonPushed = button.pushed();
40+
41+
// Set the LED to the state of the button press.
42+
digitalWrite(ledPin, buttonPushed);
43+
44+
if (buttonPushed)
45+
{
46+
// If the button was pushed, the light will be turned on. We need a brief delay to make sure the
47+
// on state of the LED is long enough to be seen.
48+
delay(100);
49+
}
50+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ButtonSuite
2-
version=2.0.0
2+
version=2.1.0
33
author=Lance A. Endres <lendres@fifthrace.com>
44
maintainer=Lance A. Endres <lendres@fifthrace.com>
55
sentence=A library for using a simple mechanical push (momentary) button as a momentary button, a latching button, a counter, an enumerator, and more.

src/PushEventButton.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool PushEventButton::pushed()
5959
{
6060
case CAPTUREPUSH:
6161
{
62-
if (status == BUTTONSUITE::JUSTPRESSED)
62+
if (status & BUTTONSUITE::JUSTPRESSED)
6363
{
6464
return true;
6565
}
@@ -71,7 +71,19 @@ bool PushEventButton::pushed()
7171

7272
case CAPTURERELEASE:
7373
{
74-
if (status == BUTTONSUITE::BUTTONSTATUS::WASSHORTPRESSED || status == BUTTONSUITE::BUTTONSTATUS::WASLONGPRESSED)
74+
if (status & (BUTTONSUITE::WASSHORTPRESSED | BUTTONSUITE::WASLONGPRESSED))
75+
{
76+
return true;
77+
}
78+
else
79+
{
80+
return false;
81+
}
82+
}
83+
84+
case CAPTUREBOTH:
85+
{
86+
if (status & (BUTTONSUITE::JUSTPRESSED | BUTTONSUITE::WASSHORTPRESSED | BUTTONSUITE::WASLONGPRESSED))
7587
{
7688
return true;
7789
}

src/PushEventButton.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class PushEventButton : public TwoStateButton
5050
enum CAPTURETYPE
5151
{
5252
CAPTUREPUSH,
53-
CAPTURERELEASE
53+
CAPTURERELEASE,
54+
CAPTUREBOTH
5455
};
5556

5657
public:

0 commit comments

Comments
 (0)