diff --git a/EButton.cpp b/EButton.cpp index 372e1d6..b59c670 100644 --- a/EButton.cpp +++ b/EButton.cpp @@ -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(); } @@ -77,6 +78,10 @@ byte EButton::getPin() { return pin; } +byte EButton::getID() { + return id; +} + byte EButton::getClicks() { return clicks; } diff --git a/EButton.h b/EButton.h index 8e83b4f..fdd4977 100644 --- a/EButton.h +++ b/EButton.h @@ -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); @@ -153,6 +153,9 @@ class EButton { // Attached pin number byte getPin(); + // Button ID + byte getID(); + // Number of clicks performed byte getClicks(); @@ -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 diff --git a/examples/SharedCallback/SharedCallback.ino b/examples/SharedCallback/SharedCallback.ino new file mode 100644 index 0000000..5aa6aed --- /dev/null +++ b/examples/SharedCallback/SharedCallback.ino @@ -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(); + } +}