Skip to content

Commit c8c4a80

Browse files
committed
Latching Button update, added AlwaysOffButton
Latching Button updated so it returns the state if long press reset is disabled. Added AlwaysOffButton.
1 parent 062bc6f commit c8c4a80

7 files changed

Lines changed: 119 additions & 33 deletions

File tree

src/AlwaysOffButton.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2019 Lance A. Endres
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#include "AlwaysOffButton.h"
25+
26+
// Constructors.
27+
AlwaysOffButton::AlwaysOffButton(int pin) :
28+
SimpleButton(pin)
29+
{
30+
}
31+
32+
AlwaysOffButton::AlwaysOffButton(int pin, int debounceInterval) :
33+
SimpleButton(pin, debounceInterval)
34+
{
35+
}
36+
37+
// Destructor.
38+
AlwaysOffButton::~AlwaysOffButton()
39+
{
40+
}
41+
42+
BUTTONSTATUS AlwaysOffButton::getStatus()
43+
{
44+
return NOTPRESSED;
45+
}

src/AlwaysOffButton.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2019 Lance A. Endres
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
/*
25+
This button always returns NOTPRESSED. See SimpleButton.h for more information.
26+
*/
27+
28+
#ifndef ALWAYSOFFBUTTON_H
29+
#define ALWAYSOFFBUTTON_H
30+
31+
#include "Arduino.h"
32+
#include "SimpleButton.h"
33+
34+
class AlwaysOffButton : public SimpleButton
35+
{
36+
public:
37+
AlwaysOffButton(int pin);
38+
AlwaysOffButton(int pin, int debounceInterval);
39+
~AlwaysOffButton();
40+
41+
public:
42+
// Returns true if the button is currently pressed.
43+
BUTTONSTATUS getStatus();
44+
};
45+
46+
#endif

src/AlwaysOnButton.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
*/
2323

2424
/*
25-
This button always returns true. The purpose of this class is to provide automatic
26-
implimentation of an always on state for libraries or classes the take TwoStateButtons
27-
as an argument. By using a TwoStateButton, as class can automatically implement
28-
momentary, latching, or always on behavior depending on which class derived from
29-
the TwoStateButton class is used. This makes it easy to switch behaviors as needed.
25+
This button always returns ISPRESSED. See SimpleButton.h for more information.
3026
*/
3127

3228
#ifndef ALWAYSONBUTTON_H

src/ButtonEnums.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,7 @@
2222
*/
2323

2424
/*
25-
This is a base class used for adding behavior to push buttons (momentary buttons).
26-
It uses a debouncer to prevent accidental double readings. It separates the button
27-
behavior into currently pressed, short press, long press, or idle (nothing
28-
happening).
29-
30-
This class also acts as a base class for other button related classes.
31-
32-
The work of debouncing and catching the state changes (LOW to HIGH or HIGH to LOW)
33-
is done by the Bounce2 library.
34-
35-
See also: PushButton, ToggleButton, PushButtonCounter, CycleButton
36-
*/
37-
38-
/*
39-
To use a button with this library, the button should be wired with one side
40-
connected to the Arduino pin and the other side connected to ground.
41-
*/
42-
43-
/*
44-
If you want debugging messages printed to the serial monitor, enable the serial monitor and
45-
use the following line in your file:
46-
47-
#define BUTTONSUITEDEBUG
25+
This file contains enumerations used by the ButtonSuite.
4826
*/
4927

5028
#ifndef BUTTONENUMS_H

src/LatchingButton.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ BUTTONSTATUS LatchingButton::getStatus()
7272
{
7373
reset();
7474
}
75+
else
76+
{
77+
_latched = !_latched;
78+
}
7579
return convertStateToButtonStatus();
7680

7781
case ISPRESSED:

src/LatchingButton.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,24 @@
2323

2424
/*
2525
Turns a push button (momentary button) into a toggle button (latching button).
26-
Pressing the button alternates between on (ISPRESSED) and off (NOTPRESSED). If the
27-
button is pushed once to turn in on (ISPRESSED) it will continue to return on
28-
(ISPRESSED) until the button is pushed again. This effectively creates a
29-
virtual latching switch controled by a momentary button. The toggle button
30-
can be reset to the base (known) state by the user (with a long press, if
26+
Pressing the button alternates between on (ISPRESSED) and off (NOTPRESSED).
27+
This effectively creates a virtual latching switch controled by a momentary button.
28+
The button can be reset to the base (known) state by the user (with a long press, if
3129
enabled) or programmically.
30+
31+
After a button is press down, the first call to getStatus returns WASPRESSED.
32+
This allows a derived class to act on the button down press, if required. Further
33+
calls to getStatus will then return either ISPRESSED or NOTPRESSED depending
34+
on the current value of _latched.
35+
36+
When the button is released, the length of the button press is evaluated. The behavior
37+
is then:
38+
Short Press - The value of _latched is switched and the new state returned (as ISPRESSED
39+
OR NOT PRESSED).
40+
41+
Long Press - If reset is enabled, the value of _latched is set to the default state
42+
and the new state returned. If reset is disabled, the behavior is the same as
43+
as short press.
3244
3345
This button only returns the states, WASPRESSED, ISPRESSED or NOTPRESSED. A short
3446
press and a long press are used to either toggle the state or reset the state,

src/SimpleButton.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
/*
2525
Abstract class that provides interface for a two state button (button that only has
2626
an on and off state).
27+
28+
The purpose of this class is to provide automatic implimentation of different behaviors.
29+
By using different classes derived from SimpleButton, a class can automatically implement
30+
momentary, latching, always on, or always off behavior. This makes it easy to implement
31+
different behaviors or even switch behaviors during run time.
2732
*/
2833

2934
#ifndef TWOSTATEBUTTON_H

0 commit comments

Comments
 (0)