Skip to content

Commit

Permalink
Fixing examples for ESP8266
Browse files Browse the repository at this point in the history
  • Loading branch information
mathertel committed May 8, 2023
1 parent 1280bd0 commit c08ce8f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/arduino-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
# Install ESP8266 platform via Boards Manager
- name: esp8266:esp8266
source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json
version: 3.0.0
version: 3.1.2
fqbn: esp8266:esp8266:nodemcuv2
sketch-paths: |
- 'examples/SimpleOneButton'
Expand Down
26 changes: 13 additions & 13 deletions examples/SpecialInput/SpecialInput.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
* In the loop function the button.tick function must be called as often as you like.
*
* * 22.01.2021 created by Matthias Hertel
*/
* * 07.02.2023 ESP32 Support added.
*/

#include "Arduino.h"
#include "OneButton.h"

// This is an example on how to use the OneClick library on other input sources than standard digital pins.
Expand All @@ -40,46 +42,44 @@
// OneButton instance will be created in setup.
OneButton *button;

void fClicked(void *s)
{
void fClicked(void *s) {
Serial.print("Click:");
Serial.println((char *)s);
}

static void fDoubleClicked(void *oneButton)
{
static void fDoubleClicked(void *oneButton) {
OneButton *button = (OneButton *)oneButton;
Serial.print("pin=");
Serial.print(button->pin());
Serial.print(" state=");
Serial.println(button->state());
}

void setup()
{
void setup() {
Serial.begin(115200);
Serial.println("One Button Example with custom input.");

// create the OneButton instance without a pin.
// create the OneButton instance without a pin.
button = new OneButton();

// Here is an example on how to use a parameter to the registered functions:
button->attachClick(fClicked, (void *)"me");
button->attachDoubleClick(fDoubleClicked, &button);

// Here is an example on how to use an inline function:
button->attachDoubleClick([]() { Serial.println("DoubleClick"); });
button->attachDoubleClick([]() {
Serial.println("DoubleClick");
});

// setup your own source of input:
pinMode(PIN_INPUT, INPUT_PULLUP);

} // setup()
} // setup()

void loop()
{
void loop() {
// read your own source of input:
bool isPressed = (digitalRead(PIN_INPUT) == LOW);

// call tick frequently with current push-state of the input
button->tick(isPressed);
} // loop()
} // loop()
59 changes: 36 additions & 23 deletions examples/TwoButtons/TwoButtons.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
This is a sample sketch to show how to use the OneButtonLibrary
to detect click events on 2 buttons in parallel.
to detect click events on 2 buttons in parallel.
The library internals are explained at
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Setup a test circuit:
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
* Connect a pushbutton to pin A2 (ButtonPin) and ground.
* The Serial interface is used for output the detected button events.
The Sketch shows how to setup the library and bind 2 buttons to their functions.
In the loop function the button1.tick and button2.tick functions have to be called as often as you like.
*/
Expand Down Expand Up @@ -37,18 +37,32 @@ Button 2 longPress stop

#include "OneButton.h"

// Setup a new OneButton on pin A1.
OneButton button1(A1, true);
// Setup a new OneButton on pin A2.
OneButton button2(A2, true);
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
#define PIN_INPUT1 A1
#define PIN_INPUT2 A2

#elif defined(ESP8266)
#define PIN_INPUT1 D3
#define PIN_INPUT2 D4

#elif defined(ESP32)
#define PIN_INPUT1 32
#define PIN_INPUT2 33

#endif

// Setup a new OneButton on pin PIN_INPUT2.
OneButton button1(PIN_INPUT1, true);
// Setup a new OneButton on pin PIN_INPUT2.
OneButton button2(PIN_INPUT2, true);


// setup code here, to run once:
void setup() {
// Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting TwoButtons...");

Expand All @@ -66,77 +80,76 @@ void setup() {
button2.attachLongPressStop(longPressStop2);
button2.attachDuringLongPress(longPress2);

} // setup
} // setup


// main code here, to run repeatedly:
// main code here, to run repeatedly:
void loop() {
// keep watching the push buttons:
button1.tick();
button2.tick();

// You can implement other code in here or just wait a while
// You can implement other code in here or just wait a while
delay(10);
} // loop
} // loop


// ----- button 1 callback functions

// This function will be called when the button1 was pressed 1 time (and no 2. button press followed).
void click1() {
Serial.println("Button 1 click.");
} // click1
} // click1


// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
Serial.println("Button 1 doubleclick.");
} // doubleclick1
} // doubleclick1


// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
Serial.println("Button 1 longPress start");
} // longPressStart1
} // longPressStart1


// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
Serial.println("Button 1 longPress...");
} // longPress1
} // longPress1


// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
Serial.println("Button 1 longPress stop");
} // longPressStop1
} // longPressStop1


// ... and the same for button 2:

void click2() {
Serial.println("Button 2 click.");
} // click2
} // click2


void doubleclick2() {
Serial.println("Button 2 doubleclick.");
} // doubleclick2
} // doubleclick2


void longPressStart2() {
Serial.println("Button 2 longPress start");
} // longPressStart2
} // longPressStart2


void longPress2() {
Serial.println("Button 2 longPress...");
} // longPress2
} // longPress2

void longPressStop2() {
Serial.println("Button 2 longPress stop");
} // longPressStop2
} // longPressStop2


// End

0 comments on commit c08ce8f

Please sign in to comment.