7#include <driver/gpio.h>
8#include <eventpp/callbacklist.h>
9#include <eventpp/utilities/conditionalremover.h>
10#include <eventpp/utilities/counterremover.h>
11#include <freertos/FreeRTOS.h>
12#include <freertos/queue.h>
13#include <soc/gpio_periph.h>
21 using CallbackList = eventpp::CallbackList<void(std::bitset<MaxID> currentState, std::bitset<MaxID> changedBtns)>;
24 static constexpr const char*
const m_tag =
"Buttons";
27 QueueHandle_t m_eventQueue;
29 static void callbacksTask(
void* selfVoid);
34 static void IRAM_ATTR isrHandler(
void* selfVoid);
40 static bool IRAM_ATTR
read(gpio_num_t);
42 static std::bitset<MaxID> IRAM_ATTR
readAll();
46 static bool IRAM_ATTR
precti(gpio_num_t cisloPinu) {
47 return read(cisloPinu);
50 return read(idTlacitka);
110 CallbackList::Handle
onChange(CallbackList::Callback function);
111 CallbackList::Handle
onChange(std::function<
void(std::bitset<MaxID> currentState)> function) {
112 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID>) { function(currentState); });
114 CallbackList::Handle
onChange(std::function<
void()> function) {
115 return onChange([=](std::bitset<MaxID>, std::bitset<MaxID>) { function(); });
119 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
120 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changedBtns) {
121 if (changedBtns[watchedButton]) {
122 function(currentState, changedBtns);
126 CallbackList::Handle
onChange(std::function<
void(
bool isPressed)> function,
ButtonID watchedButton) {
127 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
128 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changedBtns) {
129 if (changedBtns[watchedButton]) {
130 function(currentState[watchedButton]);
135 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
136 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changedBtns) {
137 if (changedBtns[watchedButton]) {
142 Buttons::CallbackList::Handle
onNextChanges(Buttons::CallbackList::Callback function,
int count) {
143 return eventpp::counterRemover(m_callbackListButtons).append(function, count);
145 Buttons::CallbackList::Handle
onNextChange(Buttons::CallbackList::Callback function) {
148 CallbackList::Handle
onNextChanges(std::function<
void(
bool isPressed)> function,
ButtonID watchedButton,
int count) {
149 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
150 return onNextChanges([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changedBtns) {
151 if (changedBtns[watchedButton]) {
152 function(currentState[watchedButton]);
158 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
159 return onNextChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changedBtns) {
160 if (changedBtns[watchedButton]) {
161 function(currentState[watchedButton]);
165 template<
typename Condition>
166 Buttons::CallbackList::Handle
onChangeUntil(Buttons::CallbackList::Callback function, Condition condition) {
167 return eventpp::conditionalRemover<Condition>(m_callbackListButtons).append(function, condition);
204 CallbackList::Handle
onPress(CallbackList::Callback function) {
205 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
206 if ((changed & currentState).any()) {
207 function(currentState, changed);
211 CallbackList::Handle
onPress(std::function<
void(std::bitset<MaxID> currentState)> function) {
212 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
213 if ((changed & currentState).any()) {
214 function(currentState);
218 CallbackList::Handle
onPress(std::function<
void()> function) {
219 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
220 if ((changed & currentState).any()) {
226 CallbackList::Handle
onPress(std::function<
void()> function,
ButtonID watchedButton) {
227 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
228 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
229 if (currentState[watchedButton] && changed[watchedButton]) {
235 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
236 return onNextChanges([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
237 if (currentState[watchedButton] && changed[watchedButton]) {
245 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
246 return onNextChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
247 if (currentState[watchedButton] && changed[watchedButton]) {
253 template<
typename Condition>
254 Buttons::CallbackList::Handle
onPressUntil(std::function<
void()> function, Condition condition,
ButtonID watchedButton) {
255 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
256 return onChangeUntil<Condition>([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
257 if (currentState[watchedButton] && changed[watchedButton]) {
296 CallbackList::Handle
onRelease(CallbackList::Callback function) {
297 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
298 if ((changed & ~currentState).any()) {
299 function(currentState, changed);
303 CallbackList::Handle
onRelease(std::function<
void(std::bitset<MaxID> currentState)> function) {
304 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
305 if ((changed & ~currentState).any()) {
306 function(currentState);
310 CallbackList::Handle
onRelease(std::function<
void()> function) {
311 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
312 if ((changed & ~currentState).any()) {
319 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
320 return onChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
321 if (!currentState[watchedButton] && changed[watchedButton]) {
327 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
328 return onNextChanges([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
329 if (!currentState[watchedButton] && changed[watchedButton]) {
337 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
338 return onNextChange([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
339 if (!currentState[watchedButton] && changed[watchedButton]) {
345 template<
typename Condition>
346 Buttons::CallbackList::Handle
onReleaseUntil(std::function<
void()> function, Condition condition,
ButtonID watchedButton) {
347 checkRange(watchedButton, 0, ButtonID::MaxID - 1, m_tag);
348 return onChangeUntil<Condition>([=](std::bitset<MaxID> currentState, std::bitset<MaxID> changed) {
349 if (!currentState[watchedButton] && changed[watchedButton]) {
408 CallbackList::Handle
priZmene(std::function<
void(std::bitset<MaxID> aktualniStav)> funkce) {
return onChange(funkce); }
411 CallbackList::Handle
priZmene(CallbackList::Callback funkce,
ButtonID sledovaneTlacitko) {
return onChange(funkce, sledovaneTlacitko); }
412 CallbackList::Handle
priZmene(std::function<
void(
bool jeStisknuto)> funkce,
ButtonID sledovaneTlacitko) {
413 return onChange(funkce, sledovaneTlacitko);
416 return onChange(funkce, sledovaneTlacitko);
451 CallbackList::Handle
priStisku(CallbackList::Callback funkce) {
454 CallbackList::Handle
priStisku(std::function<
void(std::bitset<MaxID> currentState)> funkce) {
457 CallbackList::Handle
priStisku(std::function<
void()> funkce) {
462 return onPress(funkce, sledovaneTlacitko);
500 CallbackList::Handle
priUvolneni(std::function<
void(std::bitset<MaxID> currentState)> funkce) {
508 return onRelease(funkce, sledovaneTlacitko);
ButtonID
Definition: 1_1.hpp:6
void checkRange(int value, int min, int max, const char *tag, unsigned stackTraceDepth=10)
Definition: Error.hpp:45