Skip to content

Commit 85905c1

Browse files
committed
allow multiple event callbacks and add filter option
1 parent 61440d9 commit 85905c1

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

cores/esp8266/Arduino.h

+4
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,12 @@ void optimistic_yield(uint32_t interval_us);
247247
#include "Updater.h"
248248
#include "debug.h"
249249

250+
#ifndef _GLIBCXX_VECTOR
251+
// arduino is not compatible with std::vector
250252
#define min(a,b) ((a)<(b)?(a):(b))
251253
#define max(a,b) ((a)>(b)?(a):(b))
254+
#endif
255+
252256
#define _min(a,b) ((a)<(b)?(a):(b))
253257
#define _max(a,b) ((a)>(b)?(a):(b))
254258

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+41-5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ extern "C" {
4343

4444
#include "debug.h"
4545

46+
#undef min
47+
#undef max
48+
#include <vector>
49+
4650
extern "C" void esp_schedule();
4751
extern "C" void esp_yield();
4852

4953
// -----------------------------------------------------------------------------------------------------------------------
5054
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
5155
// -----------------------------------------------------------------------------------------------------------------------
5256

57+
// arduino dont like std::vectors move static here
58+
static std::vector<WiFiEventCbList_t> cbEventList;
59+
5360
bool ESP8266WiFiGenericClass::_persistent = true;
54-
WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL;
5561
WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF;
5662

5763
ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() {
@@ -61,9 +67,34 @@ ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() {
6167
/**
6268
* set callback function
6369
* @param cbEvent WiFiEventCb
70+
* @param event optional filter (WIFI_EVENT_MAX is all events)
6471
*/
65-
void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent) {
66-
_cbEvent = cbEvent;
72+
void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent, WiFiEvent_t event) {
73+
if(!cbEvent) {
74+
return;
75+
}
76+
WiFiEventCbList_t newEventHandler;
77+
newEventHandler.cb = cbEvent;
78+
newEventHandler.event = event;
79+
cbEventList.push_back(newEventHandler);
80+
}
81+
82+
/**
83+
* removes a callback form event handler
84+
* @param cbEvent WiFiEventCb
85+
* @param event optional filter (WIFI_EVENT_MAX is all events)
86+
*/
87+
void ESP8266WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, WiFiEvent_t event) {
88+
if(!cbEvent) {
89+
return;
90+
}
91+
92+
for(uint32_t i = 0; i < cbEventList.size(); i++) {
93+
WiFiEventCbList_t entry = cbEventList[i];
94+
if(entry.cb == cbEvent && entry.event == event) {
95+
cbEventList.erase(cbEventList.begin() + i);
96+
}
97+
}
6798
}
6899

69100
/**
@@ -78,8 +109,13 @@ void ESP8266WiFiGenericClass::_eventCallback(void* arg) {
78109
WiFiClient::stopAll();
79110
}
80111

81-
if(_cbEvent) {
82-
_cbEvent((WiFiEvent_t) event->event);
112+
for(uint32_t i = 0; i < cbEventList.size(); i++) {
113+
WiFiEventCbList_t entry = cbEventList[i];
114+
if(entry.cb) {
115+
if(entry.event == (WiFiEvent_t) event->event || entry.event == WIFI_EVENT_MAX) {
116+
entry.cb((WiFiEvent_t) event->event);
117+
}
118+
}
83119
}
84120
}
85121

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
typedef void (*WiFiEventCb)(WiFiEvent_t event);
2929

30+
typedef struct {
31+
WiFiEventCb cb;
32+
WiFiEvent_t event;
33+
} WiFiEventCbList_t;
34+
3035
class ESP8266WiFiGenericClass {
3136
// ----------------------------------------------------------------------------------------------
3237
// -------------------------------------- Generic WiFi function ---------------------------------
@@ -36,7 +41,8 @@ class ESP8266WiFiGenericClass {
3641

3742
ESP8266WiFiGenericClass();
3843

39-
void onEvent(WiFiEventCb cbEvent);
44+
void onEvent(WiFiEventCb cbEvent, WiFiEvent_t event = WIFI_EVENT_MAX);
45+
void removeEvent(WiFiEventCb cbEvent, WiFiEvent_t event = WIFI_EVENT_MAX);
4046

4147
int32_t channel(void);
4248

@@ -61,7 +67,6 @@ class ESP8266WiFiGenericClass {
6167

6268
protected:
6369
static bool _persistent;
64-
static WiFiEventCb _cbEvent;
6570
static WiFiMode_t _forceSleepLastMode;
6671

6772
static void _eventCallback(void *event);

0 commit comments

Comments
 (0)