Skip to content

Commit 9d84684

Browse files
committed
Migrate to Delegate class
1 parent 16c555a commit 9d84684

File tree

2 files changed

+12
-52
lines changed

2 files changed

+12
-52
lines changed

cores/esp8266/Arduino.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void optimistic_yield(uint32_t interval_us);
224224
#include <algorithm>
225225
#include <cstdlib>
226226
#include <cmath>
227-
#include <functional>
227+
#include "Delegate.h"
228228

229229
using std::min;
230230
using std::max;
@@ -265,7 +265,7 @@ void setTZ(const char* tz);
265265
void configTime(int timezone, int daylightOffset_sec, const char* server1,
266266
const char* server2 = nullptr, const char* server3 = nullptr);
267267

268-
void attachInterrupt(uint8_t pin, std::function<void()> userFunc, int mode);
268+
void attachInterrupt(uint8_t pin, Delegate<void, void*> userFunc, int mode);
269269

270270
void configTime(const char* tz, const char* server1,
271271
const char* server2 = nullptr, const char* server3 = nullptr);

cores/esp8266/core_esp8266_wiring_digital.cpp

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -123,45 +123,17 @@ extern "C" {
123123
interrupt_handler_t()
124124
{
125125
mode = 0;
126-
isFunctional = false;
127-
fn = nullptr;
128-
arg = nullptr;
129126
}
130127
~interrupt_handler_t()
131128
{
132-
using std::function;
133-
if (isFunctional)
134-
functional.~function<void()>();
135129
}
136130
uint8_t mode;
137-
bool isFunctional;
138-
union {
139-
struct {
140-
voidFuncPtrArg fn;
141-
void* arg;
142-
};
143-
std::function<void()> functional;
144-
};
131+
Delegate<void, void*> userFunc;
145132
};
146133

147134
static interrupt_handler_t interrupt_handlers[16];
148135
static uint32_t interrupt_reg = 0;
149136

150-
void ICACHE_RAM_ATTR set_interrupt_handlers(uint8_t pin, voidFuncPtrArg userFunc, void* arg, uint8_t mode)
151-
{
152-
using std::function;
153-
interrupt_handler_t& handler = interrupt_handlers[pin];
154-
if (handler.isFunctional)
155-
{
156-
handler.functional.~function<void()>();
157-
handler.isFunctional = false;
158-
}
159-
handler.fn = userFunc;
160-
handler.arg = arg;
161-
if (handler.fn)
162-
handler.mode = mode;
163-
}
164-
165137
void ICACHE_RAM_ATTR interrupt_handler(void *arg, void *frame)
166138
{
167139
(void) arg;
@@ -184,10 +156,7 @@ extern "C" {
184156
// to make ISR compatible to Arduino AVR model where interrupts are disabled
185157
// we disable them before we call the client ISR
186158
esp8266::InterruptLock irqLock; // stop other interrupts
187-
if (handler.isFunctional)
188-
handler.functional();
189-
else
190-
handler.fn(handler.arg);
159+
handler.userFunc();
191160
}
192161
++i;
193162
}
@@ -219,13 +188,7 @@ extern "C" {
219188
extern void __attachInterruptArg(uint8_t pin, voidFuncPtrArg const userFunc, void* const arg, int mode)
220189
{
221190
isr_iram_assertion((uint32_t)userFunc);
222-
if (pin < 16)
223-
{
224-
ETS_GPIO_INTR_DISABLE();
225-
set_interrupt_handlers(pin, userFunc, arg, mode);
226-
set_interrupt_reg(pin, mode);
227-
ETS_GPIO_INTR_ENABLE();
228-
}
191+
attachInterrupt(pin, Delegate<void, void*>(userFunc, arg), mode);
229192
}
230193

231194
extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int mode)
@@ -241,7 +204,9 @@ extern "C" {
241204
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
242205
GPIEC = (1 << pin); //Clear Interrupt for this pin
243206
interrupt_reg &= ~(1 << pin);
244-
set_interrupt_handlers(pin, nullptr, nullptr, 0);
207+
interrupt_handler_t& handler = interrupt_handlers[pin];
208+
handler.userFunc = nullptr;
209+
handler.mode = 0;
245210
if (interrupt_reg)
246211
{
247212
ETS_GPIO_INTR_ENABLE();
@@ -278,21 +243,16 @@ extern "C" {
278243

279244
namespace
280245
{
281-
void set_interrupt_handlers(uint8_t pin, std::function<void()>&& userFunc, uint8_t mode)
246+
void ICACHE_RAM_ATTR set_interrupt_handlers(uint8_t pin, Delegate<void, void*>&& userFunc, uint8_t mode)
282247
{
283248
interrupt_handler_t& handler = interrupt_handlers[pin];
284-
if (!handler.isFunctional)
285-
{
286-
new (&handler.functional) std::function<void()>();
287-
handler.isFunctional = true;
288-
}
289-
if (userFunc)
249+
handler.userFunc = std::move(userFunc);
250+
if (handler.userFunc)
290251
handler.mode = mode;
291-
handler.functional = std::move(userFunc);
292252
}
293253
}
294254

295-
extern void attachInterrupt(uint8_t pin, std::function<void()> userFunc, int mode)
255+
extern void attachInterrupt(uint8_t pin, Delegate<void, void*> userFunc, int mode)
296256
{
297257
if (pin < 16)
298258
{

0 commit comments

Comments
 (0)