Skip to content

Commit 1897861

Browse files
committed
ESP32 defines in examples and documentation
1 parent 3bcba94 commit 1897861

File tree

5 files changed

+106
-27
lines changed

5 files changed

+106
-27
lines changed

CHANGELOG.md

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

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

5+
## [2.0.4] - 2022-01-22
6+
7+
* checked for ESP32 (SimpleOneButton, InterruptOneButton, BlinkMachine)
8+
and included example PIN definitions for ESP32
9+
* Documentation changes
10+
511
## [2.0.3] - 2021-10-26
612

713
* fixing parameter missuse and potential crash

examples/BlinkMachine/BlinkMachine.ino

+8-1
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,16 @@ MyActions;
6969
#define PIN_INPUT D3
7070
#define PIN_LED D4
7171

72+
#elif defined(ESP32)
73+
// Example pin assignments for a ESP32 board
74+
// Some boards have a BOOT switch using GPIO 0.
75+
#define PIN_INPUT 0
76+
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
77+
#define PIN_LED 25
78+
7279
#endif
7380

74-
// Setup a new OneButton on pin A1.
81+
// Setup a new OneButton on pin PIN_INPUT.
7582
OneButton button(PIN_INPUT, true);
7683

7784
MyActions nextAction = ACTION_OFF; // no action when starting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* FunctionalButton.ino - Example for the OneButtonLibrary library.
3+
* This is a sample sketch to show how to use OneClick library functionally on ESP32,ESP8266...
4+
*
5+
*/
6+
#include <Arduino.h>
7+
#include <OneButton.h>
8+
9+
class Button{
10+
private:
11+
OneButton button;
12+
int value;
13+
public:
14+
explicit Button(uint8_t pin):button(pin) {
15+
button.attachClick([](void *scope) { ((Button *) scope)->Clicked();}, this);
16+
button.attachDoubleClick([](void *scope) { ((Button *) scope)->DoubleClicked();}, this);
17+
button.attachLongPressStart([](void *scope) { ((Button *) scope)->LongPressed();}, this);
18+
}
19+
20+
void Clicked(){
21+
Serial.println("Click then value++");
22+
value++;
23+
}
24+
25+
void DoubleClicked(){
26+
27+
Serial.println("DoubleClick");
28+
}
29+
30+
void LongPressed(){
31+
Serial.println("LongPress and the value is");
32+
Serial.println(value);
33+
}
34+
35+
void handle(){
36+
button.tick();
37+
}
38+
};
39+
40+
Button button(0);
41+
42+
void setup() {
43+
Serial.begin(115200);
44+
}
45+
46+
void loop() {
47+
button.handle();
48+
}

examples/InterruptOneButton/InterruptOneButton.ino

+36-25
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
... and the processor datasheet.
1111
1212
Setup a test circuit:
13-
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
14-
* The pin 13 (StatusPin) is used for output attach a led and resistor to ground
15-
or see the built-in led on the standard arduino board.
13+
* Connect a pushbutton to the PIN_INPUT (see defines for processor specific examples) and ground.
14+
* The PIN_LED (see defines for processor specific examples) is used for output attach a led and resistor to VCC.
15+
or maybe use a built-in led on the standard arduino board.
1616
1717
The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
1818
In the loop function the button.tick function must be called as often as you like.
@@ -39,6 +39,13 @@
3939
#define PIN_INPUT D3
4040
#define PIN_LED D4
4141

42+
#elif defined(ESP32)
43+
// Example pin assignments for a ESP32 board
44+
// Some boards have a BOOT switch using GPIO 0.
45+
#define PIN_INPUT 0
46+
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
47+
#define PIN_LED 25
48+
4249
#endif
4350

4451
// Setup a new OneButton on pin PIN_INPUT
@@ -59,16 +66,20 @@ unsigned long pressStartTime;
5966

