File tree Expand file tree Collapse file tree 7 files changed +24
-47
lines changed Expand file tree Collapse file tree 7 files changed +24
-47
lines changed Original file line number Diff line number Diff line change @@ -271,7 +271,6 @@ void setBtnLevel(bool level);
271
271
272
272
// подключить функцию-обработчик событий
273
273
void attach(void (*handler)());
274
- void attach(void (*handler)(void* self));
275
274
276
275
// отключить функцию-обработчик событий
277
276
void detach();
@@ -1038,6 +1037,8 @@ if (btn.busy()) {
1038
1037
- ` VirtEncButton `
1039
1038
- ` EncButton `
1040
1039
1040
+ > Внутри коллбэка можно получить указатель на текущий объект (который вызвал коллбэк) из переменной ` void* EB_self `
1041
+
1041
1042
``` cpp
1042
1043
EncButton eb (2, 3, 4);
1043
1044
@@ -1051,33 +1052,8 @@ void callback() {
1051
1052
break;
1052
1053
// ...
1053
1054
}
1054
- }
1055
-
1056
- void setup() {
1057
- eb.attach(callback);
1058
- }
1059
1055
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
1081
1057
}
1082
1058
1083
1059
void setup() {
Original file line number Diff line number Diff line change 6
6
EncButton eb (2 , 3 , 4 );
7
7
8
8
void cb () {
9
+ // здесь EB_self - указатель на сам объект
10
+
9
11
Serial.print (" callback: " );
10
12
switch (eb.action ()) {
11
13
case EB_PRESS:
Original file line number Diff line number Diff line change @@ -44,20 +44,20 @@ void setup() {
44
44
Serial.begin (115200 );
45
45
46
46
// обработчики
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 ();
49
49
if (action != EB_HOLD) Serial.println (" b0" );
50
50
decode (action);
51
51
});
52
52
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 ();
55
55
if (action != EB_HOLD) Serial.println (" b1" );
56
56
decode (action);
57
57
});
58
58
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 ();
61
61
if (action != EB_HOLD) Serial.println (" b2" );
62
62
decode (action);
63
63
});
Original file line number Diff line number Diff line change 1
1
name =EncButton
2
- version =3.6.1
2
+ version =3.6.2
3
3
author =AlexGyver <
[email protected] >
4
4
maintainer =AlexGyver <
[email protected] >
5
5
sentence =Light and powerful library for button and encoder operation for Arduino
Original file line number Diff line number Diff line change 3
3
4
4
#include " flags.h"
5
5
#include " io.h"
6
+ #include " self.h"
6
7
7
8
#ifndef __AVR__
8
9
#include < functional>
69
70
class VirtButton {
70
71
#ifdef __AVR__
71
72
typedef void (*ActionHandler)();
72
- typedef void (*ActionHandlerThis)(void *);
73
73
#else
74
74
typedef std::function<void ()> ActionHandler;
75
- typedef std::function<void (void *)> ActionHandlerThis;
76
75
#endif
77
76
78
77
public:
@@ -142,18 +141,10 @@ class VirtButton {
142
141
#endif
143
142
}
144
143
145
- // подключить функцию-обработчик событий (вида void f(void*))
146
- void attach (ActionHandlerThis handler) {
147
- #ifndef EB_NO_CALLBACK
148
- cbt = handler;
149
- #endif
150
- }
151
-
152
144
// отключить функцию-обработчик событий
153
145
void detach () {
154
146
#ifndef EB_NO_CALLBACK
155
147
cb = nullptr ;
156
- cbt = nullptr ;
157
148
#endif
158
149
}
159
150
@@ -409,8 +400,11 @@ class VirtButton {
409
400
void call () {
410
401
if (action ()) {
411
402
#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
+ }
414
408
#endif
415
409
}
416
410
}
@@ -429,7 +423,6 @@ class VirtButton {
429
423
430
424
#ifndef EB_NO_CALLBACK
431
425
ActionHandler cb = nullptr ;
432
- ActionHandlerThis cbt = nullptr ;
433
426
#endif
434
427
435
428
private:
Original file line number Diff line number Diff line change
1
+ #include " self.h"
2
+
3
+ void * EB_self = nullptr ;
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ extern void * EB_self ;
You can’t perform that action at this time.
0 commit comments