mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-15 00:02:49 +03:00
allow multiple event callbacks and add filter option
This commit is contained in:
@ -247,8 +247,12 @@ void optimistic_yield(uint32_t interval_us);
|
||||
#include "Updater.h"
|
||||
#include "debug.h"
|
||||
|
||||
#ifndef _GLIBCXX_VECTOR
|
||||
// arduino is not compatible with std::vector
|
||||
#define min(a,b) ((a)<(b)?(a):(b))
|
||||
#define max(a,b) ((a)>(b)?(a):(b))
|
||||
#endif
|
||||
|
||||
#define _min(a,b) ((a)<(b)?(a):(b))
|
||||
#define _max(a,b) ((a)>(b)?(a):(b))
|
||||
|
||||
|
@ -43,6 +43,10 @@ extern "C" {
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
#undef min
|
||||
#undef max
|
||||
#include <vector>
|
||||
|
||||
extern "C" void esp_schedule();
|
||||
extern "C" void esp_yield();
|
||||
|
||||
@ -50,8 +54,10 @@ extern "C" void esp_yield();
|
||||
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// arduino dont like std::vectors move static here
|
||||
static std::vector<WiFiEventCbList_t> cbEventList;
|
||||
|
||||
bool ESP8266WiFiGenericClass::_persistent = true;
|
||||
WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL;
|
||||
WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF;
|
||||
|
||||
ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() {
|
||||
@ -61,9 +67,34 @@ ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() {
|
||||
/**
|
||||
* set callback function
|
||||
* @param cbEvent WiFiEventCb
|
||||
* @param event optional filter (WIFI_EVENT_MAX is all events)
|
||||
*/
|
||||
void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent) {
|
||||
_cbEvent = cbEvent;
|
||||
void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent, WiFiEvent_t event) {
|
||||
if(!cbEvent) {
|
||||
return;
|
||||
}
|
||||
WiFiEventCbList_t newEventHandler;
|
||||
newEventHandler.cb = cbEvent;
|
||||
newEventHandler.event = event;
|
||||
cbEventList.push_back(newEventHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a callback form event handler
|
||||
* @param cbEvent WiFiEventCb
|
||||
* @param event optional filter (WIFI_EVENT_MAX is all events)
|
||||
*/
|
||||
void ESP8266WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, WiFiEvent_t event) {
|
||||
if(!cbEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
||||
WiFiEventCbList_t entry = cbEventList[i];
|
||||
if(entry.cb == cbEvent && entry.event == event) {
|
||||
cbEventList.erase(cbEventList.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,8 +109,13 @@ void ESP8266WiFiGenericClass::_eventCallback(void* arg) {
|
||||
WiFiClient::stopAll();
|
||||
}
|
||||
|
||||
if(_cbEvent) {
|
||||
_cbEvent((WiFiEvent_t) event->event);
|
||||
for(uint32_t i = 0; i < cbEventList.size(); i++) {
|
||||
WiFiEventCbList_t entry = cbEventList[i];
|
||||
if(entry.cb) {
|
||||
if(entry.event == (WiFiEvent_t) event->event || entry.event == WIFI_EVENT_MAX) {
|
||||
entry.cb((WiFiEvent_t) event->event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,11 @@
|
||||
|
||||
typedef void (*WiFiEventCb)(WiFiEvent_t event);
|
||||
|
||||
typedef struct {
|
||||
WiFiEventCb cb;
|
||||
WiFiEvent_t event;
|
||||
} WiFiEventCbList_t;
|
||||
|
||||
class ESP8266WiFiGenericClass {
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
// -------------------------------------- Generic WiFi function ---------------------------------
|
||||
@ -36,7 +41,8 @@ class ESP8266WiFiGenericClass {
|
||||
|
||||
ESP8266WiFiGenericClass();
|
||||
|
||||
void onEvent(WiFiEventCb cbEvent);
|
||||
void onEvent(WiFiEventCb cbEvent, WiFiEvent_t event = WIFI_EVENT_MAX);
|
||||
void removeEvent(WiFiEventCb cbEvent, WiFiEvent_t event = WIFI_EVENT_MAX);
|
||||
|
||||
int32_t channel(void);
|
||||
|
||||
@ -61,7 +67,6 @@ class ESP8266WiFiGenericClass {
|
||||
|
||||
protected:
|
||||
static bool _persistent;
|
||||
static WiFiEventCb _cbEvent;
|
||||
static WiFiMode_t _forceSleepLastMode;
|
||||
|
||||
static void _eventCallback(void *event);
|
||||
|
Reference in New Issue
Block a user