6067
// This function is called from the interrupt when the signal on the PIN_INPUT has changed.
6168
// do not use Serial in here.
62-
#if defined(ARDUINO_AVR_UNO) || defined (ARDUINO_AVR_NANO_EVERY)
63-
void checkTicks()
64-
{
69+
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
70+
void checkTicks() {
6571
// include all buttons here to be checked
6672
button.tick(); // just call tick() to check the state.
6773
}
6874

6975
#elif defined(ESP8266)
70-
ICACHE_RAM_ATTR void checkTicks()
71-
{
76+
ICACHE_RAM_ATTR void checkTicks() {
77+
// include all buttons here to be checked
78+
button.tick(); // just call tick() to check the state.
79+
}
80+
81+
#elif defined(ESP32)
82+
void IRAM_ATTR checkTicks() {
7283
// include all buttons here to be checked
7384
button.tick(); // just call tick() to check the state.
7485
}
@@ -77,15 +88,13 @@ ICACHE_RAM_ATTR void checkTicks()
7788

7889

7990
// this function will be called when the button was pressed 1 time only.
80-
void singleClick()
81-
{
91+
void singleClick() {
8292
Serial.println("singleClick() detected.");
8393
} // singleClick
8494

8595

8696
// this function will be called when the button was pressed 2 times in a short timeframe.
87-
void doubleClick()
88-
{
97+
void doubleClick() {
8998
Serial.println("doubleClick() detected.");
9099

91100
ledState = !ledState; // reverse the LED
@@ -94,37 +103,40 @@ void doubleClick()
94103

95104

96105
// this function will be called when the button was pressed multiple times in a short timeframe.
97-
void multiClick()
98-
{
99-
Serial.print("multiClick(");
100-
Serial.print(button.getNumberClicks());
101-
Serial.println(") detected.");
106+
void multiClick() {
107+
int n = button.getNumberClicks();
108+
if (n == 3) {
109+
Serial.println("tripleClick detected.");
110+
} else if (n == 4) {
111+
Serial.println("quadrupleClick detected.");
112+
} else {
113+
Serial.print("multiClick(");
114+
Serial.print(n);
115+
Serial.println(") detected.");
116+
}
102117

103118
ledState = !ledState; // reverse the LED
104119
digitalWrite(PIN_LED, ledState);
105120
} // multiClick
106121

107122

108123
// this function will be called when the button was held down for 1 second or more.
109-
void pressStart()
110-
{
124+
void pressStart() {
111125
Serial.println("pressStart()");
112126
pressStartTime = millis() - 1000; // as set in setPressTicks()
113127
} // pressStart()
114128

115129

116130
// this function will be called when the button was released after a long hold.
117-
void pressStop()
118-
{
131+
void pressStop() {
119132
Serial.print("pressStop(");
120133
Serial.print(millis() - pressStartTime);
121134
Serial.println(") detected.");
122135
} // pressStop()
123136

124137

125138
// setup code here, to run once:
126-
void setup()
127-
{
139+
void setup() {
128140
Serial.begin(115200);
129141
Serial.println("One Button Example with interrupts.");
130142

@@ -164,8 +176,7 @@ void setup()
164176

165177

166178
// main code here, to run repeatedly:
167-
void loop()
168-
{
179+
void loop() {
169180
// keep watching the push button, even when no interrupt happens:
170181
button.tick();
171182

examples/SimpleOneButton/SimpleOneButton.ino

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@
2323
#define PIN_INPUT 2
2424
#define PIN_LED 13
2525

26-
#else if defined(ESP8266)
26+
#elif defined(ESP8266)
2727
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
2828
// This LED is lighting on output level LOW.
2929
#define PIN_INPUT D3
3030
#define PIN_LED D4
3131

32+
#elif defined(ESP32)
33+
// Example pin assignments for a ESP32 board
34+
// Some boards have a BOOT switch using GPIO 0.
35+
#define PIN_INPUT 0
36+
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
37+
#define PIN_LED 25
38+
3239
#endif
3340

3441
// Setup a new OneButton on pin PIN_INPUT

0 commit comments

Comments
 (0)