-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,8 +269,9 @@ void setDebTimeout(uint8_t tout); | |
// умолч. HIGH, то есть true - кнопка нажата | ||
void setBtnLevel(bool level); | ||
// подключить функцию-обработчик событий (вида void f()) | ||
// подключить функцию-обработчик событий | ||
void attach(void (*handler)()); | ||
void attach(void (*handler)(void* self)); | ||
// отключить функцию-обработчик событий | ||
void detach(); | ||
|
@@ -1061,26 +1062,50 @@ void loop() { | |
} | ||
``` | ||
С версии 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; | ||
// ... | ||
} | ||
} | ||
void setup() { | ||
eb.attach(callback); | ||
} | ||
void loop() { | ||
eb.tick(); | ||
} | ||
``` | ||
|
||
<a id="double"></a> | ||
|
||
### Одновременное нажатие | ||
Библиотека нативно поддерживает работу с двумя одновременно нажатыми кнопками как с третьей кнопкой. Для этого нужно: | ||
1. Cоздать виртуальную кнопку `VirtButton` | ||
2. Вызвать обработку реальных кнопок | ||
3. Передать виртуальной кнопке в обработку эти кнопки (это могут быть объекты классов `VirtButton`, `Button`, `EncButton` + их `T`-версии) | ||
4. Далее опрашивать события | ||
1. Cоздать специальную кнопку `MultiButton` | ||
2. Передать виртуальной кнопке в обработку свои кнопки (это могут быть объекты классов `VirtButton`, `Button`, `EncButton` + их `T`-версии). **Мульти-кнопка сама опросит обе кнопки!** | ||
3. Опрашивать события или слушать обработчик | ||
|
||
```cpp | ||
Button b0(4); | ||
Button b1(5); | ||
VirtButton b2; // 1 | ||
MultiButton b2; // 1 | ||
|
||
void loop() { | ||
b0.tick(); // 2 | ||
b1.tick(); // 2 | ||
b2.tick(b0, b1); // 3 | ||
b2.tick(b0, b1); // 2 | ||
|
||
// 4 | ||
// 3 | ||
if (b0.click()) Serial.println("b0 click"); | ||
if (b1.click()) Serial.println("b1 click"); | ||
if (b2.click()) Serial.println("b0+b1 click"); | ||
|
@@ -1792,7 +1817,10 @@ void loop() { | |
- v3.5.5 - коллбэк на базе std::function для ESP | ||
- v3.5.8 - добавлен метод releaseHoldStep() | ||
- v3.5.11 - добавлен метод skipEvents() для игнорирования событий кнопки в сложных сценариях использования | ||
- v3.6.0 | ||
- Добавлен класс MultiButton для корректного опроса нескольких кнопок с вызовом обработчика | ||
- Добавлено подключение обработчика с передачей указателя на объект | ||
<a id="feedback"></a> | ||
## Баги и обратная связь | ||
При нахождении багов создавайте **Issue**, а лучше сразу пишите на почту [[email protected]](mailto:[email protected]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// опрос одновременного нажатия двух кнопок как нажатия третьей кнопки | ||
// с корректным вызовом обработчиков | ||
|
||
#include <Arduino.h> | ||
#include <EncButton.h> | ||
|
||
Button b0(4); | ||
Button b1(5); | ||
MultiButton b2; // виртуальная | ||
|
||
void decode(uint16_t action) { | ||
switch (action) { | ||
case EB_PRESS: | ||
Serial.println("press"); | ||
break; | ||
case EB_STEP: | ||
Serial.println("step"); | ||
break; | ||
case EB_RELEASE: | ||
Serial.println("release"); | ||
break; | ||
case EB_CLICK: | ||
Serial.println("click"); | ||
break; | ||
case EB_CLICKS: | ||
Serial.print("clicks"); | ||
break; | ||
case EB_REL_HOLD: | ||
Serial.println("release hold"); | ||
break; | ||
case EB_REL_HOLD_C: | ||
Serial.print("release hold clicks "); | ||
break; | ||
case EB_REL_STEP: | ||
Serial.println("release step"); | ||
break; | ||
case EB_REL_STEP_C: | ||
Serial.print("release step clicks "); | ||
break; | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
// обработчики | ||
b0.attach([](void* b) { | ||
uint16_t action = static_cast<VirtButton*>(b)->action(); | ||
if (action != EB_HOLD) Serial.println("b0"); | ||
decode(action); | ||
}); | ||
|
||
b1.attach([](void* b) { | ||
uint16_t action = static_cast<VirtButton*>(b)->action(); | ||
if (action != EB_HOLD) Serial.println("b1"); | ||
decode(action); | ||
}); | ||
|
||
b2.attach([](void* b) { | ||
uint16_t action = static_cast<VirtButton*>(b)->action(); | ||
if (action != EB_HOLD) Serial.println("b2"); | ||
decode(action); | ||
}); | ||
} | ||
|
||
void loop() { | ||
// обработка одновременного нажатия двух кнопок | ||
// обрабатываются все три кнопки | ||
b2.tick(b0, b1); | ||
|
||
// или вручную | ||
if (b0.click()) Serial.println("b0 click"); | ||
if (b1.click()) Serial.println("b1 click"); | ||
if (b2.click()) Serial.println("b0+b1 click"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=EncButton | ||
version=3.5.11 | ||
version=3.6.0 | ||
author=AlexGyver <[email protected]> | ||
maintainer=AlexGyver <[email protected]> | ||
sentence=Light and powerful library for button and encoder operation for Arduino | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
#pragma once | ||
#include <Arduino.h> | ||
|
||
#include "core/VirtButton.h" | ||
#include "core/VirtEncoder.h" | ||
#include "core/VirtEncButton.h" | ||
#include "core/Button.h" | ||
#include "core/EncButton.h" | ||
#include "core/Encoder.h" | ||
#include "core/EncButton.h" | ||
#include "core/MultiButton.h" | ||
#include "core/VirtButton.h" | ||
#include "core/VirtEncButton.h" | ||
#include "core/VirtEncoder.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
#include <Arduino.h> | ||
|
||
#include "VirtButton.h" | ||
|
||
class MultiButton : public VirtButton { | ||
public: | ||
template <typename T0, typename T1> | ||
bool tick(T0& b0, T1& b1) { | ||
b0.clear(); | ||
b1.clear(); | ||
b0.tickRaw(); | ||
b1.tickRaw(); | ||
|
||
if (bf.read(EB_BOTH)) { | ||
if (!b0.pressing() && !b1.pressing()) bf.clear(EB_BOTH); | ||
if (!b0.pressing()) b0.reset(); | ||
if (!b1.pressing()) b1.reset(); | ||
b0.clear(); | ||
b1.clear(); | ||
return VirtButton::tick(true); | ||
} else { | ||
if (b0.pressing() && b1.pressing()) bf.set(EB_BOTH); | ||
b0.call(); | ||
b1.call(); | ||
return VirtButton::tick(false); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters