working example of callback with date passing inside and outside

This commit is contained in:
Dmitry Borisenko
2020-08-24 15:09:43 +03:00
parent e44d2dc793
commit ce7c550410
3 changed files with 43 additions and 13 deletions

View File

@@ -5,9 +5,28 @@ AsyncActions::AsyncActions(){};
void AsyncActions::loop() {
count++;
if (count > 5000) {
if(_cb) _cb; //что означает эта запись? это и есть вызов callback?
// Проверяем что переменная содержит указатель - не пустая не null
// и непосредственно вызываем то, на что это указывает
// просто пишем имя - без () - это указатель на фунецию.
// () - вызываем функцию - с пустым набором параметров
if (_cb != NULL) {
_cb();
}
//или ровно тоже самое
//if (_cb) _cb();
if (_pcb) {
if (_pcb("SomeTextValue")) {
Serial.print("Got true!");
} else {
Serial.print("Got false!");
}
}
count = 0;
}
}
AsyncActions* async;

View File

@@ -1,4 +1,5 @@
#include "Bus/BusScannerFactory.h"
#include "Class/AsyncActions.h"
#include "Class/Switch.h"
#include "Cmd.h"
#include "DeviceList.h"
@@ -6,7 +7,6 @@
#include "HttpServer.h"
#include "Init.h"
#include "Utils/Timings.h"
#include "Class/AsyncActions.h"
void not_async_actions();
@@ -69,7 +69,8 @@ void setup() {
udp_init();
#endif
ts.add(TEST, 1000 * 60, [&](void*) { pm.info(printMemoryStatus()); }, nullptr, true);
ts.add(
TEST, 1000 * 60, [&](void*) { pm.info(printMemoryStatus()); }, nullptr, true);
just_load = false;
initialized = true;
@@ -77,14 +78,20 @@ void setup() {
async = new AsyncActions();
//async->setCallback([&](void*) {
//
//
//
//
//});
async->setCallback(myCallback); //
async->setCallback([]() {
Serial.println("123");
});
async->setCallback([](const String str) {
Serial.println(str);
return false;
});
}
void loop() {