Skip to content

Commit d559b06

Browse files
authored
Merge pull request #18 from IPdotSetAF/17-event-button-index-argument
17 event button index argument
2 parents 2aaba47 + a88cf05 commit d559b06

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ _ezb.Subscribe(BTN_3, Btn3Hold, HOLD);
9191
_ezb.Subscribe(BTN_3, Btn3Release, RELEASED);
9292
_ezb.Subscribe(BTN_4, Btn4Hold, HOLD);
9393
_ezb.Subscribe(BTN_4, Btn4Release, RELEASED);
94+
95+
//or you can pass lambda functions
96+
_ezb.Subscribe(BTN_4, []() {
97+
//...
98+
}, HOLD);
99+
_ezb.Subscribe(BTN_4, [](int index) {
100+
//index will be the index of the button triggering the event
101+
//...
102+
}, RELEASED);
94103
```
95104
> [!IMPORTANT]
96105
> `button index` stands for an array inside EZButton that holds your button states and IS NOT PIN of the button.
@@ -115,7 +124,18 @@ void loop()
115124
_ezb.Loop();
116125
}
117126
```
127+
8. Define Event Functions that was used in Subscriptions (step 5)
128+
``` C++
129+
void Btn1HoldRelease(){
130+
//...
131+
}
118132

133+
// you can also get the button index
134+
void Btn2Release(int index){
135+
//...
136+
}
137+
//...
138+
```
119139
### Debugging
120140
> [!TIP]
121141
> To enable debugging, you need to add the `-DEZBUTTON_DEBUG` parameter to your `build_flags`.

examples/Blink/Blink.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ void setup() {
1919
_ezb.Subscribe(BTN_1, Btn1Pressed, PRESSED);
2020
_ezb.Subscribe(BTN_1, Btn1Released, RELEASED);
2121
_ezb.Subscribe(BTN_1, Btn1Hold, HOLD);
22+
//Or you can use lambda functions
23+
// _ezb.Subscribe(BTN_1, [](){Serial.println("pressed");}, PRESSED);
24+
25+
//You can also get the button index in the event
2226
_ezb.Subscribe(BTN_1, Btn1HoldReleased, HOLD_RELEASED);
27+
//Or you can use lambda functions
28+
// _ezb.Subscribe(BTN_1, [](int index){Serial.println(index);}, HOLD);
2329
}
2430

2531
void loop() {
@@ -41,7 +47,7 @@ void Btn1Hold() {
4147
digitalWrite(LED_BUILTIN, state);
4248
}
4349

44-
void Btn1HoldReleased() {
50+
void Btn1HoldReleased(int index) {
4551
for (int i = 0; i < 6; i++) {
4652
state = !state;
4753
digitalWrite(LED_BUILTIN, state);

src/EZButton.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void EZButton::Loop()
7171
unsigned long stateChangeDiff = millis() - _buttonDownMillis[i];
7272
if (stateChangeDiff < DebounceTime)
7373
continue;
74-
74+
7575
if (buttonStates[i])
7676
{
7777
if (!_buttonLastState[i])
@@ -107,21 +107,26 @@ void EZButton::Loop()
107107
delete[] buttonStates;
108108
}
109109

110-
void EZButton::Subscribe(int index, void (*event)(), EventTypes type)
110+
void EZButton::Subscribe(int index, Event event, EventTypes type)
111111
{
112112
_events[EventIndex(index, type)] = event;
113113

114114
LogEvent("Subscribe:", index, type);
115115
}
116116

117+
void EZButton::Subscribe(int index, void (*event)(), EventTypes type)
118+
{
119+
Subscribe(index, (Event)event, type);
120+
}
121+
117122
void EZButton::CallEvent(int index, EventTypes type)
118123
{
119124
int i = EventIndex(index, type);
120125

121-
LogEvent("Call:",index, type);
126+
LogEvent("Call:", index, type);
122127

123128
if (_events[i] != nullptr)
124-
_events[i]();
129+
_events[i](index);
125130
}
126131

127132
int EZButton::EventIndex(int index, EventTypes type)

src/EZButton.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ enum EventTypes
1212
RELEASED
1313
};
1414

15+
typedef void (*Event)(int); // Type alias for function pointer
16+
1517
class EZButton
1618
{
1719
public:
@@ -29,6 +31,7 @@ class EZButton
2931

3032
void Blackout(unsigned long milis);
3133
void Loop();
34+
void Subscribe(int index, Event event, EventTypes type);
3235
void Subscribe(int index, void (*event)(), EventTypes type);
3336

3437
private:
@@ -39,7 +42,6 @@ class EZButton
3942
bool *_buttonLastState;
4043
unsigned long _blackoutTime = 0;
4144

42-
typedef void (*Event)(); // Type alias for function pointer
4345
Event *_events;
4446
void (*_readButtons)(bool *, int);
4547

0 commit comments

Comments
 (0)