22#include < Schedule.h>
33#include " Arduino.h"
44
5- #if defined(ESP8266)
6-
7- // Duplicate typedefs from core_esp8266_wiring_digital.cpp
8- // Keep in sync
9- typedef void (*voidFuncPtr)(void );
10- typedef void (*voidFuncPtrArg)(void *);
11-
12- typedef struct {
13- uint8_t mode;
14- voidFuncPtr fn;
15- void * arg;
16- } interrupt_handler_t ;
17-
18- // Helper functions for Functional interrupt routines
19- extern " C" interrupt_handler_t * __getInterruptHandler (uint8_t pin);
20-
21- #elif defined(ESP32)
22-
23- // Duplicate typedefs from esp32-hal-gpio.c
24- // Keep in sync
25- typedef void (*voidFuncPtr)(void );
26- typedef void (*voidFuncPtrArg)(void *);
27- typedef struct {
28- voidFuncPtr fn;
29- void * arg;
30- } InterruptHandle_t;
31-
32- // Helper functions for Functional interrupt routines
33- extern " C" InterruptHandle_t* __getInterruptHandler (uint8_t pin);
34-
35- #endif
36-
375void ICACHE_RAM_ATTR interruptFunctional (void * arg)
386{
397 ArgStructure* localArg = static_cast <ArgStructure*>(arg);
@@ -44,32 +12,30 @@ void ICACHE_RAM_ATTR interruptFunctional(void* arg)
4412 }
4513 if (localArg->functionInfo ->reqScheduledFunction )
4614 {
47- schedule_function (std::bind (localArg->functionInfo ->reqScheduledFunction ,InterruptInfo (*(localArg->interruptInfo ))));
15+ schedule_function (
16+ [reqScheduledFunction = localArg->functionInfo ->reqScheduledFunction ,
17+ interruptInfo = *localArg->interruptInfo ]() { reqScheduledFunction (interruptInfo); });
4818 }
4919 if (localArg->functionInfo ->reqFunction )
5020 {
5121 localArg->functionInfo ->reqFunction ();
5222 }
5323}
5424
55- void cleanupFunctional (void * arg)
56- {
57- ArgStructure* localArg = static_cast <ArgStructure*>(arg);
58- delete localArg;
59- }
25+ void cleanupFunctional (void * arg)
26+ {
27+ ArgStructure* localArg = static_cast <ArgStructure*>(arg);
28+ delete localArg;
29+ }
6030
6131void attachInterrupt (uint8_t pin, std::function<void (void )> intRoutine, int mode)
6232{
6333 // use the local interrupt routine which takes the ArgStructure as argument
6434
65- #if defined(ESP8266)
66- interrupt_handler_t * handler = __getInterruptHandler (pin);
67- #elif defined(ESP32)
68- InterruptHandle_t* handler = __getInterruptHandler (pin);
69- #endif
70- if (handler->arg )
35+ void * localArg = detachInterruptArg (pin);
36+ if (localArg)
7137 {
72- cleanupFunctional (handler-> arg );
38+ cleanupFunctional (localArg );
7339 }
7440
7541 FunctionInfo* fi = new FunctionInfo;
@@ -78,19 +44,15 @@ void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode
7844 ArgStructure* as = new ArgStructure;
7945 as->functionInfo = fi;
8046
81- :: attachInterruptArg (pin, static_cast <voidFuncPtrArg>( interruptFunctional) , as, mode);
47+ attachInterruptArg (pin, interruptFunctional, as, mode);
8248}
8349
8450void attachScheduledInterrupt (uint8_t pin, std::function<void (InterruptInfo)> scheduledIntRoutine, int mode)
8551{
86- #if defined(ESP8266)
87- interrupt_handler_t * handler = __getInterruptHandler (pin);
88- #elif defined(ESP32)
89- InterruptHandle_t* handler = __getInterruptHandler (pin);
90- #endif
91- if (handler->arg )
52+ void * localArg = detachInterruptArg (pin);
53+ if (localArg)
9254 {
93- cleanupFunctional (handler-> arg );
55+ cleanupFunctional (localArg );
9456 }
9557
9658 InterruptInfo* ii = new InterruptInfo (pin);
@@ -102,20 +64,14 @@ void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> sc
10264 as->interruptInfo = ii;
10365 as->functionInfo = fi;
10466
105- :: attachInterruptArg (pin, static_cast <voidFuncPtrArg>( interruptFunctional) , as, mode);
67+ attachInterruptArg (pin, interruptFunctional, as, mode);
10668}
10769
10870void detachFunctionalInterrupt (uint8_t pin)
10971{
110- #if defined(ESP8266)
111- interrupt_handler_t * handler = __getInterruptHandler (pin);
112- #elif defined(ESP32)
113- InterruptHandle_t* handler = __getInterruptHandler (pin);
114- #endif
115- if (handler->arg )
72+ void * localArg = detachInterruptArg (pin);
73+ if (localArg)
11674 {
117- cleanupFunctional (handler-> arg );
75+ cleanupFunctional (localArg );
11876 }
119- ::detachInterrupt (pin);
12077}
121-
0 commit comments