Skip to content

Commit 27da5fd

Browse files
authored
Merge pull request #135 from mathertel/V2.5.0
This release is a minor update including som smaller fixes. Functions marked with deprecated will be removed in version 3.x Formatting of source code conformint the standard Arduino IDE 2.0 formatting using .clang-format Version for platform.io in sync with version for Arduino Introducing the OneButtonTiny class for small environments with limited program space and memory.
2 parents 0ed3b51 + a93a729 commit 27da5fd

9 files changed

+499
-34
lines changed

.clang-format

+1-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ BreakBeforeTernaryOperators: true
5757
BreakConstructorInitializers: BeforeColon
5858
BreakInheritanceList: BeforeColon
5959
BreakStringLiterals: false
60-
# v12 has various problems with this set to 0 (no limit)
61-
# possible workaround is to set this to 4294967295 aka some large number
62-
# see https://reviews.llvm.org/D96896
6360
ColumnLimit: 0
6461
# "" matches none
6562
CommentPragmas: ""
@@ -117,7 +114,7 @@ PenaltyReturnTypeOnItsOwnLine: 1
117114
# Used as a fallback if alignment style can't be detected from code (DerivePointerAlignment: true)
118115
PointerAlignment: Right
119116
RawStringFormats: []
120-
ReflowComments: true
117+
ReflowComments: false
121118
SortIncludes: false
122119
SortUsingDeclarations: false
123120
SpaceAfterCStyleCast: false

.markdownlint.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"default": true,
3+
"no-hard-tabs": true,
4+
"no-trailing-spaces": true,
5+
"blank_lines": true,
6+
"no-multiple-blanks": {
7+
"maximum": 2
8+
},
9+
"ul-indent": {
10+
"indent": 2
11+
},
12+
"ul-style": {
13+
"style": "asterisk"
14+
},
15+
"line-length": false,
16+
"no-inline-html": false,
17+
"code-block-style": {
18+
"style": "fenced"
19+
},
20+
"no-duplicate-header": {
21+
"allow_different_nesting": true
22+
}
23+
}

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable changes to this project will be documented in this file starting 2021.
44

5+
## [2.5.0] - 2023-12-02
6+
7+
This release is a minor update including som smaller fixes.
8+
9+
* Functions marked with deprecated will be removed in version 3.x
10+
* Formatting of source code conformint the standard Arduino IDE 2.0 formatting using .clang-format
11+
* Version for platform.io in sync with version for Arduino
12+
* Introducing the `OneButtonTiny` class for small environments with limited program space and memory.
13+
14+
515
## [2.1.0] - 2023-05-10
616

717
This release is a minor update as there is new internal functionality and

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ You can find more details on this library at
1212

1313
The change log of this library can be found in [CHANGELOG](CHANGELOG.md).
1414

15+
1516
## Getting Started
1617

1718
Clone this repository into `Arduino/Libraries` or use the built-in Arduino IDE Library manager to install
@@ -25,6 +26,30 @@ a copy of this library. You can find more detail about installing libraries
2526

2627
Each physical button requires its own `OneButton` instance. You can initialize them like this:
2728

29+
30+
### OneButton Tiny version
31+
32+
The OneButton Library was extended over time with functionality that was requested for specific
33+
use cases. This makes the library growing over time too and therefore was limiting use cases using very small processors like attiny84.
34+
35+
Staring with version 2.5 the OneButton Library starts supporting these processors with limited
36+
memory and low cpu frequencies by introducing the `OneButtonTiny` class that offers a subset of
37+
the features of the complete `OneButton` class by exposing the following events as callbacks:
38+
39+
* Click event
40+
* DoubleClick event
41+
* LongPressStart event
42+
* Callbacks without parameters
43+
44+
This saves up to 1k of binary program space that is a huge amount on these processors.
45+
46+
With Version 2.5 the `OneButtonTiny` class is now in a beta state.
47+
48+
* Any Issues or pull requests fixing problems are welcome.
49+
* Any new feature request for the `OneButtonTiny` class will be rejected to keep size small.
50+
* New, reasonable functionality will be added to the OneButton class only.
51+
52+
2853
### Initialize a Button to GND
2954

