Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
GyverLibs committed Aug 27, 2024
1 parent fe7a166 commit 67c6629
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 47 deletions.
30 changes: 3 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ void setBtnLevel(bool level);
// подключить функцию-обработчик событий
void attach(void (*handler)());
void attach(void (*handler)(void* self));
// отключить функцию-обработчик событий
void detach();
Expand Down Expand Up @@ -1038,6 +1037,8 @@ if (btn.busy()) {
- `VirtEncButton`
- `EncButton`

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

Expand All @@ -1051,33 +1052,8 @@ void callback() {
break;
// ...
}
}

void setup() {
eb.attach(callback);
}

void loop() {
eb.tick();
}
```
С версии 3.6.0 библиотека поддерживает подключение обработчика с отправкой в него указателя на объект:
```cpp
EncButton eb(2, 3, 4);
void callback(void* self) {
EncButton& enc = *static_cast<EncButton*>(self);
switch (enc.action()) {
case EB_PRESS:
// ...
break;
case EB_HOLD:
// ...
break;
// ...
}
// здесь EB_self указатель на eb
}

void setup() {
Expand Down
2 changes: 2 additions & 0 deletions examples/callback/callback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
EncButton eb(2, 3, 4);

void cb() {
// здесь EB_self - указатель на сам объект

Serial.print("callback: ");
switch (eb.action()) {
case EB_PRESS:
Expand Down
12 changes: 6 additions & 6 deletions examples/doubleCallback/doubleCallback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ void setup() {
Serial.begin(115200);

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

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

b2.attach([](void* b) {
uint16_t action = static_cast<VirtButton*>(b)->action();
b2.attach([]() {
uint16_t action = static_cast<VirtButton*>(EB_self)->action();
if (action != EB_HOLD) Serial.println("b2");
decode(action);
});
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=EncButton
version=3.6.1
version=3.6.2
author=AlexGyver <[email protected]>
maintainer=AlexGyver <[email protected]>
sentence=Light and powerful library for button and encoder operation for Arduino
Expand Down
19 changes: 6 additions & 13 deletions src/core/VirtButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "flags.h"
#include "io.h"
#include "self.h"

#ifndef __AVR__
#include <functional>
Expand Down Expand Up @@ -69,10 +70,8 @@
class VirtButton {
#ifdef __AVR__
typedef void (*ActionHandler)();
typedef void (*ActionHandlerThis)(void*);
#else
typedef std::function<void()> ActionHandler;
typedef std::function<void(void*)> ActionHandlerThis;
#endif

public:
Expand Down Expand Up @@ -142,18 +141,10 @@ class VirtButton {
#endif
}

// подключить функцию-обработчик событий (вида void f(void*))
void attach(ActionHandlerThis handler) {
#ifndef EB_NO_CALLBACK
cbt = handler;
#endif
}

// отключить функцию-обработчик событий
void detach() {
#ifndef EB_NO_CALLBACK
cb = nullptr;
cbt = nullptr;
#endif
}

Expand Down Expand Up @@ -409,8 +400,11 @@ class VirtButton {
void call() {
if (action()) {
#ifndef EB_NO_CALLBACK
if (cb) cb();
if (cbt) cbt(this);
if (cb) {
EB_self = this;
cb();
EB_self = nullptr;
}
#endif
}
}
Expand All @@ -429,7 +423,6 @@ class VirtButton {

#ifndef EB_NO_CALLBACK
ActionHandler cb = nullptr;
ActionHandlerThis cbt = nullptr;
#endif

private:
Expand Down
3 changes: 3 additions & 0 deletions src/core/self.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "self.h"

void* EB_self = nullptr;
3 changes: 3 additions & 0 deletions src/core/self.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

extern void* EB_self;

0 comments on commit 67c6629

Please sign in to comment.