Skip to content

Commit

Permalink
ESP32 defines in examples and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mathertel committed Jan 21, 2022
1 parent 3bcba94 commit 1897861
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## [2.0.4] - 2022-01-22

* checked for ESP32 (SimpleOneButton, InterruptOneButton, BlinkMachine)
and included example PIN definitions for ESP32
* Documentation changes

## [2.0.3] - 2021-10-26

* fixing parameter missuse and potential crash
Expand Down
9 changes: 8 additions & 1 deletion examples/BlinkMachine/BlinkMachine.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,16 @@ MyActions;
#define PIN_INPUT D3
#define PIN_LED D4

#elif defined(ESP32)
// Example pin assignments for a ESP32 board
// Some boards have a BOOT switch using GPIO 0.
#define PIN_INPUT 0
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
#define PIN_LED 25

#endif

// Setup a new OneButton on pin A1.
// Setup a new OneButton on pin PIN_INPUT.
OneButton button(PIN_INPUT, true);

MyActions nextAction = ACTION_OFF; // no action when starting
Expand Down
48 changes: 48 additions & 0 deletions examples/FunctionalButton/FunctionalButton.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* FunctionalButton.ino - Example for the OneButtonLibrary library.
* This is a sample sketch to show how to use OneClick library functionally on ESP32,ESP8266...
*
*/
#include <Arduino.h>
#include <OneButton.h>

class Button{
private:
OneButton button;
int value;
public:
explicit Button(uint8_t pin):button(pin) {
button.attachClick([](void *scope) { ((Button *) scope)->Clicked();}, this);
button.attachDoubleClick([](void *scope) { ((Button *) scope)->DoubleClicked();}, this);
button.attachLongPressStart([](void *scope) { ((Button *) scope)->LongPressed();}, this);
}

void Clicked(){
Serial.println("Click then value++");
value++;
}

void DoubleClicked(){

Serial.println("DoubleClick");
}

void LongPressed(){
Serial.println("LongPress and the value is");
Serial.println(value);
}

void handle(){
button.tick();
}
};

Button button(0);

void setup() {
Serial.begin(115200);
}

void loop() {
button.handle();
}
61 changes: 36 additions & 25 deletions examples/InterruptOneButton/InterruptOneButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
... and the processor datasheet.
Setup a test circuit:
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
* The pin 13 (StatusPin) is used for output attach a led and resistor to ground
or see the built-in led on the standard arduino board.
* Connect a pushbutton to the PIN_INPUT (see defines for processor specific examples) and ground.
* The PIN_LED (see defines for processor specific examples) is used for output attach a led and resistor to VCC.
or maybe use a built-in led on the standard arduino board.
The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
In the loop function the button.tick function must be called as often as you like.
Expand All @@ -39,6 +39,13 @@
#define PIN_INPUT D3
#define PIN_LED D4

#elif defined(ESP32)
// Example pin assignments for a ESP32 board
// Some boards have a BOOT switch using GPIO 0.
#define PIN_INPUT 0
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
#define PIN_LED 25

#endif

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

// This function is called from the interrupt when the signal on the PIN_INPUT has changed.
// do not use Serial in here.
#if defined(ARDUINO_AVR_UNO) || defined (ARDUINO_AVR_NANO_EVERY)
void checkTicks()
{
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
void checkTicks() {
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}

#elif defined(ESP8266)
ICACHE_RAM_ATTR void checkTicks()
{
ICACHE_RAM_ATTR void checkTicks() {
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}

#elif defined(ESP32)
void IRAM_ATTR checkTicks() {
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}
Expand All @@ -77,15 +88,13 @@ ICACHE_RAM_ATTR void checkTicks()


// this function will be called when the button was pressed 1 time only.
void singleClick()
{
void singleClick() {
Serial.println("singleClick() detected.");
} // singleClick


// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleClick()
{
void doubleClick() {
Serial.println("doubleClick() detected.");

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


// this function will be called when the button was pressed multiple times in a short timeframe.
void multiClick()
{
Serial.print("multiClick(");
Serial.print(button.getNumberClicks());
Serial.println(") detected.");
void multiClick() {
int n = button.getNumberClicks();
if (n == 3) {
Serial.println("tripleClick detected.");
} else if (n == 4) {
Serial.println("quadrupleClick detected.");
} else {
Serial.print("multiClick(");
Serial.print(n);
Serial.println(") detected.");
}

ledState = !ledState; // reverse the LED
digitalWrite(PIN_LED, ledState);
} // multiClick


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


// this function will be called when the button was released after a long hold.
void pressStop()
{
void pressStop() {
Serial.print("pressStop(");
Serial.print(millis() - pressStartTime);
Serial.println(") detected.");
} // pressStop()


// setup code here, to run once:
void setup()
{
void setup() {
Serial.begin(115200);
Serial.println("One Button Example with interrupts.");

Expand Down Expand Up @@ -164,8 +176,7 @@ void setup()


// main code here, to run repeatedly:
void loop()
{
void loop() {
// keep watching the push button, even when no interrupt happens:
button.tick();

Expand Down
9 changes: 8 additions & 1 deletion examples/SimpleOneButton/SimpleOneButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@
#define PIN_INPUT 2
#define PIN_LED 13

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

#elif defined(ESP32)
// Example pin assignments for a ESP32 board
// Some boards have a BOOT switch using GPIO 0.
#define PIN_INPUT 0
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
#define PIN_LED 25

#endif

// Setup a new OneButton on pin PIN_INPUT
Expand Down

0 comments on commit 1897861

Please sign in to comment.