Skip to content

Commit 67c6629

Browse files
committed
upd
1 parent fe7a166 commit 67c6629

File tree

7 files changed

+24
-47
lines changed

7 files changed

+24
-47
lines changed

README.md

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ void setBtnLevel(bool level);
271271
272272
// подключить функцию-обработчик событий
273273
void attach(void (*handler)());
274-
void attach(void (*handler)(void* self));
275274
276275
// отключить функцию-обработчик событий
277276
void detach();
@@ -1038,6 +1037,8 @@ if (btn.busy()) {
10381037
- `VirtEncButton`
10391038
- `EncButton`
10401039

1040+
> Внутри коллбэка можно получить указатель на текущий объект (который вызвал коллбэк) из переменной `void* EB_self`
1041+
10411042
```cpp
10421043
EncButton eb(2, 3, 4);
10431044

@@ -1051,33 +1052,8 @@ void callback() {
10511052
break;
10521053
// ...
10531054
}
1054-
}
1055-
1056-
void setup() {
1057-
eb.attach(callback);
1058-
}
10591055

1060-
void loop() {
1061-
eb.tick();
1062-
}
1063-
```
1064-
1065-
С версии 3.6.0 библиотека поддерживает подключение обработчика с отправкой в него указателя на объект:
1066-
```cpp
1067-
EncButton eb(2, 3, 4);
1068-
1069-
void callback(void* self) {
1070-
EncButton& enc = *static_cast<EncButton*>(self);
1071-
1072-
switch (enc.action()) {
1073-
case EB_PRESS:
1074-
// ...
1075-
break;
1076-
case EB_HOLD:
1077-
// ...
1078-
break;
1079-
// ...
1080-
}
1056+
// здесь EB_self указатель на eb
10811057
}
10821058

10831059
void setup() {

examples/callback/callback.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
EncButton eb(2, 3, 4);
77

88
void cb() {
9+
// здесь EB_self - указатель на сам объект
10+
911
Serial.print("callback: ");
1012
switch (eb.action()) {
1113
case EB_PRESS:

examples/doubleCallback/doubleCallback.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ void setup() {
4444
Serial.begin(115200);
4545

4646
// обработчики
47-
b0.attach([](void* b) {
48-
uint16_t action = static_cast<VirtButton*>(b)->action();
47+
b0.attach([]() {
48+
uint16_t action = static_cast<VirtButton*>(EB_self)->action();
4949
if (action != EB_HOLD) Serial.println("b0");
5050
decode(action);
5151
});
5252

53-
b1.attach([](void* b) {
54-
uint16_t action = static_cast<VirtButton*>(b)->action();
53+
b1.attach([]() {
54+
uint16_t action = static_cast<VirtButton*>(EB_self)->action();
5555
if (action != EB_HOLD) Serial.println("b1");
5656
decode(action);
5757
});
5858

59-
b2.attach([](void* b) {
60-
uint16_t action = static_cast<VirtButton*>(b)->action();
59+
b2.attach([]() {
60+
uint16_t action = static_cast<VirtButton*>(EB_self)->action();
6161
if (action != EB_HOLD) Serial.println("b2");
6262
decode(action);
6363
});

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=EncButton
2-
version=3.6.1
2+
version=3.6.2
33
author=AlexGyver <[email protected]>
44
maintainer=AlexGyver <[email protected]>
55
sentence=Light and powerful library for button and encoder operation for Arduino

src/core/VirtButton.h

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

44
#include "flags.h"
55
#include "io.h"
6+
#include "self.h"
67

78
#ifndef __AVR__
89
#include <functional>
@@ -69,10 +70,8 @@
6970
class VirtButton {
7071
#ifdef __AVR__
7172
typedef void (*ActionHandler)();
72-
typedef void (*ActionHandlerThis)(void*);
7373
#else
7474
typedef std::function<void()> ActionHandler;
75-
typedef std::function<void(void*)> ActionHandlerThis;
7675
#endif
7776

7877
public:
@@ -142,18 +141,10 @@ class VirtButton {
142141
#endif
143142
}
144143

145-
// подключить функцию-обработчик событий (вида void f(void*))
146-
void attach(ActionHandlerThis handler) {
147-
#ifndef EB_NO_CALLBACK
148-
cbt = handler;
149-
#endif
150-
}
151-
152144
// отключить функцию-обработчик событий
153145
void detach() {
154146
#ifndef EB_NO_CALLBACK
155147
cb = nullptr;
156-
cbt = nullptr;
157148
#endif
158149
}
159150

@@ -409,8 +400,11 @@ class VirtButton {
409400
void call() {
410401
if (action()) {
411402
#ifndef EB_NO_CALLBACK
412-
if (cb) cb();
413-
if (cbt) cbt(this);
403+
if (cb) {
404+
EB_self = this;
405+
cb();
406+
EB_self = nullptr;
407+
}
414408
#endif
415409
}
416410
}
@@ -429,7 +423,6 @@ class VirtButton {
429423

430424
#ifndef EB_NO_CALLBACK
431425
ActionHandler cb = nullptr;
432-
ActionHandlerThis cbt = nullptr;
433426
#endif
434427

435428
private:

src/core/self.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "self.h"
2+
3+
void* EB_self = nullptr;

src/core/self.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
extern void* EB_self;

0 commit comments

Comments
 (0)