Skip to content

Commit 298704d

Browse files
authored
Merge pull request #2 from jfjlaros/master
Added button IDs for shared callback functions.
2 parents ce6b5bf + 96ae53f commit 298704d

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

EButton.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "EButton.h"
22

3-
EButton::EButton(byte pin, bool pressedLow) {
3+
EButton::EButton(byte pin, bool pressedLow, byte id) {
44
this->pin = pin;
55
pinMode(pin, pressedLow ? INPUT_PULLUP : INPUT);
66
pressedState = !pressedLow;
7+
this->id = id;
78
reset();
89
}
910

@@ -77,6 +78,10 @@ byte EButton::getPin() {
7778
return pin;
7879
}
7980

81+
byte EButton::getID() {
82+
return id;
83+
}
84+
8085
byte EButton::getClicks() {
8186
return clicks;
8287
}

EButton.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ typedef void (*EButtonEventHandler)(EButton&);
101101
class EButton {
102102
public:
103103
// Constructor.
104-
EButton(byte pin, bool pressedLow = true);
104+
EButton(byte pin, bool pressedLow = true, byte id = 0);
105105

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

156+
// Button ID
157+
byte getID();
158+
156159
// Number of clicks performed
157160
byte getClicks();
158161

@@ -178,6 +181,7 @@ class EButton {
178181

179182
// ----- Configuration-specific fields -----
180183
byte pin; // Attached pin
184+
byte id; // Button ID
181185
byte debounceTime = EBUTTON_DEFAULT_DEBOUNCE; // Debounce time in ms (between 0 and 255)
182186
#if defined(EBUTTON_SUPPORT_DONE_CLICKING) || defined(EBUTTON_SUPPORT_SINGLE_AND_DOUBLE_CLICKS)
183187
unsigned int clickTime = EBUTTON_DEFAULT_CLICK; // Time the button has to be released in order to complete counting clicks
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "EButton.h"
2+
3+
EButton buttons[] = {
4+
{6, true, 1}, {4, true, 2}, {2, true, 3},
5+
{7, true, 4}, {5, true, 5}, {3, true, 6}};
6+
7+
8+
void sharedClick(EButton& btn) {
9+
Serial.println(btn.getID());
10+
}
11+
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
16+
for (EButton& btn: buttons) {
17+
btn.attachSingleClick(sharedClick);
18+
}
19+
}
20+
21+
void loop() {
22+
for (EButton& btn: buttons) {
23+
btn.tick();
24+
}
25+
}

0 commit comments

Comments
 (0)