Skip to content

Commit fa9bc7b

Browse files
committed
added debugging
fixed event pointer indexing bug
1 parent 7789351 commit fa9bc7b

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

include/EZButton.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <Arduino.h>
55

6+
// #define DEBUG
7+
68
#define EVENT_COUNT 4
79
enum EventTypes
810
{
@@ -41,6 +43,11 @@ class EZButton
4143
void (*_readButtons)(bool *, int);
4244

4345
void CallEvent(int index, EventTypes type);
46+
int EventIndex(int index, EventTypes type);
47+
48+
#ifdef DEBUG
49+
void DebugEvents(int index, EventTypes type);
50+
#endif
4451
};
4552

4653
#endif // EZButton_H

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "EZButton",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "A Library for managing different button events. Events include: OnPressed, OnReleased, OnHold, OnHoldReleased.",
55
"keywords": "button, key, event, hold, press, release",
66
"repository":

src/EZButton.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,36 @@ void EZButton::CheckButtons()
8282

8383
void EZButton::Subscribe(int index, void (*event)(), EventTypes type)
8484
{
85-
_events[index + type * EVENT_COUNT] = event;
85+
_events[EventIndex(index, type)] = event;
86+
87+
#ifdef DEBUG
88+
Serial.println("Subscribe:");
89+
DebugEvents(index, type);
90+
#endif
8691
}
8792

8893
void EZButton::CallEvent(int index, EventTypes type)
8994
{
90-
int i = index + type * EVENT_COUNT;
95+
int i = EventIndex(index, type);
96+
97+
#ifdef DEBUG
98+
Serial.println("Call:");
99+
DebugEvents(index, type);
100+
#endif
101+
91102
if (_events[i] != nullptr)
92103
_events[i]();
93-
}
104+
}
105+
106+
int EZButton::EventIndex(int index, EventTypes type){
107+
return index + type * _numButtons;
108+
}
109+
110+
#ifdef DEBUG
111+
void EZButton::DebugEvents(int index, EventTypes type)
112+
{
113+
Serial.println("index : " + (String)index);
114+
Serial.println("event : " + (String)type);
115+
Serial.println("i: " + (String)EventIndex(index, type));
116+
}
117+
#endif

0 commit comments

Comments
 (0)