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);
271271
272272// подключить функцию-обработчик событий
273273void attach(void (*handler)());
274- void attach(void (*handler)(void* self));
275274
276275// отключить функцию-обработчик событий
277276void detach();
@@ -1038,6 +1037,8 @@ if (btn.busy()) {
10381037- ` VirtEncButton `
10391038- ` EncButton `
10401039
1040+ > Внутри коллбэка можно получить указатель на текущий объект (который вызвал коллбэк) из переменной ` void* EB_self `
1041+
10411042``` cpp
10421043EncButton 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
10831059void setup() {
Original file line number Diff line number Diff line change 66EncButton eb (2 , 3 , 4 );
77
88void cb () {
9+ // здесь EB_self - указатель на сам объект
10+
911 Serial.print (" callback: " );
1012 switch (eb.action ()) {
1113 case EB_PRESS:
Original file line number Diff line number Diff 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 });
Original file line number Diff line number Diff line change 11name =EncButton
2- version =3.6.1
2+ version =3.6.2
33author =AlexGyver <
[email protected] >
44maintainer =AlexGyver <
[email protected] >
55sentence =Light and powerful library for button and encoder operation for Arduino
Original file line number Diff line number Diff line change 33
44#include " flags.h"
55#include " io.h"
6+ #include " self.h"
67
78#ifndef __AVR__
89#include < functional>
6970class 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:
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