3055
```CPP
@@ -43,6 +68,7 @@ OneButton btn = OneButton(
4368
);
4469
```
4570

71+
4672
### Initialize a Button to VCC
4773

4874
```CPP
@@ -62,6 +88,7 @@ OneButton btn = OneButton(
6288
);
6389
```
6490

91+
6592
### Attach State Events
6693

6794
Once you have your button initialized, you can handle events by attaching them to the button
@@ -103,6 +130,7 @@ void loop() {
103130
}
104131
```
105132

133+
106134
### Usage with lambdas that capture context
107135

108136
You __can't pass__ a lambda-__with-context__ to an argument which expects a __function pointer__. To work that around,
@@ -115,6 +143,7 @@ okBtn.attachClick([](void *ctx){Serial.println(*((BtnHandler*)ctx) -> state);},
115143
116144
See also discussion in [Issue #112](https://github.com/mathertel/OneButton/issues/112).
117145
146+
118147
## State Events
119148
120149
Here's a full list of events handled by this library:
@@ -128,6 +157,7 @@ Here's a full list of events handled by this library:
128157
| `attachDuringLongPress` | Fires periodically as long as the button is held down. |
129158
| `attachLongPressStop` | Fires when the button is released after a long hold. |
130159
160+
131161
### Event Timing
132162
133163
Valid events occur when `tick()` is called after a specified number of milliseconds. You can use
@@ -151,6 +181,7 @@ The former functions `setDebounceTicks`, `setClickTicks` and `setPressTicks` are
151181
The term `Ticks` in these functions where confusing. Replace them with the ...Ms function calls.
152182
There is no functional change on them.
153183
184+
154185
### Additional Functions
155186
156187
`OneButton` also provides a couple additional functions to use for querying button status:
@@ -163,11 +194,13 @@ There is no functional change on them.
163194
| `int state()` | Get the OneButton state |
164195
| `int debouncedValue()` | Get the OneButton debounced value |
165196
197+
166198
### `tick()` and `reset()`
167199
168200
You can specify a logic level when calling `tick(bool)`, which will skip reading the pin and use
169201
that level instead. If you wish to reset the internal state of your buttons, call `reset()`.
170202
203+
171204
## Troubleshooting
172205
173206
If your buttons aren't acting they way they should, check these items:

examples/BlinkMachine/BlinkMachine.ino

+37-28
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
-------- ------ |
2929
| OFF |<--click-+->| ON | |
3030
-------- | ------ |
31-
| | |
32-
| d-click |
33-
| | |
31+
^ | | |
32+
| | d-click |
33+
longpress | | |
3434
| V |
3535
| ------ |
3636
+- | SLOW | |
@@ -47,24 +47,24 @@
4747
// 06.10.2012 created by Matthias Hertel
4848
// 26.03.2017 state diagram added, minor changes
4949

50-
#include "OneButton.h"
50+
// #include "OneButton.h"
51+
#include "OneButtonTiny.h" // This example also works with reduced OneButtonTiny class saving.
5152

5253
// The actions I ca do...
5354
typedef enum {
54-
ACTION_OFF, // set LED "OFF".
55-
ACTION_ON, // set LED "ON"
56-
ACTION_SLOW, // blink LED "SLOW"
57-
ACTION_FAST // blink LED "FAST"
58-
}
59-
MyActions;
55+
ACTION_OFF, // set LED "OFF".
56+
ACTION_ON, // set LED "ON"
57+
ACTION_SLOW, // blink LED "SLOW"
58+
ACTION_FAST // blink LED "FAST"
59+
} MyActions;
6060

6161
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY) ||defined(ARDUINO_UNOR4_WIFI)
6262
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
6363
#define PIN_INPUT A1
6464
#define PIN_LED 13
6565

6666
#elif defined(ARDUINO_attiny)
67-
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
67+
// Example for Arduino ATTiny85
6868
#define PIN_INPUT PB0
6969
#define PIN_LED PB1
7070

@@ -83,36 +83,40 @@ MyActions;
8383

8484
#endif
8585

86-
// Setup a new OneButton on pin PIN_INPUT.
87-
OneButton button(PIN_INPUT, true);
86+
// Setup a new OneButton on pin PIN_INPUT.
87+
// OneButton button(PIN_INPUT, true);
88+
OneButtonTiny button(PIN_INPUT, true); // This example also works with reduced OneButtonTiny class saving.
8889

89-
MyActions nextAction = ACTION_OFF; // no action when starting
90+
MyActions nextAction = ACTION_OFF; // no action when starting
9091

9192

9293
// setup code here, to run once.
9394
void setup() {
9495
// enable the standard led on pin 13.
95-
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
96+
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
9697

97-
// link the myClickFunction function to be called on a click event.
98+
// link the myClickFunction function to be called on a click event.
9899
button.attachClick(myClickFunction);
99100

100-
// link the doubleclick function to be called on a doubleclick event.
101+
// link the doubleclick function to be called on a doubleclick event.
101102
button.attachDoubleClick(myDoubleClickFunction);
102103

104+
// link the doubleclick function to be called on a doubleclick event.
105+
button.attachLongPressStart(myDoubleClickFunction);
106+
103107
// set 80 msec. debouncing time. Default is 50 msec.
104108
button.setDebounceMs(80);
105-
} // setup
109+
} // setup
106110

107111

108-
// main code here, to run repeatedly:
112+
// main code here, to run repeatedly:
109113
void loop() {
110114
unsigned long now = millis();
111115

112116
// keep watching the push button:
113117
button.tick();
114118

115-
// You can implement other code in here or just wait a while
119+
// You can implement other code in here or just wait a while
116120

117121
if (nextAction == ACTION_OFF) {
118122
// do nothing.
@@ -128,17 +132,17 @@ void loop() {
128132
digitalWrite(PIN_LED, LOW);
129133
} else {
130134
digitalWrite(PIN_LED, HIGH);
131-
} // if
135+
} // if
132136

133137
} else if (nextAction == ACTION_FAST) {
134138
// do a fast blinking
135139
if (now % 200 < 100) {
136140
digitalWrite(PIN_LED, LOW);
137141
} else {
138142
digitalWrite(PIN_LED, HIGH);
139-
} // if
140-
} // if
141-
} // loop
143+
} // if
144+
} // if
145+
} // loop
142146

143147

144148
// this function will be called when the button was pressed 1 time and them some time has passed.
@@ -147,7 +151,7 @@ void myClickFunction() {
147151
nextAction = ACTION_ON;
148152
else
149153
nextAction = ACTION_OFF;
150-
} // myClickFunction
154+
} // myClickFunction
151155

152156

153157
// this function will be called when the button was pressed 2 times in a short timeframe.
@@ -160,8 +164,13 @@ void myDoubleClickFunction() {
160164

161165
} else if (nextAction == ACTION_FAST) {
162166
nextAction = ACTION_ON;
163-
} // if
164-
} // myDoubleClickFunction
167+
} // if
168+
} // myDoubleClickFunction
165169

166-
// End
167170

171+
// this function will be called when a long press was detected.
172+
void myLongPressFunction() {
173+
nextAction = ACTION_OFF;
174+
} // myLongPressFunction
175+
176+
// End

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "OneButton",
3-
"version": "2.1.0",
3+
"version": "2.5.0",
44
"keywords": "arduino, button, pushbutton",
55
"description": "This Arduino library is improving the usage of a singe button for input. It shows how to use an digital input pin with a single pushbutton attached for detecting some of the typical button press events like single clicks, double clicks and long-time pressing. This enables you to reuse the same button for multiple functions and lowers the hardware invests.",
66
"repository": {

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=OneButton
2-
version=2.1.0
2+
version=2.5.0
33
author=Matthias Hertel
44
maintainer=Matthias Hertel, https://www.mathertel.de
55
sentence=Arduino library for improving the usage of a singe input button.

0 commit comments

Comments
 (0)