Skip to content

Commit

Permalink
Merge pull request #2 from jfjlaros/master
Browse files Browse the repository at this point in the history
Added button IDs for shared callback functions.
  • Loading branch information
jonnieZG authored Feb 13, 2023
2 parents ce6b5bf + 96ae53f commit 298704d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion EButton.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "EButton.h"

EButton::EButton(byte pin, bool pressedLow) {
EButton::EButton(byte pin, bool pressedLow, byte id) {
this->pin = pin;
pinMode(pin, pressedLow ? INPUT_PULLUP : INPUT);
pressedState = !pressedLow;
this->id = id;
reset();
}

Expand Down Expand Up @@ -77,6 +78,10 @@ byte EButton::getPin() {
return pin;
}

byte EButton::getID() {
return id;
}

byte EButton::getClicks() {
return clicks;
}
Expand Down
6 changes: 5 additions & 1 deletion EButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ typedef void (*EButtonEventHandler)(EButton&);
class EButton {
public:
// Constructor.
EButton(byte pin, bool pressedLow = true);
EButton(byte pin, bool pressedLow = true, byte id = 0);

// Debounce time - delay after the first transition, before sampling the next state.
void setDebounceTime(byte time);
Expand Down Expand Up @@ -153,6 +153,9 @@ class EButton {
// Attached pin number
byte getPin();

// Button ID
byte getID();

// Number of clicks performed
byte getClicks();

Expand All @@ -178,6 +181,7 @@ class EButton {

// ----- Configuration-specific fields -----
byte pin; // Attached pin
byte id; // Button ID
byte debounceTime = EBUTTON_DEFAULT_DEBOUNCE; // Debounce time in ms (between 0 and 255)
#if defined(EBUTTON_SUPPORT_DONE_CLICKING) || defined(EBUTTON_SUPPORT_SINGLE_AND_DOUBLE_CLICKS)
unsigned int clickTime = EBUTTON_DEFAULT_CLICK; // Time the button has to be released in order to complete counting clicks
Expand Down
25 changes: 25 additions & 0 deletions examples/SharedCallback/SharedCallback.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "EButton.h"

EButton buttons[] = {
{6, true, 1}, {4, true, 2}, {2, true, 3},
{7, true, 4}, {5, true, 5}, {3, true, 6}};


void sharedClick(EButton& btn) {
Serial.println(btn.getID());
}


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

for (EButton& btn: buttons) {
btn.attachSingleClick(sharedClick);
}
}

void loop() {
for (EButton& btn: buttons) {
btn.tick();
}
}

0 comments on commit 298704d

Please sign in to comment.